administrate icon indicating copy to clipboard operation
administrate copied to clipboard

Make Field::Select more flexible

Open jumjamjohn opened this issue 7 years ago • 1 comments

  • This PR adds more flexibility to Field::Select so we can set an array of arrays and a hash to collection 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 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: "取り消し"

jumjamjohn avatar Sep 25 '18 20:09 jumjamjohn

Any word on this? It would be really useful to have. I feel like Administrate has so much potential and then stuff like this doesn't get merged. Can I do something to fix it and get it merged? I'd be happy to update the syntax if that's the issue here.

bjgaynor avatar Oct 30 '19 21:10 bjgaynor

Closed due to lack of activity. Additionally, I am not sure about overloading collection. In order to make Administrate work with minimum surprises, I think collection should behave exactly like options_for_select. I have done this at https://github.com/thoughtbot/administrate/pull/2348, which also implements enums.

However this means that collection would not be able to provide groups. I think this is fine and instead there should be a separate option for grouped options. Eg: grouped_options, which is the name it receives in the documentation of grouped_options_for_select.

pablobm avatar Apr 17 '23 13:04 pablobm