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

Add block to collection_check_boxes and collection_radio_buttons. #477

Open
lcreid opened this issue Jul 15, 2018 · 3 comments
Open

Add block to collection_check_boxes and collection_radio_buttons. #477

lcreid opened this issue Jul 15, 2018 · 3 comments

Comments

@lcreid
Copy link
Contributor

lcreid commented Jul 15, 2018

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?

@lcreid
Copy link
Contributor Author

lcreid commented Jul 23, 2018

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?

@stephancom
Copy link

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.

@lapser
Copy link

lapser commented Nov 27, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants