Module: Spree::Admin::NavigationHelper

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

Instance Method Summary collapse

Instance Method Details

#admin_breadcrumb(*ancestors, &block) ⇒ Object

Add items to current page breadcrumb hierarchy



11
12
13
14
# File 'app/helpers/spree/admin/navigation_helper.rb', line 11

def admin_breadcrumb(*ancestors, &block)
  admin_breadcrumbs.concat(ancestors) if ancestors.present?
  admin_breadcrumbs.push(capture(&block)) if block_given?
end

#admin_breadcrumbsObject



6
7
8
# File 'app/helpers/spree/admin/navigation_helper.rb', line 6

def admin_breadcrumbs
  @admin_breadcrumbs ||= []
end

#admin_page_titleObject



30
31
32
33
34
35
36
37
38
39
40
# File 'app/helpers/spree/admin/navigation_helper.rb', line 30

def admin_page_title
  if content_for?(:title)
    content_for(:title)
  elsif content_for?(:page_title)
    content_for(:page_title)
  elsif admin_breadcrumbs.any?
    admin_breadcrumbs.map { |breadcrumb| strip_tags(breadcrumb) }.reverse.join(' - ')
  else
    t(controller.controller_name, default: controller.controller_name.titleize, scope: 'spree')
  end
end

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



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

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



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

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


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

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


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

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


97
98
99
100
101
# File 'app/helpers/spree/admin/navigation_helper.rb', line 97

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


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

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


92
93
94
95
# File 'app/helpers/spree/admin/navigation_helper.rb', line 92

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


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

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

#render_admin_breadcrumbsObject

Render Bootstrap style breadcrumbs



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

def render_admin_breadcrumbs
  if content_for?(:page_title)
    admin_breadcrumb(content_for(:page_title))
  end

   :ol, class: 'breadcrumb' do
    segments = admin_breadcrumbs.map do |level|
      (:li, level, class: "breadcrumb-item #{level == admin_breadcrumbs.last ? 'active' : ''}")
    end
    safe_join(segments)
  end
end

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



148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/helpers/spree/admin/navigation_helper.rb', line 148

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[:data] ||= {}
  options[:data][:hook] = "admin_settings_#{link_text.downcase.tr(' ', '_')}"
  (:li, options) do
    link_to(link_text, url)
  end
end

#solidus_icon(icon_name) ⇒ Object



126
127
128
# File 'app/helpers/spree/admin/navigation_helper.rb', line 126

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

#tab(*args, &_block) ⇒ Object

Make an admin tab that covers 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.
* :match_path can also be a callable that takes a request and determines whether the menu item is selected for the request.


48
49
50
51
52
53
54
55
56
57
58
59
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
# File 'app/helpers/spree/admin/navigation_helper.rb', line 48

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 = t(options[:label], scope: [:spree, :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].respond_to?(:call)
    options[:match_path].call(request)
  elsif options[:match_path]
    request.fullpath.starts_with?("#{spree.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