module MenusHelper def menu(uid, options = {}, &block) options.reverse_merge!({:locale => Fullstack::Cms.localized? ? I18n.locale.to_s : nil, :wrap => true}) locale = options.delete(:locale) wrap = options.delete(:wrap) wrapper_options = options.delete(:wrapper_html) || {} link_options = options.delete(:link_html) || {} menu = fetch_menu!(uid, locale) if wrap content_tag :ul, wrapper_options, false do menu_links_for(menu, link_options) end else menu_links_for(menu, link_options) end end def menu_link(link, options = {}) options.reverse_merge({:rel => link.nofollow ? "nofollow" : nil}) link_to(link.label, menu_link_url(link), options) end def menu_link_url(link) return "#" unless link link.url.present? ? link.url : (link.linked ? page_path_for(link.linked) : "#") end # ====================== # = Deprecated Methods = # ====================== def link_url(link) ActiveSupport::Deprecation.warn("This method is deprecated use 'menu_link_url(link)' instead") menu_link_url(link) end def find_or_create_menu(uid, locale = nil) ActiveSupport::Deprecation.warn("This method is deprecated use 'menu(uid, :locale => locale_name)' instead") if locale Menu.find_or_create_by_uid_and_locale(uid, locale.to_s, :name => "#{uid}".gsub("-", "_").humanize) else Menu.find_or_create_by_uid(uid, :name => "#{uid}".gsub("-", "_").humanize) end end def nav_items_for(uid, locale = nil) ActiveSupport::Deprecation.warn("This method is deprecated use 'menu(uid, :locale => locale_name, :wrap => false)' instead") menu = find_or_create_menu(uid, locale) html = "" menu.links.each do |link| unless block_given? html << nav_item(link.label, link_url(link), :rel => link.nofollow ? "nofollow" : nil) else html << yield(link) end end html.html_safe end def render_menu(uid, options = nil, &block) ActiveSupport::Deprecation.warn("This method is deprecated use 'menu(uid, :locale => locale_name)' instead") content_tag :ul, nav_items_for(uid, &block), options, false end # =================== # = Private Methods = # =================== def menu_links_for(menu, link_options = {}) html = "" menu.links.each do |link| unless block_given? html << content_tag(:li, menu_link(link, link_options)) else html << yield(link) end end html.html_safe end def fetch_menu!(uid, locale) query = Menu.where(:uid => uid.to_s) if locale query = query.where(:locale => locale.to_s) end menu = query.first raise "Menu '#{uid}' not found for locale '#{locale}'" if menu.nil? menu end end