Module: Spree::Admin::NavigationHelper

Defined in:
app/helpers/spree/admin/navigation_helper.rb

Instance Method Summary collapse

Instance Method Details

#button(text, icon_name = nil, button_type = 'submit', options = {}) ⇒ Object



179
180
181
182
183
184
185
# File 'app/helpers/spree/admin/navigation_helper.rb', line 179

def button(text, icon_name = nil, button_type = 'submit', options={})
  if icon_name
    icon = (:span, '', class: "icon icon-#{icon_name}")
    text.insert(0, icon + ' ')
  end
  button_tag(text.html_safe, options.merge(type: button_type, class: "btn btn-primary #{options[:class]}"))
end


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'app/helpers/spree/admin/navigation_helper.rb', line 187

def button_link_to(text, url, html_options = {})
  if (html_options[:method] &&
      html_options[:method].to_s.downcase != 'get' &&
      !html_options[:remote])
    form_tag(url, method: html_options.delete(:method), class: 'display-inline') do
      button(text, html_options.delete(:icon), nil, html_options)
    end
  else
    if html_options['data-update'].nil? && html_options[:remote]
      object_name, action = url.split('/')[-2..-1]
      html_options['data-update'] = [action, object_name.singularize].join('_')
    end

    html_options.delete('data-update') unless html_options['data-update']

    html_options[:class]  = html_options[:class] ? "btn #{html_options[:class]}" : "btn btn-default"

    if html_options[:icon]
      icon = (:span, '', class: "icon icon-#{html_options[:icon]}")
      text.insert(0, icon + ' ')
    end

    link_to(text.html_safe, url, html_options)
  end
end

sidebar are used on order edit, product edit, user overview etc. this link is shown so a user can collapse the sidebar



74
75
76
77
78
79
80
81
# File 'app/helpers/spree/admin/navigation_helper.rb', line 74

def collapse_sidebar_link
   :div, class: "collapse-sidebar" do
    link_to "javascript:;", class: "js-collapse-sidebar" do
      (:span, nil, class: "icon icon-chevron-right") +
      (:span, "Collapse sidebar", class: "text")
    end
  end
end

#configurations_menu_item(link_text, url, description = '') ⇒ Object



213
214
215
216
217
218
219
# File 'app/helpers/spree/admin/navigation_helper.rb', line 213

def configurations_menu_item(link_text, url, description = '')
  %(<tr>
    <td>#{link_to(link_text, url)}</td>
    <td>#{description}</td>
  </tr>
  ).html_safe
end

#configurations_sidebar_menu_item(link_text, url, options = {}) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
# File 'app/helpers/spree/admin/navigation_helper.rb', line 221

def configurations_sidebar_menu_item(link_text, url, options = {})
  is_selected = url.ends_with?(controller.controller_name) ||
                url.ends_with?("#{controller.controller_name}/edit") ||
                url.ends_with?("#{controller.controller_name.singularize}/edit")

  options[:class] = 'sidebar-menu-item'
  options[:class] << ' selected' if is_selected
  (:li, options) do
    link_to(link_text, url)
  end
end

#icon(icon_name) ⇒ Object



175
176
177
# File 'app/helpers/spree/admin/navigation_helper.rb', line 175

def icon(icon_name)
  icon_name ? (:i, '', class: icon_name) : ''
end

#klass_for(name) ⇒ Object

finds class for a given symbol / string

Example : :products returns Spree::Product :my_products returns MyProduct if MyProduct is defined :my_products returns My::Product if My::Product is defined if cannot constantize it returns nil This will allow us to use cancan abilities on tab



119
120
121
122
123
124
125
# File 'app/helpers/spree/admin/navigation_helper.rb', line 119

def klass_for(name)
  model_name = name.to_s

  ["Spree::#{model_name.classify}", model_name.classify, model_name.gsub('_', '/').classify].find do |t|
    t.safe_constantize
  end.try(:safe_constantize)
end


127
128
129
130
131
132
133
# File 'app/helpers/spree/admin/navigation_helper.rb', line 127

def link_to_clone(resource, options={})
  options[:data] = { action: 'clone', :'original-title' => Spree.t(:clone) }
  options[:class] = "btn btn-primary btn-sm with-tip"
  options[:method] = :post
  options[:icon] = :clone
  button_link_to '', clone_object_url(resource), options
end


154
155
156
157
158
159
160
# File 'app/helpers/spree/admin/navigation_helper.rb', line 154

def link_to_delete(resource, options={})
  url = options[:url] || object_url(resource)
  name = options[:name] || Spree.t(:delete)
  options[:class] = "btn btn-danger btn-sm delete-resource"
  options[:data] = { confirm: Spree.t(:are_you_sure), action: 'remove' }
  link_to_with_icon 'delete', name, url, options
end


141
142
143
144
145
146
# File 'app/helpers/spree/admin/navigation_helper.rb', line 141

def link_to_edit(resource, options={})
  url = options[:url] || edit_object_url(resource)
  options[:data] = { action: 'edit' }
  options[:class] = "btn btn-primary btn-sm"
  link_to_with_icon('edit', Spree.t(:edit), url, options)
end


148
149
150
151
152
# File 'app/helpers/spree/admin/navigation_helper.rb', line 148

def link_to_edit_url(url, options={})
  options[:data] = { action: 'edit' }
  options[:class] = "btn btn-primary btn-sm"
  link_to_with_icon('edit', Spree.t(:edit), url, options)
end


