Skip to content

Commit

Permalink
Add :include_blank option to Field::Select
Browse files Browse the repository at this point in the history
- copy/pasted pretty directly from Field::BelongsTo
  • Loading branch information
Eli Fatsi committed Jul 14, 2021
1 parent 9d20aad commit 4991c34
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/views/fields/select/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ to be displayed on a resource's edit form page.
:last,
:first,
field.data,
)
),
include_blank: field.include_blank_option
) %>
<% else %>
<%= f.select(
Expand All @@ -37,7 +38,8 @@ to be displayed on a resource's edit form page.
:to_s,
:to_s,
field.data,
)
),
include_blank: field.include_blank_option
) %>
<% end %>
</div>
3 changes: 3 additions & 0 deletions docs/customizing_dashboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ an array or an object responding to `:call`. Defaults to `[]`.
`:searchable` - Specify if the attribute should be considered when searching.
Default is `true`.

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

**Field::String**

`:searchable` - Specify if the attribute should be considered when searching.
Expand Down
4 changes: 4 additions & 0 deletions lib/administrate/field/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def selectable_options
collection
end

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

private

def collection
Expand Down
1 change: 1 addition & 0 deletions spec/administrate/views/fields/select/_edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
attribute: :email_subscriber,
data: false,
selectable_options: [true, false, nil],
include_blank_option: true,
)

render(
Expand Down
32 changes: 32 additions & 0 deletions spec/features/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,36 @@
expect(element_selections.first("option").value).not_to eq("")
end
end

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

expect(kind.deferred_class).to eq(Administrate::Field::Select)
expect(kind.options[:include_blank]).to eq(true)

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

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
kind = fields[:kind]
kind.options[:include_blank] = false

expect(kind.deferred_class).to eq(Administrate::Field::Select)
expect(kind.options[:include_blank]).to eq(false)

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

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

0 comments on commit 4991c34

Please sign in to comment.