Making FormBuilder#select work better with ActiveRecord collections

I’ve been working on a custom form builder that adds several convenient features to select(); together, they make it easier to select from a collection of ActiveRecords. Basically, instead of doing this:

f.select(:folder_id, @folders.map { |folder| [folder.name, folder.id] }, disabled: @folders.select(&:deleted?).map(&:id))

You can do this:

f.select(:folder_id, @folders, record_label: :name, disabled: :deleted?)

With these changes, the disabled option can now take a proc or symbol that filters the available choices; the record_label option tells select() that the choices are ActiveRecord objects and how to get the “name” for each one.

I’ve pasted my current implementation below—it’s not ready to be dropped into ActionView (it should be factored differently, could probably be written better, and it doesn’t support grouped options), but it should give you an idea of what I have in mind.

Is this something people might be interested in incorporating into Rails? If so, I can start turning this into a workable patch; if not, I needn’t bother.

def select(method, choices, options = {}, html_options = {})

If disabled option is a Symbol, Method, or Proc, build a

disabled list by filtering the choices list with it.

options[:disabled] = choices.select(&options[:disabled]) if options[:disabled].respond_to? :to_proc

The record_label option is a shorthand for:

choice_template: [record_label, :id]

if label = options.delete(:record_label)

options[:choice_template] = [label, :id]

end

If choice_template option is present, run all the choices through it.

if template = options.delete(:choice_template)

template = make_array(template).map(&:to_proc)

choices = choices.map { |choice| template.map { |t| t.(choice) } }

If :disabled is present, run its contents through the same template (well, the last

item in the template, which controls the value).

options[:disabled] = make_array(options[:disabled]).map(&template.last)

end

Call into super

super(method, choices, options, html_options)

end

private

def make_array(obj)

Array.try_convert(obj) || [obj]

end

Does not the collection_select work o this way? I know it has a label_method that you can pass name to get the same behavior, and if I’m not wrong disabled can accept a proc.

You’re right—that could have saved me some time yesterday!

It might still make sense to make collection_select()'s :disabled (and :selected) treat a Symbol as a method to call on each object in the collection, instead of requiring you to write a proc. Would there be interest in a more limited patch that did that, or should I just buckle down and write a proc?