Guest User

Untitled

a guest
May 27th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. ## the result is to build a dropdown menu bar based off of a dropdown model which has_many webpages:
  2. <ul id="nav">
  3. <li>
  4. <a class="current" href="/home">Home</a>
  5. </li>
  6. <li>
  7. <a href="#">Admin</a>
  8. <ul>
  9. <li>
  10. <a href="/photos">Photos</a>
  11. </li>
  12. <li>
  13. <a href="/photos">Products</a>
  14. </li>
  15. </ul>
  16. </li>
  17. </ul>
  18. ## my old confusing method
  19.  
  20. def build_menu_bar
  21.  
  22. dropdowns = Dropdown.all(:order => 'position ASC')
  23. white_space = "\r\n"
  24.  
  25. content_tag :ul, :id => "nav" do
  26. dropdowns.map do |dropdown|
  27.  
  28. webpage = dropdown.webpages.find_by_name(dropdown.name)
  29. unless webpage.nil?
  30. permalink = webpage.permalink
  31. redirect = webpage.redirect
  32. else
  33. redirect = dropdown.redirect
  34. end
  35. menu_bar_class = permalink == params[:permalink] && !params[:permalink].nil? ? "current" : nil
  36. location = dropdown.webpages.map(&:name).include?(dropdown.name) ? permalink : "#{@permalink}#"
  37. if (dropdown.access_level != 0 && admin?) || dropdown.access_level == 0
  38. submenus = dropdown.webpages.sort{|a,b| a[:position] <=> b[:position]}
  39. content_tag :li, (content_tag :a, dropdown.name, :href => redirect.nil? ? "/#{location}" : redirect, :class => menu_bar_class) + white_space +
  40.  
  41. sub_menus = dropdown.webpages.map(&:name).length == 1 && dropdown.webpages.first.name.downcase == dropdown.name.downcase ? "" : (content_tag :ul do
  42. submenus.map do |webpage|
  43. dropdown_class = webpage.permalink == params[:permalink] ? "current" : nil
  44. (content_tag :li, (content_tag :a, webpage.name, :href => webpage.redirect.nil? ? "/#{webpage.permalink}" : webpage.redirect, :class => dropdown_class) unless dropdowns.map(&:name).include?(webpage.name) || !webpage.visible )
  45. end.join
  46. end)
  47. end
  48. end
  49. end
  50. end
  51. ## my new and improved refactored code
  52.  
  53. def new_build_menu_bar
  54. dropdowns = Dropdown.all(:order => 'position ASC')
  55.  
  56. content_tag :ul, :id => "nav" do
  57. dropdowns.map do |d|
  58. content_tag :li, d.webpages.length == 0 ? make_link(d) : make_link(d) + make_webpage_links(d) if view_conditions(d)
  59. end
  60. end
  61. end
  62.  
  63. def make_webpage_links(d)
  64. webpage_links = d.webpages.map { |w| content_tag :li, make_link(w) if view_conditions(w, d) }
  65. webpage_links.compact.blank? ? "" : (content_tag :ul, webpage_links)
  66. end
  67.  
  68. def view_conditions(obj, obj2 = nil)
  69. if obj2.nil? || obj2.name != obj.name
  70. (obj.access_level != 0 && admin?) || obj.access_level == 0
  71. end
  72. end
  73.  
  74. def make_link(obj)
  75. content_tag :a, obj.name, :href => href(obj), :class => is_selected?(obj.name, @permalink)
  76. end
  77.  
  78. def href(obj)
  79. if obj.redirect
  80. obj.redirect
  81. else
  82. if obj.class.name == "Dropdown"
  83. href_for_dropdown(obj)
  84. else
  85. "/#{obj.permalink}"
  86. end
  87. end
  88. end
  89.  
  90. def href_for_dropdown(obj)
  91. obj.webpages.map(&:name).include?(obj.name) ? "/#{obj.webpages.find_by_name(obj.name).permalink}" : "#"
  92. end
  93.  
  94. def is_selected?(obj_name, permalink)
  95. compare_name = permalink.nil? ? controller.controller_name : permalink.downcase
  96. obj_name.downcase == compare_name ? "current" : nil
  97. end
Add Comment
Please, Sign In to add comment