Module: Spree::Admin::NavigationHelper
- Defined in:
- app/helpers/spree/admin/navigation_helper.rb
Overview
rubocop:disable Metrics/ModuleLength
Constant Summary collapse
- ICON_SIZE =
Makes an admin navigation tab (
- 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 resource 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 - 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 resource to link (uses URL helpers).
14- MENU_ICON_SIZE =
18
Instance Method Summary collapse
- #active_badge(condition, options = {}) ⇒ Object
- #adjustments_actions ⇒ Object
-
#button(text, icon_name = nil, button_type = 'submit', options = {}) ⇒ Object
Override: Add disable_with option to prevent multiple request on consecutive clicks.
- #button_link_to(text, url, html_options = {}) ⇒ Object
- #configurations_sidebar_menu_item(link_text, url, options = {}) ⇒ Object
- #images_actions ⇒ Object
-
#klass_for(name) ⇒ Object
finds class for a given symbol / string.
- #link_to_clone(resource, options = {}) ⇒ Object
- #link_to_clone_promotion(promotion, options = {}) ⇒ Object
- #link_to_delete(resource, options = {}) ⇒ Object
- #link_to_edit(resource, options = {}) ⇒ Object
- #link_to_edit_url(url, options = {}) ⇒ Object
- #link_to_with_icon(icon_name, text, url, options = {}) ⇒ Object
- #main_menu ⇒ Object
-
#main_menu_item(text, url: nil, icon: nil) ⇒ Object
Single main menu item.
- #main_part_classes ⇒ Object
- #main_sidebar_classes ⇒ Object
- #order_actions ⇒ Object
- #order_tabs ⇒ Object
- #orders_actions ⇒ Object
- #page_header_back_button(url) ⇒ Object
- #payments_actions ⇒ Object
-
#per_page_dropdown ⇒ Object
the per_page_dropdown is used on index pages like orders, products, promotions etc.
-
#per_page_dropdown_params(args = nil) ⇒ Object
helper method to create proper url to apply per page ing fixes https://github.com/spree/spree/issues/6888.
- #prices_actions ⇒ Object
- #product_actions ⇒ Object
- #product_properties_actions ⇒ Object
- #product_tabs ⇒ Object
- #products_actions ⇒ Object
- #spree_icon(icon_name) ⇒ Object
- #stock_actions ⇒ Object
- #store_credits_actions ⇒ Object
- #tab(*args) ⇒ Object
- #user_actions ⇒ Object
- #user_tabs ⇒ Object
- #users_actions ⇒ Object
- #variants_actions ⇒ Object
- #wrapper_classes ⇒ Object
Instance Method Details
#active_badge(condition, options = {}) ⇒ Object
264 265 266 267 268 269 270 271 272 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 264 def active_badge(condition, = {}) label = [:label] label ||= condition ? Spree.t(:say_yes) : Spree.t(:say_no) css_class = condition ? 'badge-active' : 'badge-inactive' content_tag(:small, class: "badge badge-pill #{css_class}") do label end end |
#adjustments_actions ⇒ Object
361 362 363 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 361 def adjustments_actions Rails.application.config.spree_backend.actions[:adjustments] end |
#button(text, icon_name = nil, button_type = 'submit', options = {}) ⇒ Object
Override: Add disable_with option to prevent multiple request on consecutive clicks
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 198 def (text, icon_name = nil, = 'submit', = {}) if icon_name icon = if icon_name.ends_with?('.svg') svg_icon(name: icon_name, classes: "icon icon-#{icon_name}", width: ICON_SIZE, height: ICON_SIZE) else content_tag(:span, '', class: "icon icon-#{icon_name}") end text = "#{icon} #{text}" end css_classes = [:class] || 'btn-primary' ( text.html_safe, .merge( type: , class: "btn #{css_classes}", 'data-disable-with' => "#{Spree.t(:saving)}..." ) ) end |
#button_link_to(text, url, html_options = {}) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 219 def (text, url, = {}) if [:method] && ![:method].to_s.casecmp('get').zero? && ![:remote] [:class] = [:class] ? "btn #{[:class]}" : 'btn btn-primary' form_tag(url, method: .delete(:method)) do (text, .delete(:icon), nil, ) end else if ['data-update'].nil? && [:remote] object_name, action = url.split('/')[-2..-1] ['data-update'] = [action, object_name.singularize].join('_') end .delete('data-update') unless ['data-update'] [:class] = [:class] ? "btn #{[:class]}" : 'btn btn-light' if [:icon] icon = if [:icon].ends_with?('.svg') svg_icon(name: [:icon], classes: "icon icon-#{[:icon]}", width: ICON_SIZE, height: ICON_SIZE) else content_tag(:span, '', class: "icon icon-#{[:icon]}") end text = "#{icon} #{text}" end link_to(text.html_safe, url, .except(:icon)) end end |
#configurations_sidebar_menu_item(link_text, url, options = {}) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 252 def (link_text, url, = {}) [:is_selected] ||= url.ends_with?(controller.controller_name) || url.ends_with?("#{controller.controller_name}/edit") || url.ends_with?("#{controller.controller_name.singularize}/edit") [:class] = 'sidebar-menu-item d-block w-100' [:class] << ' selected font-weight-bold' if [:is_selected] content_tag(:li, ) do link_to(link_text, url, class: "#{'text-muted' unless [:is_selected]} sidebar-submenu-item w-100 py-2 py-md-1 pl-5 d-block") end end |
#images_actions ⇒ Object
349 350 351 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 349 def images_actions Rails.application.config.spree_backend.actions[:images] 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
128 129 130 131 132 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 128 def klass_for(name) model_name = name.to_s ["Spree::#{model_name.classify}", model_name.classify, model_name.tr('_', '/').classify].find(&:safe_constantize).try(:safe_constantize) end |
#link_to_clone(resource, options = {}) ⇒ Object
134 135 136 137 138 139 140 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 134 def link_to_clone(resource, = {}) [:data] = { action: 'clone', 'original-title': Spree.t(:clone) } [:class] = 'btn btn-light btn-sm with-tip icon-link' [:method] = :post [:icon] = 'clone.svg' '', clone_object_url(resource), end |
#link_to_clone_promotion(promotion, options = {}) ⇒ Object
142 143 144 145 146 147 148 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 142 def link_to_clone_promotion(promotion, = {}) [:data] = { action: 'clone', 'original-title': Spree.t(:clone) } [:class] = 'btn btn-light btn-sm with-tip' [:method] = :post [:icon] = 'clone.svg' '', clone_admin_promotion_path(promotion), end |
#link_to_delete(resource, options = {}) ⇒ Object
163 164 165 166 167 168 169 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 163 def link_to_delete(resource, = {}) url = [:url] || object_url(resource) name = [:name] || Spree.t(:delete) [:class] = 'btn btn-danger btn-sm delete-resource' [:data] = { confirm: Spree.t(:are_you_sure), action: 'remove' } link_to_with_icon 'delete.svg', name, url, end |
#link_to_edit(resource, options = {}) ⇒ Object
150 151 152 153 154 155 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 150 def link_to_edit(resource, = {}) url = [:url] || edit_object_url(resource) [:data] = { action: 'edit' } [:class] = 'btn btn-light btn-sm' link_to_with_icon('edit.svg', Spree.t(:edit), url, ) end |
#link_to_edit_url(url, options = {}) ⇒ Object
157 158 159 160 161 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 157 def link_to_edit_url(url, = {}) [:data] = { action: 'edit' } [:class] = 'btn btn-light btn-sm' link_to_with_icon('edit.svg', Spree.t(:edit), url, ) end |
#link_to_with_icon(icon_name, text, url, options = {}) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 171 def link_to_with_icon(icon_name, text, url, = {}) [:class] = ([:class].to_s + " icon-link with-tip action-#{icon_name}").strip [:title] = text if [:no_text] text = [:no_text] ? '' : content_tag(:span, text) .delete(:no_text) [:width] ||= ICON_SIZE [:height] ||= ICON_SIZE if icon_name icon = if icon_name.ends_with?('.svg') svg_icon(name: icon_name, classes: "#{'mr-2' unless text.empty?} icon icon-#{icon_name}", width: [:width], height: [:height]) else content_tag(:span, '', class: "#{'mr-2' unless text.empty?} icon icon-#{icon_name}") end text = "#{icon} #{text}" end link_to(text.html_safe, url, ) end |
#main_menu ⇒ Object
309 310 311 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 309 def Rails.application.config.spree_backend. end |
#main_menu_item(text, url: nil, icon: nil) ⇒ Object
Single main menu item
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 76 def (text, url: nil, icon: nil) link_to url, 'data-toggle': 'collapse', class: 'd-flex w-100 px-3 py-2 position-relative align-items-center' do if icon.ends_with?('.svg') svg_icon(name: icon, classes: 'mr-2 text-muted', width: MENU_ICON_SIZE, height: MENU_ICON_SIZE) + content_tag(:span, raw(" #{text}"), class: 'text-muted') + svg_icon(name: 'chevron-right.svg', classes: 'drop-menu-indicator text-muted position-absolute', width: (MENU_ICON_SIZE - 8), height: (MENU_ICON_SIZE - 8)) else content_tag(:span, nil, class: "icon text-muted icon-#{icon} mr-2") + content_tag(:span, raw(" #{text}"), class: 'text-muted') + svg_icon(name: 'chevron-right.svg', classes: 'drop-menu-indicator text-muted position-absolute', width: (MENU_ICON_SIZE - 8), height: (MENU_ICON_SIZE - 8)) end end end |
#main_part_classes ⇒ Object
274 275 276 277 278 279 280 281 282 283 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 274 def main_part_classes Spree::Deprecation.warn(<<-DEPRECATION, caller) Admin::NavigationHelper#main_part_classes is deprecated and will be removed in Spree 5.0. DEPRECATION if ['sidebar-minimized'] == 'true' 'col-12 sidebar-collapsed' else 'col-9 offset-3 col-md-10 offset-md-2' end end |
#main_sidebar_classes ⇒ Object
285 286 287 288 289 290 291 292 293 294 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 285 def Spree::Deprecation.warn(<<-DEPRECATION, caller) Admin::NavigationHelper#main_sidebar_classes is deprecated and will be removed in Spree 5.0. DEPRECATION if ['sidebar-minimized'] == 'true' 'col-3 col-md-2 sidebar' else 'p-0 col-3 col-md-2 sidebar' end end |
#order_actions ⇒ Object
329 330 331 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 329 def order_actions Rails.application.config.spree_backend.actions[:order] end |
#order_tabs ⇒ Object
313 314 315 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 313 def order_tabs Rails.application.config.spree_backend.tabs[:order] end |
#orders_actions ⇒ Object
325 326 327 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 325 def orders_actions Rails.application.config.spree_backend.actions[:orders] end |
#page_header_back_button(url) ⇒ Object
303 304 305 306 307 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 303 def (url) link_to url, class: 'btn btn-light mr-3 pr-1' do svg_icon name: 'chevron-left.svg', width: 15, height: 15 end end |
#payments_actions ⇒ Object
365 366 367 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 365 def payments_actions Rails.application.config.spree_backend.actions[:payments] end |
#per_page_dropdown ⇒ Object
the per_page_dropdown is used on index pages like orders, products, promotions etc. this method generates the select_tag
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 92 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::Backend::Config.admin_products_per_page = [] 5.times do |amount| << (amount + 1) * Spree::Backend::Config.admin_products_per_page end else per_page_default = Spree::Backend::Config.admin_orders_per_page = %w{25 50 75} end selected_option = params[:per_page].try(:to_i) || per_page_default select_tag(:per_page, (, selected_option), class: "w-auto form-control js-per-page-select per-page-selected-#{selected_option} custom-select custom-select-sm") end |
#per_page_dropdown_params(args = nil) ⇒ Object
helper method to create proper url to apply per page ing fixes https://github.com/spree/spree/issues/6888
113 114 115 116 117 118 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 113 def per_page_dropdown_params(args = nil) args = params.permit!.to_h.clone args.delete(:page) args.delete(:per_page) args end |
#prices_actions ⇒ Object
353 354 355 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 353 def prices_actions Rails.application.config.spree_backend.actions[:prices] end |
#product_actions ⇒ Object
345 346 347 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 345 def product_actions Rails.application.config.spree_backend.actions[:product] end |
#product_properties_actions ⇒ Object
377 378 379 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 377 def product_properties_actions Rails.application.config.spree_backend.actions[:product_properties] end |
#product_tabs ⇒ Object
321 322 323 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 321 def product_tabs Rails.application.config.spree_backend.tabs[:product] end |
#products_actions ⇒ Object
341 342 343 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 341 def products_actions Rails.application.config.spree_backend.actions[:products] end |
#spree_icon(icon_name) ⇒ Object
189 190 191 192 193 194 195 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 189 def spree_icon(icon_name) if icon_name.ends_with?('.svg') icon_name ? svg_icon(name: icon_name, classes: icon_name, width: MENU_ICON_SIZE, height: MENU_ICON_SIZE) : '' else icon_name ? content_tag(:span, '', class: icon_name) : '' end end |
#stock_actions ⇒ Object
369 370 371 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 369 def stock_actions Rails.application.config.spree_backend.actions[:stock] end |
#store_credits_actions ⇒ Object
357 358 359 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 357 def store_credits_actions Rails.application.config.spree_backend.actions[:store_credits] end |
#tab(*args) ⇒ Object
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 23 def tab(*args) = { label: args.first.to_s } # Return if resource is found and user is not allowed to :admin return '' if (klass = klass_for(args.first.to_s)) && cannot?(:admin, klass) = .merge(args.pop) if args.last.is_a?(Hash) [:route] ||= "admin_#{args.first}" destination_url = [:url] || spree.send("#{[:route]}_path") if [:do_not_titleize] == true titleized_label = [:label] else titleized_label = Spree.t([:label], default: [:label], scope: [:admin, :tab]).titleize end css_classes = ['sidebar-menu-item d-block w-100 position-relative'] if (selected = [:selected]).nil? selected = if [:match_path].is_a? Regexp request.fullpath =~ [:match_path] elsif [:match_path] request.fullpath.starts_with?("#{spree.admin_path}#{[:match_path]}") else args.include?(controller.controller_name.to_sym) end end link = if [:icon] link_to_with_icon( [:icon], titleized_label, destination_url, class: 'w-100 px-3 py-2 d-flex align-items-center text-muted', width: MENU_ICON_SIZE, height: MENU_ICON_SIZE ) else link_to( titleized_label, destination_url, class: "sidebar-submenu-item w-100 py-2 py-md-1 pl-5 d-block #{selected ? 'font-weight-bold' : 'text-muted'}" ) end css_classes << 'selected' if selected css_classes << [:css_class] if [:css_class] content_tag('li', link, class: css_classes.join(' ')) end |
#user_actions ⇒ Object
337 338 339 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 337 def user_actions Rails.application.config.spree_backend.actions[:user] end |
#user_tabs ⇒ Object
317 318 319 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 317 def user_tabs Rails.application.config.spree_backend.tabs[:user] end |
#users_actions ⇒ Object
333 334 335 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 333 def users_actions Rails.application.config.spree_backend.actions[:users] end |
#variants_actions ⇒ Object
373 374 375 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 373 def variants_actions Rails.application.config.spree_backend.actions[:variants] end |
#wrapper_classes ⇒ Object
296 297 298 299 300 301 |
# File 'app/helpers/spree/admin/navigation_helper.rb', line 296 def wrapper_classes Spree::Deprecation.warn(<<-DEPRECATION, caller) Admin::NavigationHelper#wrapper_classes is deprecated and will be removed in Spree 5.0. DEPRECATION 'sidebar-minimized' if ['sidebar-minimized'] == 'true' end |