- This PR adds more flexibility to
Field::Select
so we can set an array of arrays and a hash tocollection
option. This behavior would be more intuitive since it follows Rails form helper convention. - This also solves the absence of
Enum
field referred to #322 and #533 (closed) so we don't have to create a Enum field as a custom or built-in field anymore. - Added select_spec.rb so we can test Select field
By default, it displays label data(e.g. "Pending") instead of raw persisted data(e.g. 1) on show/index page. Search feature would be an issue because users would want to search by label data but the system searches only by persisted data for the moment. However, that issue should be solved by another PR, not this one. Thank you for this awesome gem
Basic usage:
status: Field::Select.with_options(collection: [ ['Pending',1], ['Approved',2], ['Rejected',3] ])
=>
<select name="post[status]" id="post_status">
<option selected="selected" value="1">Pending</option>
<option value="2">Approved</option>
<option value="3">Rejected</option>
</select>
For Enum:
status: Field::Select.with_options(collection: Post.localized_statuses.invert)
class Post < ApplicationRecord
enum status: { submitted: 0, approved: 1, rejected: 2 }
def self.localized_statuses
I18n.t 'activerecord.attributes.post/status'
end
# or
# def self.localized_statuses
# Post.statuses.keys.map {|k| [k, Post.human_attribute_name("post.status.#{k}")] }
# end
end
activerecord:
attributes:
post/status:
submitted: "承認待ち"
approved: "承認済み"
rejected: "取り消し"