Add block to `collection_check_boxes` and `collection_radio_buttons`.
lcreid opened this issue · 3 comments
The Rails collection_check_boxes
and collection_radio_button
methods can take a block. If a block is provided, the helper yields a special builder to the block. The idea is that the programmer can format the check box or radio button more precisely in the block, if the standard format produced by the Rails helpers doesn't give the desired results.
Our helpers do not take a block. We can speculate that since our helpers format the check boxes and radio buttons in a consistent and pleasing way, there's less need for the block. There may also have been a technical reason that made yielding to a block difficult. I haven't investigated enough to have an idea of that.
However, there still may arise situations in which it would be convenient to have the ability to output the controls in a block. On example would be when you want to show all the check boxes or radio buttons, but some of them need to be disabled. Another example might be to highlight certain "dangerous" check boxes or radio buttons with the "danger" colour.
Another argument in favour of providing this is simply to make our helpers more closely mimic the behaviour of the Rails helpers. I bet there are a number of cases of people wasting an hour or two trying to get a block to work with our helpers before they realized that our helper doesn't work that way.
One alternative is for programmers to code the loop themselves. This is probably not a large burden, although they would have to remember to add the div.form-group
and the error handling in order to be 100 percent compatible with our helpers.
Would we consider a pull request for this functionality if someone submitted one?
Here's a specific example I'm having trouble implementing: I have a has_many :team_members, through:
on the Subject
model. I want to provide a simple user interface with a check boxes for each person on staff. Checking the box makes them a member of the team. However, I want to prevent the user from removing themself from the team, so I don't want to show the check box for the current user.
In ordinary Rails I could do this:
<%= form_for @subject do |f| %>
<%= f.collection_check_boxes(:team_member_ids, Person.all, :id, :name) do |builder| %>
<% unless current_user == builder.object %>
<%= builder.check_box %><%= builder.name %>
<% end %>
<p><%= f.submit %></p>
<% end %>
I can't seem to find a similarly elegant way to do this with bootstrap_form
's collection_check_box
. @mattbrictson or anyone else, do you have any ideas for this? Am I missing something?
Another thought I'm having is perhaps in addition to the value_method
and text_method
parameters, add an options_method
parameter. This could be a proc, which gets called for each item, receives the object as a parameter, and generates additional options for the underlying check_box.
In my case, I want the checkboxes to toggle visibility of a div elsewhere on the page, so I might do something like:
= f.collection_check_boxes :toggleable_things, Toggleable.all, :id, :name, ->(t) { { data: { toggle: dom_id(t) } } }
and thus each checkbox would have an attribute like data-toggle="toggleable_123"
In @lcreid 's example right now, I would just replace Person.all
with something like Person.where.not(id: current_user.id)
(why include something in the collection just to skip it?)- however, maybe you want that shown but disabled, so you might do:
= f.collection_check_boxes(:team_member_ids, Person.all, :id, :name, ->(p) { { disabled: p == current_user } })
This seems like it should be a bit easier to manage and fairly elegant. Another parameter could be permitted for label_options
. Each could also be permitted as a symbol which calls that method on the underlying object.
One alternative is for programmers to code the loop themselves. This is probably not a large burden, although they would have to remember to add the
div.form-group
and the error handling in order to be 100 percent compatible with our helpers.
@lcreid can you provide an example to be 100 percent compatible with your helpers?