Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AwesomeNestedSetInput < Formtastic::Inputs::CheckBoxesInput
- def to_html
- unless options[:nested_set]
- super
- else
- nested_wrapping(options)
- end
- end
- def nested_wrapping(options)
- input_wrapping do
- choices_wrapping do
- legend_html <<
- hidden_field_for_all <<
- choices_group_wrapping do
- html_template_for_nested_set(options)
- end
- end
- end
- end
- def html_template_for_nested_set(options)
- if options[:show_current_first]
- model = reflection.options[:through].to_s.singularize.camelize.constantize
- additional_collection = options[:collection].first.class.where(
- :id => model.where(object.class.name.downcase.to_sym => object).pluck(:category_id)
- ).where(:parent_id => nil) rescue nil
- collection = additional_collection + options[:collection].where.not(:id => additional_collection.ids) rescue nil
- else
- collection = options[:collection]
- end
- if collection.present?
- collection.map{|menu|
- html_for_nested(menu)
- }.join("\n").html_safe
- end
- end
- def html_for_nested(menu, from_nested=false)
- choice = [menu.title, menu.id]
- first_wrap = choice_wrapping(choice_wrapping_html_options(choice)) do
- choice_html(choice) << sub_children(menu)
- 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,
- {:class=>"sub_item-#{menu.id} sub-item"}
- ) unless menu.leaf?
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement