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



87
88
89
# File 'app/helpers/spree/admin/navigation_helper.rb', line 87

def button(text, icon_name = nil, button_type = 'submit', options = {})
  button_tag(text, options.merge(type: button_type, class: "fa fa-#{icon_name} button"))
end


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/helpers/spree/admin/navigation_helper.rb', line 91

def button_link_to(text, url, html_options = {})
  html_options = { class: '' }.merge(html_options)
  if html_options[:method] &&
     html_options[:method].to_s.downcase != 'get' &&
     !html_options[:remote]
    form_tag(url, method: html_options.delete(:method)) do
      button(text, html_options.delete(:icon), nil, html_options)
    end
  else
    html_options[:class] += ' button'

    if html_options[:icon]
      html_options[:class] += " fa fa-#{html_options[:icon]}"
    end
    link_to(text, url, html_options)
  end
end

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



109
110
111
112
113
114
115
# File 'app/helpers/spree/admin/navigation_helper.rb', line 109

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



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

def configurations_sidebar_menu_item(link_text, url, options = {})
  is_active = url.ends_with?(controller.controller_name) ||
              url.ends_with?("#{controller.controller_name}/edit") ||
              url.ends_with?("#{controller.controller_name.singularize}/edit")
  options[:class] = is_active ? 'active' : nil
  (:li, options) do
    link_to(link_text, url)
  end
end

#icon(icon_name) ⇒ Object



83
84
85
# File 'app/helpers/spree/admin/navigation_helper.rb', line 83

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


45
46
47
48
# File 'app/helpers/spree/admin/navigation_helper.rb', line 45

def link_to_clone(resource, options = {})
  options[:data] = { action: 'clone' }
  link_to_with_icon('copy', Spree.t(:clone), clone_object_url(resource), options)
end


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

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


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

def link_to_edit(resource, options = {})
  url = options[:url] || edit_object_url(resource)
  options[:data] = { action: 'edit' }
  link_to_with_icon('edit', Spree.t('actions.edit'), url, options)
end


61
62
63
64
# File 'app/helpers/spree/admin/navigation_helper.rb', line 61

def link_to_edit_url(url, options = {})
  options[:data] = { action: 'edit' }
  link_to_with_icon('edit', Spree.t('actions.edit'), url, options)
end


50
51
52
53
# File 'app/helpers/spree/admin/navigation_helper.rb', line 50

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


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

def link_to_with_icon(icon_name, text, url, options = {})
  options[:class] = (options[:class].to_s + " fa fa-#{icon_name} icon_link with-tip").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)
  link_to(text, url, options)
end

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



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

def settings_tab_item(link_text, url, options = {})
  is_active = url.ends_with?(controller.controller_name) ||
              url.ends_with?("#{controller.controller_name}/edit") ||
              url.ends_with?("#{controller.controller_name.singularize}/edit")
  options[:class] = 'fa'
  options[:class] += ' active' if is_active
  options[:datahook] = "admin_settings_#{link_text.downcase.tr(' ', '_')}"
  (:li, options) do
    link_to(link_text, url)
  end
end

#tab(*args, &_block) ⇒ Object

Make an admin tab that coveres one or more resources supplied by symbols 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.


9
10
11
12
13
14
15
16
17
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
# File 'app/helpers/spree/admin/navigation_helper.rb', line 9

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

  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")
  label = Spree.t(options[:label], scope: [:admin, :tab])

  css_classes = []

  if options[:icon]
    link = link_to_with_icon(options[:icon], label, destination_url)
    css_classes << 'tab-with-icon'
  else
    link = link_to(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?("#{admin_path}#{options[:match_path]}")
  else
    request.fullpath.starts_with?(destination_url) ||
      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 + (yield if block_given?), class: css_classes.join(' ') )
end