Skip to content

Commit

Permalink
Select field selected value (#1844)
Browse files Browse the repository at this point in the history
When using the select field for a boolean attribute that can be either `true`, `false` or `nil`, the current behavior causes `false` and `nil` to be treated the same. If the attribute is set to `false`, the edit dashboard shows the selected value as being the `nil` option instead of `false` as expected.
  • Loading branch information
nhippenmeyer authored Dec 19, 2020
1 parent ded79d2 commit 33a46ff
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/views/fields/select/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ to be displayed on a resource's edit form page.
field.selectable_options,
:last,
:first,
field.data.presence,
field.data,
)
) %>
<% else %>
Expand All @@ -36,7 +36,7 @@ to be displayed on a resource's edit form page.
field.selectable_options,
:to_s,
:to_s,
field.data.presence,
field.data,
)
) %>
<% end %>
Expand Down
40 changes: 40 additions & 0 deletions spec/administrate/views/fields/select/_edit_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require "rails_helper"
require "administrate/field/select"

describe "fields/select/_form", type: :view do
it "displays the selected option" do
customer = build(:customer)
select = instance_double(
"Administrate::Field::Select",
attribute: :email_subscriber,
data: false,
selectable_options: [true, false, nil],
)

render(
partial: "fields/select/form.html.erb",
locals: { field: select, f: form_builder(customer) },
)

expect(rendered).to have_css(
%{select[name="customer[email_subscriber]"]
option[value="false"][selected="selected"]},
)
end

def form_builder(object)
ActionView::Helpers::FormBuilder.new(
object.model_name.singular,
object,
build_template,
{},
)
end

def build_template
Object.new.tap do |template|
template.extend ActionView::Helpers::FormHelper
template.extend ActionView::Helpers::FormOptionsHelper
end
end
end

0 comments on commit 33a46ff

Please sign in to comment.