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

Allow for label/value setting in Field::Select collection #1646

Merged
merged 2 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 21 additions & 9 deletions app/views/fields/select/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,25 @@ to be displayed on a resource's edit form page.
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.select(
field.attribute,
options_from_collection_for_select(
field.selectable_options,
:to_s,
:to_s,
field.data.presence,
)
) %>
<% if field.selectable_options.first&.is_a?(Array) %>
<%= f.select(
field.attribute,
options_from_collection_for_select(
field.selectable_options,
:last,
:first,
field.data.presence,
)
) %>
<% else %>
<%= f.select(
field.attribute,
options_from_collection_for_select(
field.selectable_options,
:to_s,
:to_s,
field.data.presence,
)
) %>
<% end %>
</div>
18 changes: 18 additions & 0 deletions spec/features/products_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@
end
end

it "respects setting of label/value in Field::Select" do
old_release_year = ProductDashboard::ATTRIBUTE_TYPES[:release_year]

ProductDashboard::ATTRIBUTE_TYPES[:release_year] = Administrate::Field::Select.with_options(
collection: [
["Last Year", 2019],
["This Year", 2020]
],
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit of a hacky way to test out the new functionality. Let me know if you'd prefer I add some arbitrary functionality to the example app to avoid the use of temporarily resetting this attribute type.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

["Last Year", 2019],


visit new_admin_product_path
expect(page).to have_select("Release year", with_options: ["This Year"])
expect(page.find("option", match: :first).text).to eq("Last Year")
expect(page.find("option", match: :first).value).to eq("2019")

ProductDashboard::ATTRIBUTE_TYPES[:release_year] = old_release_year
end

it "edits product and meta tag data correctly" do
product = create(:product)

Expand Down