Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## the result is to build a dropdown menu bar based off of a dropdown model which has_many webpages:
- <ul id="nav">
- <li>
- <a class="current" href="/home">Home</a>
- </li>
- <li>
- <a href="#">Admin</a>
- <ul>
- <li>
- <a href="/photos">Photos</a>
- </li>
- <li>
- <a href="/photos">Products</a>
- </li>
- </ul>
- </li>
- </ul>
- ## my old confusing method
- def build_menu_bar
- dropdowns = Dropdown.all(:order => 'position ASC')
- white_space = "\r\n"
- content_tag :ul, :id => "nav" do
- dropdowns.map do |dropdown|
- webpage = dropdown.webpages.find_by_name(dropdown.name)
- unless webpage.nil?
- permalink = webpage.permalink
- redirect = webpage.redirect
- else
- redirect = dropdown.redirect
- end
- menu_bar_class = permalink == params[:permalink] && !params[:permalink].nil? ? "current" : nil
- location = dropdown.webpages.map(&:name).include?(dropdown.name) ? permalink : "#{@permalink}#"
- if (dropdown.access_level != 0 && admin?) || dropdown.access_level == 0
- submenus = dropdown.webpages.sort{|a,b| a[:position] <=> b[:position]}
- content_tag :li, (content_tag :a, dropdown.name, :href => redirect.nil? ? "/#{location}" : redirect, :class => menu_bar_class) + white_space +
- sub_menus = dropdown.webpages.map(&:name).length == 1 && dropdown.webpages.first.name.downcase == dropdown.name.downcase ? "" : (content_tag :ul do
- submenus.map do |webpage|
- dropdown_class = webpage.permalink == params[:permalink] ? "current" : nil
- (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 )
- end.join
- end)
- end
- end
- end
- end
- ## my new and improved refactored code
- def new_build_menu_bar
- dropdowns = Dropdown.all(:order => 'position ASC')
- content_tag :ul, :id => "nav" do
- dropdowns.map do |d|
- content_tag :li, d.webpages.length == 0 ? make_link(d) : make_link(d) + make_webpage_links(d) if view_conditions(d)
- end
- end
- end
- def make_webpage_links(d)
- webpage_links = d.webpages.map { |w| content_tag :li, make_link(w) if view_conditions(w, d) }
- webpage_links.compact.blank? ? "" : (content_tag :ul, webpage_links)
- end
- def view_conditions(obj, obj2 = nil)
- if obj2.nil? || obj2.name != obj.name
- (obj.access_level != 0 && admin?) || obj.access_level == 0
- end
- end
- def make_link(obj)
- content_tag :a, obj.name, :href => href(obj), :class => is_selected?(obj.name, @permalink)
- end
- def href(obj)
- if obj.redirect
- obj.redirect
- else
- if obj.class.name == "Dropdown"
- href_for_dropdown(obj)
- else
- "/#{obj.permalink}"
- end
- end
- end
- def href_for_dropdown(obj)
- obj.webpages.map(&:name).include?(obj.name) ? "/#{obj.webpages.find_by_name(obj.name).permalink}" : "#"
- end
- def is_selected?(obj_name, permalink)
- compare_name = permalink.nil? ? controller.controller_name : permalink.downcase
- obj_name.downcase == compare_name ? "current" : nil
- end
Add Comment
Please, Sign In to add comment