135
136
137
138
139
# File 'app/helpers/spree/admin/navigation_helper.rb', line 135

def link_to_new(resource)
  options[:data] = { action: 'new' }
  options[:class] = "btn btn-success btn-sm"
  link_to_with_icon('plus', Spree.t(:new), edit_object_url(resource))
end


162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/helpers/spree/admin/navigation_helper.rb', line 162

def link_to_with_icon(icon_name, text, url, options = {})
  options[:class] = (options[:class].to_s + " icon-link with-tip action-#{icon_name}").strip
  options[:class] += ' no-text' if options[:no_text]
  options[:title] = text if options[:no_text]
  text = options[:no_text] ? '' : (:span, text, class: 'text')
  options.delete(:no_text)
  if icon_name
    icon = (:span, '', class: "icon icon-#{icon_name}")
    text.insert(0, icon + ' ')
  end
  link_to(text.html_safe, url, options)
end

Single main menu item



56
57
58
59
60
61
62
# File 'app/helpers/spree/admin/navigation_helper.rb', line 56

def main_menu_item text, url: nil, icon: nil
  link_to url, :'data-toggle' => "collapse", :'data-parent' => '#sidebar' do
    (:span, nil, class: "icon icon-#{icon}") +
    (:span, " #{text}", class: 'text') +
    (:span, nil, class: "icon icon-chevron-left pull-right")
  end
end

Main menu tree menu



65
66
67
68
69
70
# File 'app/helpers/spree/admin/navigation_helper.rb', line 65

def main_menu_tree text, icon: nil, sub_menu: nil, url: '#'
   :li, class: 'sidebar-menu-item' do
    main_menu_item(text, url: url, icon: icon) +
    render(partial: "spree/admin/shared/sub_menu/#{sub_menu}")
  end
end

#main_part_classesObject



233
234
235
236
237
238
239
# File 'app/helpers/spree/admin/navigation_helper.rb', line 233

def main_part_classes
  if cookies['sidebar-minimized'] == 'true'
    return 'col-sm-12 col-md-12 sidebar-collapsed'
  else
    return 'col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2'
  end
end

#per_page_dropdownObject

the per_page_dropdown is used on index pages like orders, products, promotions etc. this method generates the select_tag



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/helpers/spree/admin/navigation_helper.rb', line 85

def per_page_dropdown
  # there is a config setting for admin_products_per_page, only for the orders page
  if @products && per_page_default = Spree::Config.admin_products_per_page
    per_page_options = []
    5.times do |amount|
      per_page_options << (amount + 1) * Spree::Config.admin_products_per_page
    end
  else
    per_page_default = 15
    per_page_options = %w{5 15 30 45 60}
  end

  select_tag(:per_page,
    options_for_select(per_page_options, params['per_page'] || per_page_default),
    { class: "form-control pull-right js-per-page-select" })
end

#per_page_dropdown_params(args = nil) ⇒ Object

helper method to create proper url to apply per page filtering fixes github.com/spree/spree/issues/6888



104
105
106
107
108
109
# File 'app/helpers/spree/admin/navigation_helper.rb', line 104

def per_page_dropdown_params(args = nil)
  args ||= params.clone
  args.delete(:page)
  args.delete(:per_page)
  args
end

#tab(*args) ⇒ Object

Makes an admin navigation tab (<li> tag) that links to a routing resource under /admin. The arguments should be a list of symbolized controller names that will cause this tab to be highlighted, with the first being the name of the resouce to link (uses URL helpers).

Option hash may follow. Valid options are

* :label to override link text, otherwise based on the first resource name (translated)
* :route to override automatically determining the default route
* :match_path as an alternative way to control when the tab is active, /products would
  match /admin/products, /admin/products/5/variants etc.  Can be a String or a Regexp.
  Controller names are ignored if :match_path is provided.

Example:

# Link to /admin/orders, also highlight tab for ProductsController and ShipmentsController
tab :orders, :products, :shipments


18
19
20
21
22
23
24
25
26
27
28
29
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/spree/admin/navigation_helper.rb', line 18

def tab(*args)
  options = { label: args.first.to_s }

  # Return if resource is found and user is not allowed to :admin
  return '' if klass = klass_for(options[:label]) and cannot?(:admin, klass)

  if args.last.is_a?(Hash)
    options = options.merge(args.pop)
  end
  options[:route] ||=  "admin_#{args.first}"

  destination_url = options[:url] || spree.send("#{options[:route]}_path")
  titleized_label = Spree.t(options[:label], default: options[:label], scope: [:admin, :tab]).titleize

  css_classes = ['sidebar-menu-item']

  if options[:icon]
    link = link_to_with_icon(options[:icon], titleized_label, destination_url)
  else
    link = link_to(titleized_label, destination_url)
  end

  selected = if options[:match_path].is_a? Regexp
    request.fullpath =~ options[:match_path]
  elsif options[:match_path]
    request.fullpath.starts_with?("#{spree.admin_path}#{options[:match_path]}")
  else
    args.include?(controller.controller_name.to_sym)
  end
  css_classes << 'selected' if selected

  if options[:css_class]
    css_classes << options[:css_class]
  end
  ('li', link, class: css_classes.join(' '))
end

#wrapper_classesObject



241
242
243
244
245
# File 'app/helpers/spree/admin/navigation_helper.rb', line 241

def wrapper_classes
  if cookies['sidebar-minimized'] == 'true'
    return 'sidebar-minimized'
  end
end