Module: MenusHelper

Defined in:
app/helpers/menus_helper.rb

Instance Method Summary collapse

Instance Method Details



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/helpers/menus_helper.rb', line 60

def breadcrumbs_component limit=20
  results = []
  results << (link_to icon('home', t('home')), '/', title: t('home'))
  @parent_object = nil
  if @parent_resources.present?
    @parent_resources.reverse!.each_with_index do |pr, i|
      begin
        if i > 0
          results << (link_to t("#{pr.singularize}"), {controller: pr, action: 'index', (@parent_resources[i - 1].singularize + '_id') => instance_variable_get("@#{@parent_resources[i - 1].singularize}").id})
        else
          results << (link_to t("#{pr.singularize}"), {controller: pr, action: 'index'})
        end
      rescue
        results << t("#{pr.singularize}")
      end
      pr_id = params[(pr.singularize + '_id').to_sym]
      parent_object = pr_id.present? ? pr.classify.constantize.find(pr_id) : instance_variable_get("@#{pr.singularize}")
      results << {parent_object: @parent_object, resource: pr, current: (link_to parent_object.to_s, {controller: pr, action: 'show', id: parent_object}) + (link_to ('&nbsp;<b class="caret"></b>').html_safe, '#', "data-toggle" => 'dropdown', class: 'dropdown-toggle')}
      @parent_object = parent_object
    end
  end
  if params[:id]

    if @parent_resources.present?
      results << (link_to t("#{controller_name.singularize}"), {controller: controller_name, action: 'index', (@parent_resources.last.singularize + '_id') => instance_variable_get("@#{@parent_resources.last.singularize}").id})
    else
      begin
        results << (link_to t("#{controller_name.singularize}"), {controller: controller_name, action: 'index'})
      rescue
        results << t("#{controller_name.singularize}")
      end
    end
    current_object = controller_name.classify.constantize.find(params[:id])
    results << {parent_object: @parent_object, resource: controller_name, current: (link_to current_object.to_s, {controller: controller_name, action: 'show', id: current_object}) + (link_to ('&nbsp;<b class="caret"></b>').html_safe, '#', "data-toggle" => 'dropdown', class: 'dropdown-toggle')}
  elsif !controller_name.eql?('home')
    results << t("#{controller_name.singularize}")
  end
  result = (render 'rich_table_component/component/breadcrumbs', breadcrumbs: results, limit: limit)
  @parent_resources = nil
  result
end

#construct_all_menuObject



3
4
5
6
7
8
9
10
11
12
13
# File 'app/helpers/menus_helper.rb', line 3

def construct_all_menu
  result = ""
    Menu.roots.each do |menu_root|
      if current_user.present?
        result << '<div class="component menu menu_component grid_3 alpha omega"><div class="component_content">'
        result << (construct_menu menu_root.sorted_tree)
        result << '</div></div>'
      end
    end
  sanitize result
end

#construct_menu(menus) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/helpers/menus_helper.rb', line 30

def construct_menu(menus)
  result = ""
  menus.keep_if{|menu| (menu.roles & current_user.roles).present?}
  
  if menus.length > 0
    root_depth = menus[0].depth
    curr_depth = root_depth - 1
    menus.each do |menu|
      if menu.depth > curr_depth
        result << '<ul class="parent_' + (menu.depth - root_depth).to_s + '">'
      elsif menu.depth < curr_depth
        result << '</ul></li>' * (curr_depth - menu.depth)
      else
        # same level
        result << '</li>'
      end
      result << '<li class="child_' + (menu.depth - root_depth).to_s + '">'
      result << menu_item(menu)
      curr_depth = menu.depth
    end
    result << '</li></ul>' * (curr_depth - menus[0].depth + 1)
  end
  sanitize result
end

#construct_menu_group(menu_group_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/menus_helper.rb', line 16

def construct_menu_group(menu_group_id)
  result = ""
  if(Menu.exists?(menu_group_id))
    menus = Menu.find(menu_group_id).sorted_tree
    menus.keep_if{|menu| (menu.roles & current_user.roles).present?}
    if current_user.present? && menus.present?
      result << '<div class="component menu menu_component grid_3 alpha omega"><div class="component_content">'
      result << (construct_menu menus)
      result << '</div></div>'
    end
  end
  sanitize result
end


55
56
57
# File 'app/helpers/menus_helper.rb', line 55

def menu_item(menu)
  link_to t(menu.name), (menu.url.start_with?("'") ? eval("User.send(:sanitize_sql_array, [#{menu.url}])") : menu.url), class: ('menu_item ' + (menu.url == request.original_fullpath ? 'current_menu_item' : ''))
end

#static_menu(options = {}, title_options = {}, icon_options = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/helpers/menus_helper.rb', line 104

def static_menu(options = {}, title_options = {}, icon_options = {})
  case options
  when Hash
    _menus = options[:menu]
    _title = options[:title]
    _icon = options[:icon]
  else
    _menus = options
    _title = title_options
    _icon = icon_options
  end


  result = ""
  sub_result = static_menu_li(menus: _menus, title: _title)

  unless sub_result.length < 50
    # list is not empty
    #result << '<div class="grid_3 alpha omega">'
    if _title.present?
      result << ' <li class="dropdown">'
      result << '<a href="#" class="dropdown-toggle" data-toggle="dropdown">' + _title + '<b class="caret"></b></a>'
    end
    result << sub_result
    if _title.present?
      result << ' </li>'
    end
    #result << '</div>'
  end
  result.html_safe
end

#static_menu_li(options = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/helpers/menus_helper.rb', line 136

def static_menu_li(options = {})
  menus = options[:menus]
  level = options[:level].presence || 0
  title = options[:title]
  ability = options[:ability]

  result = ""

  if menus.present? 
    result << '<ul class="dropdown-menu parent_' + level.to_s + '">' if title.present?
    menus.compact.each do |menu|
      subresult = ""
      subresult << '<li class="child_' + level.to_s + '">'

      case menu
      when Array
        subresult << (static_menu_li menus: menu, level: (level + 1)).presence || ""
      when Hash
        subresult << (link_to menu[:label], menu[:url], class: 'menu_item')
      else
        subresult << menu
      end
        
      subresult << '</li>'

      if menu.class == Hash && menu[:ability].present?
        unless can?(menu[:ability].keys.first, menu[:ability].values.first)
          subresult = ""
        end
      end


      if ability.present?
        unless can?(ability.keys.first, ability.values.first)
          subresult = ""
        end
      end

      result << subresult

    end
    result << '</ul>' if title.present?
    result
  end

end