Skip to content

Instantly share code, notes, and snippets.

@michelson
Created April 5, 2012 21:56
Show Gist options
  • Save michelson/2314495 to your computer and use it in GitHub Desktop.
Save michelson/2314495 to your computer and use it in GitHub Desktop.
Formtastic check boxes with awesome_nested_set
# put this file in any autoloaded path on rails app. HINT: app/inputs/check_boxes_input.rb
class CheckBoxesInput < Formtastic::Inputs::CheckBoxesInput
def to_html
unless options[:nested_set]
super
else
nested_wrapping(options)
end
end
def nested_wrapping(options)
choices_wrapping do
legend_html <<
hidden_field_for_all <<
choices_group_wrapping do
html_template_for_nested_set(options)
end
end
end
def html_template_for_nested_set(options)
options[:collection].map{|menu|
html_for_nested(menu)
}.join("\n").html_safe
end
def html_for_nested(menu, from_nested=false)
choice = [menu.name , menu.id]
first_wrap = choice_wrapping(choice_wrapping_html_options(choice)) do
nested = from_nested ? "" : sub_children(menu)
choice_html(choice) << nested
end
end
def sub_children(menu)
template.content_tag( :ul,
menu.children.collect do |child|
html_for_nested(child, true)
end.join("\n").html_safe,
{:style=>"margin-left:20px", :class=>"sub_item-#{menu.id} sub-item"}
) unless menu.leaf?
end
end

model

class Category < ActiveRecord::Base
   acts_as_nested_set
....

View

= f.input :categories, :as=>:check_boxes, :collection=>Category.where(["parent_id is NULL"]) , :nested_set=>true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment