Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. class AwesomeNestedSetInput < Formtastic::Inputs::CheckBoxesInput
  2. def to_html
  3. unless options[:nested_set]
  4. super
  5. else
  6. nested_wrapping(options)
  7. end
  8. end
  9.  
  10. def nested_wrapping(options)
  11. input_wrapping do
  12. choices_wrapping do
  13. legend_html <<
  14. hidden_field_for_all <<
  15. choices_group_wrapping do
  16. html_template_for_nested_set(options)
  17. end
  18. end
  19. end
  20. end
  21.  
  22. def html_template_for_nested_set(options)
  23. if options[:show_current_first]
  24. model = reflection.options[:through].to_s.singularize.camelize.constantize
  25. additional_collection = options[:collection].first.class.where(
  26. :id => model.where(object.class.name.downcase.to_sym => object).pluck(:category_id)
  27. ).where(:parent_id => nil) rescue nil
  28.  
  29. collection = additional_collection + options[:collection].where.not(:id => additional_collection.ids) rescue nil
  30. else
  31. collection = options[:collection]
  32. end
  33.  
  34. if collection.present?
  35. collection.map{|menu|
  36. html_for_nested(menu)
  37. }.join("\n").html_safe
  38. end
  39. end
  40.  
  41. def html_for_nested(menu, from_nested=false)
  42. choice = [menu.title, menu.id]
  43. first_wrap = choice_wrapping(choice_wrapping_html_options(choice)) do
  44. choice_html(choice) << sub_children(menu)
  45. end
  46. end
  47.  
  48. def sub_children(menu)
  49. template.content_tag( :ul,
  50. menu.children.collect do |child|
  51. html_for_nested(child, true)
  52. end.join("\n").html_safe,
  53. {:class=>"sub_item-#{menu.id} sub-item"}
  54. ) unless menu.leaf?
  55. end
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement