Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include_blank in Belongs to form #1222

Merged
merged 1 commit into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/views/fields/belongs_to/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ that displays all possible records to associate with.
<%= f.label field.permitted_attribute %>
</div>
<div class="field-unit__field">
<%= f.select(field.permitted_attribute) do %>
<%= options_for_select(field.associated_resource_options, field.selected_option) %>
<% end %>
<%= f.select(field.permitted_attribute,
options_for_select(field.associated_resource_options, field.selected_option),
include_blank: field.include_blank_option) %>
</div>
3 changes: 3 additions & 0 deletions docs/customizing_dashboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ Example: `.with_options(scope: -> { MyModel.includes(:rel).limit(5) })`
`:class_name` - Specifies the name of the associated class.
Defaults to `:#{attribute}.to_s.singularize.camelcase`.

`:include_blank` - Specifies if the select element to be rendered should include
blank option. Default is `true`.

`:searchable` - Specify if the attribute should be considered when searching.
Default is `false`.

Expand Down
6 changes: 5 additions & 1 deletion lib/administrate/field/belongs_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def permitted_attribute
end

def associated_resource_options
[nil] + candidate_resources.map do |resource|
candidate_resources.map do |resource|
[display_candidate_resource(resource), resource.send(primary_key)]
end
end
Expand All @@ -21,6 +21,10 @@ def selected_option
data && data.send(primary_key)
end

def include_blank_option
options.fetch(:include_blank, true)
end

private

def candidate_resources
Expand Down
1 change: 1 addition & 0 deletions spec/example_app/app/dashboards/customer_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CustomerDashboard < Administrate::BaseDashboard
class_name: "Country",
searchable: true,
searchable_fields: ["name"],
include_blank: true,
),
password: Field::Password,
}
Expand Down
34 changes: 34 additions & 0 deletions spec/features/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,38 @@
expect(page).to have_label(custom_label)
end
end

context "include_blank option for belongs_to" do
before { create_list(:country, 5) }

it "should have blank option if set to true" do
dashboard = CustomerDashboard.new
fields = dashboard.attribute_types
territory = fields[:territory]
territory.options[:include_blank] = true

expect(territory.deferred_class).to eq(Administrate::Field::BelongsTo)
expect(territory.options[:include_blank]).to eq(true)

visit new_admin_customer_path
element_selections = find("select[name=\"customer[country_code]\"]")

expect(element_selections.first("option").value).to eq("")
end

it "should not have blank option if set to false" do
dashboard = CustomerDashboard.new
fields = dashboard.attribute_types
territory = fields[:territory]
territory.options[:include_blank] = false

expect(territory.deferred_class).to eq(Administrate::Field::BelongsTo)
expect(territory.options[:include_blank]).to eq(false)

visit new_admin_customer_path
element_selections = find("select[name=\"customer[country_code]\"]")

expect(element_selections.first("option").value).not_to eq("")
end
end
end
28 changes: 27 additions & 1 deletion spec/lib/fields/belongs_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,39 @@
candidates = field.associated_resource_options

expect(Foo).to have_received(:all)
expect(candidates).to eq([nil])
expect(candidates).to eq([])
ensure
remove_constants :Foo
end
end
end

describe "include_blank option" do
context "default value as true" do
it "determines if choices has blank option or not" do
association = Administrate::Field::BelongsTo
field = association.new(:customers, [], :edit)
candidates = field.associated_resource_options

expect(field.include_blank_option). to eq(true)
expect(candidates).to eq([])
end
end

context "set value as false" do
it "determines if choices has blank option or not" do
association = Administrate::Field::BelongsTo.with_options(
include_blank: false,
)
field = association.new(:customers, [], :edit)
candidates = field.associated_resource_options

expect(field.include_blank_option). to eq(false)
expect(candidates).to eq([])
end
end
end

describe "primary_key option" do
it "determines what primary key is used on the relationship for the form" do
begin
Expand Down