Skip to content

Commit

Permalink
Include_blank in Belongs to form (#1222)
Browse files Browse the repository at this point in the history
Replaces the functionality wherein nil is already included in the possible
options of belongs_to. In the current functionality of administrate, if you
add `include_blank: true` in f.select it would have double blank option in the
selections.

Changes in spec/factories.rb where made by running:

```
rubocop \
  --require rubocop-rspec \
  --only FactoryBot/AttributeDefinedStatically \
  --auto-correct
```

Co-authored-by: Bryan Mark Fajutag <bryan.fajutag@adish.co.jp>
  • Loading branch information
brynmrk and Bryan Mark Fajutag authored Oct 5, 2020
1 parent eda8f53 commit 23a5bf4
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 5 deletions.
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

0 comments on commit 23a5bf4

Please sign in to comment.