https://github.com/sergeypedan/formtastic-grouped-checkboxes
Grouped checkboxes input for Formtastic and ActiveAdmin
https://github.com/sergeypedan/formtastic-grouped-checkboxes
activeadmin checkboxes formtastic gem rails ruby ruby-gem ruby-on-rails rubygem
Last synced: 3 months ago
JSON representation
Grouped checkboxes input for Formtastic and ActiveAdmin
- Host: GitHub
- URL: https://github.com/sergeypedan/formtastic-grouped-checkboxes
- Owner: sergeypedan
- License: mit
- Created: 2023-07-08T18:05:31.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-07-09T17:43:29.000Z (almost 3 years ago)
- Last Synced: 2025-12-01T21:41:59.065Z (6 months ago)
- Topics: activeadmin, checkboxes, formtastic, gem, rails, ruby, ruby-gem, ruby-on-rails, rubygem
- Language: Ruby
- Homepage:
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# Formtastic grouped checkboxes
[](https://badge.fury.io/rb/formtastic_grouped_check_boxes)
Group your Formtastic checkboxes like grouped select via “grouped_collection_select” in Rails.
## Installation
Add this line to your application's Gemfile:
```ruby
gem "formtastic_grouped_check_boxes"
```
Import our Sass file “formtastic-grouped-check-boxes.css” in your CSS entrypoint that compiles CSS files:
```sass
// app/assets/stylesheets/application.sass
// Your regular ActiveAdmin files
@import ...
@import ...
// This gem’s files
@import "formtastic-grouped-check-boxes"
```
## Use
This gem adds a new input type `:grouped_check_boxes`.
It also adds 3 new input options, 2 of which follow the Rails naming convention from `grouped_collection_select` ([docs](https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-grouped_collection_select)):
- `group_method` — The name of a method which, when called on a member of `collection`, returns an array of child objects. It can also be a `Proc` / `lambda`, that will be called for each member of the collection to retrieve the value.
- `group_label_method` — The name of a method which, when called on a member of collection, returns a string to be used as the group name. No `Proc` support here yet.
- `group_label_parent` — Whether to prepend the fieldset label with the parent input title (on: “Technologies / Marketing”, off: “Marketing”).
### Example
Providing that our models look like so:
```ruby
class Project < ApplicationRecord
has_many :technologies # details omitted
accepts_nested_attributes_for :technologies
end
```
```ruby
class Technology < ApplicationRecord
has_many :projects # details omitted
belongs_to :area, foreign_key: :area_id, class_name: "TechnologyArea", optional: true
end
# Table name: technologies
# id :bigint not null, primary key
# name :string not null
# area_id :bigint
```
```ruby
class TechnologyArea < ApplicationRecord
has_many :technologies
end
# Table name: technology_areas
# id :bigint not null, primary key
# name :string not null
```
In ActiveAdmin you can do the following
```ruby
ActiveAdmin.register Project do
permit_params technology_ids: [], ...
form do |f|
f.inputs "Technologies" do
f.input :technologies,
as: :grouped_check_boxes, \
collection: Technology.order(:name) \
.select(:id, :name, :area_id) \ # note the `:area_id`
.includes(:area), \ # prevent N+1
group_method: :area, \ # calls `.area` on each instance of `Technology` (that’s why we need `:area_id`)
group_label_method: :name, \ # calls `.name` on each instance of `TechnologyArea`
group_label_parent: true # not required
end
end
end
```
Results in:

While producing such code:
```html
Technologies
-
Technologies / No subgroup
-
BitBucket
-
BrainTree API (Ruby SDK)
Technologies / Marketing
-
Direct sales
-
Google Analytics
```