Administrate: Feature Request: Options From Collection For Select

Created on 11 Apr 2017  路  2Comments  路  Source: thoughtbot/administrate

I have just worked through and implemented a Field::Select after reading #420 and using the new Field::Select.with_options. Thanks for everyone being open and providing tips on that one.

If possible, I am trying to get the options_from_collection_for_select to work.

I'll have a bash at it and report back here but not sure if this is something that will work out of the box with the Custom Field Types Feature or if I need to be a bit more cleverer than I am about it.

This would be a nice feature to document though.

Again, I really appreciate this gem, I have used ActiveAdmin in the past but this seems much easier from my intermediate user perspective.

Most helpful comment

I have managed to get this working by creating the following custom field type

This works soooo nice. It probably just needs a private method and option for a default value as per the method in rails.

I implemented this so I can select a users manager from a dropdown of existing active managers.

app/fields/collection_select_field.rb

require "administrate/field/base"

class CollectionSelectField < Administrate::Field::Base
  def to_s
    data
  end

  def selectable_options
    collection
  end

  def selectable_value
    value_method
  end

  def selectable_text
    text_method
  end

  private

  def collection
    @collection ||= options.fetch(:collection, [])
  end

  def value_method
    @value_method ||= options.fetch(:value_method, nil)
  end

  def text_method
    @text_method ||= options.fetch(:text_method, nil)
  end
end

app/dashboards/object_dashboard.rb

    manager_id: CollectionSelectField.with_options(
      collection: User.active.managers,
      value_method: "id",
      text_method: "email",
    )

app/views/fields/show.html.erb

<div class="field-unit__label">
  <%= f.label field.attribute %>
</div>
<div class="field-unit__field">
  <%= f.select(
    field.attribute,
    options_from_collection_for_select(
      field.selectable_options,
      field.selectable_value,
      field.selectable_text,
      field.data.presence,
    )
  ) %>
</div>

I'd submit a pull request but I've never done one before and I suck at tests. But I would like to do this cause I think the functionality would be liked by a lot of users of this awesome gem.

All 2 comments

I have managed to get this working by creating the following custom field type

This works soooo nice. It probably just needs a private method and option for a default value as per the method in rails.

I implemented this so I can select a users manager from a dropdown of existing active managers.

app/fields/collection_select_field.rb

require "administrate/field/base"

class CollectionSelectField < Administrate::Field::Base
  def to_s
    data
  end

  def selectable_options
    collection
  end

  def selectable_value
    value_method
  end

  def selectable_text
    text_method
  end

  private

  def collection
    @collection ||= options.fetch(:collection, [])
  end

  def value_method
    @value_method ||= options.fetch(:value_method, nil)
  end

  def text_method
    @text_method ||= options.fetch(:text_method, nil)
  end
end

app/dashboards/object_dashboard.rb

    manager_id: CollectionSelectField.with_options(
      collection: User.active.managers,
      value_method: "id",
      text_method: "email",
    )

app/views/fields/show.html.erb

<div class="field-unit__label">
  <%= f.label field.attribute %>
</div>
<div class="field-unit__field">
  <%= f.select(
    field.attribute,
    options_from_collection_for_select(
      field.selectable_options,
      field.selectable_value,
      field.selectable_text,
      field.data.presence,
    )
  ) %>
</div>

I'd submit a pull request but I've never done one before and I suck at tests. But I would like to do this cause I think the functionality would be liked by a lot of users of this awesome gem.

I think this functionality was introduced at https://github.com/thoughtbot/administrate/pull/1646. It works slightly differently: the collection is expected to be an array of arrays in the shape [[label, value], [label, value], ...], but I think it serves the same purpose. I'm going to close this issue, but feel free to leave a comment if you think there's more to it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  4Comments

Reedian picture Reedian  路  4Comments

conwayanderson picture conwayanderson  路  4Comments

migu0 picture migu0  路  3Comments

sedubois picture sedubois  路  4Comments