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

Feature Request: Options From Collection For Select #825

Closed
jaykilleen opened this issue Apr 11, 2017 · 2 comments
Closed

Feature Request: Options From Collection For Select #825

jaykilleen opened this issue Apr 11, 2017 · 2 comments

Comments

@jaykilleen
Copy link

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.

@jaykilleen
Copy link
Author

jaykilleen commented Apr 12, 2017

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.

@pablobm
Copy link
Collaborator

pablobm commented Apr 22, 2021

I think this functionality was introduced at #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.

@pablobm pablobm closed this as completed Apr 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants