Module: MenusHelper

Defined in:
app/helpers/menus_helper.rb

Instance Method Summary collapse

Instance Method Details

#fetch_menu!(uid, locale) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/helpers/menus_helper.rb', line 101

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

#find_or_create_menu(uid, locale = nil) ⇒ Object



54
55
56
57
58
59
60
61
# File 'app/helpers/menus_helper.rb', line 54

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

Deprecated Methods =



49
50
51
52
# File 'app/helpers/menus_helper.rb', line 49

def link_url(link)
  ActiveSupport::Deprecation.warn("This method is deprecated use 'menu_link_url(link)' instead")
  menu_link_url(link)
end


22
23
24
25
26
27
28
29
30
31
32
# File 'app/helpers/menus_helper.rb', line 22

def links(uid, options = {})
  options.reverse_merge!({:locale => Fullstack::Cms.localized? ? I18n.locale.to_s : nil })
  locale = options.delete(:locale)
  menu   = fetch_menu!(uid, locale)
  if block_given?
    yield(menu.links)
    ""
  else
    menu.links
  end
end


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/helpers/menus_helper.rb', line 3

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
     :ul, wrapper_options, false do
      menu_links_for(menu, link_options)
    end
  else
    menu_links_for(menu, link_options)
  end

end


34
35
36
37
# File 'app/helpers/menus_helper.rb', line 34

def menu_link(link, options = {})
  options.reverse_merge({:rel => link.nofollow ? "nofollow" : nil})
  link_to(link.label, menu_link_url(link), options)
end


39
40
41
42
# File 'app/helpers/menus_helper.rb', line 39

def menu_link_url(link)
  return "#" unless link
  link.url.present? ? link.url : (link.linked ? page_path_for(link.linked) : "#")
end

Private Methods =



89
90
91
92
93
94
95
96
97
98
99
# File 'app/helpers/menus_helper.rb', line 89

def menu_links_for(menu, link_options = {})
  html = ""
  menu.links.each do |link|
    unless block_given?
      html << (:li, menu_link(link, link_options))    
    else
      html << yield(link)  
    end
  end
  html.html_safe
end


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/helpers/menus_helper.rb', line 63

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

#render_menu(uid, options = nil, &block) ⇒ Object



80
81
82
83
# File 'app/helpers/menus_helper.rb', line 80

def render_menu(uid, options = nil, &block)
  ActiveSupport::Deprecation.warn("This method is deprecated use 'menu(uid, :locale => locale_name)' instead")
   :ul, nav_items_for(uid, &block), options, false
end