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
  • 14
    18

    Instance Method Summary collapse

    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, options = {})
      label = options[:label]
      label ||= condition ? Spree.t(:say_yes) : Spree.t(:say_no)
      css_class = condition ? 'badge-active' : 'badge-inactive'
    
      (: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 button(text, icon_name = nil, button_type = 'submit', options = {})
      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
                 (:span, '', class: "icon icon-#{icon_name}")
               end
        text = "#{icon} #{text}"
      end
    
      css_classes = options[:class] || 'btn-primary'
      button_tag(
        text.html_safe,
        options.merge(
          type: button_type,
          class: "btn #{css_classes}",
          'data-disable-with' => "#{Spree.t(:saving)}..."
        )
      )
    end
    
    
    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 button_link_to(text, url, html_options = {})
      if html_options[:method] &&
          !html_options[:method].to_s.casecmp('get').zero? &&
          !html_options[:remote]
    
        html_options[:class] = html_options[:class] ? "btn #{html_options[:class]}" : 'btn btn-primary'
    
        form_tag(url, method: html_options.delete(:method)) 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-light'
    
        if html_options[:icon]
          icon = if html_options[:icon].ends_with?('.svg')
                   svg_icon(name: html_options[:icon], classes: "icon icon-#{html_options[:icon]}", width: ICON_SIZE, height: ICON_SIZE)
                 else
                   (:span, '', class: "icon icon-#{html_options[:icon]}")
                 end
          text = "#{icon} #{text}"
        end
    
        link_to(text.html_safe, url, html_options.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 configurations_sidebar_menu_item(link_text, url, options = {})
      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 d-block w-100'
      options[:class] << ' selected font-weight-bold' if options[:is_selected]
      (:li, options) do
        link_to(link_text, url, class: "#{'text-muted' unless options[: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
    
    
    134
    135
    136
    137
    138
    139
    140
    # File 'app/helpers/spree/admin/navigation_helper.rb', line 134
    
    def link_to_clone(resource, options = {})
      options[:data] = { action: 'clone', 'original-title': Spree.t(:clone) }
      options[:class] = 'btn btn-light btn-sm with-tip icon-link'
      options[:method] = :post
      options[:icon] = 'clone.svg'
      button_link_to '', clone_object_url(resource), options
    end
    
    
    142
    143
    144
    145
    146
    147
    148
    # File 'app/helpers/spree/admin/navigation_helper.rb', line 142
    
    def link_to_clone_promotion(promotion, options = {})
      options[:data] = { action: 'clone', 'original-title': Spree.t(:clone) }
      options[:class] = 'btn btn-light btn-sm with-tip'
      options[:method] = :post
      options[:icon] = 'clone.svg'
      button_link_to '', clone_admin_promotion_path(promotion), options
    end
    
    
    163
    164
    165
    166
    167
    168
    169
    # File 'app/helpers/spree/admin/navigation_helper.rb', line 163
    
    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.svg', name, url, options
    end
    
    
    150
    151
    152
    153
    154
    155
    # File 'app/helpers/spree/admin/navigation_helper.rb', line 150
    
    def link_to_edit(resource, options = {})
      url = options[:url] || edit_object_url(resource)
      options[:data] = { action: 'edit' }
      options[:class] = 'btn btn-light btn-sm'
      link_to_with_icon('edit.svg', Spree.t(:edit), url, options)
    end
    
    
    157
    158
    159
    160
    161
    # File 'app/helpers/spree/admin/navigation_helper.rb', line 157
    
    def link_to_edit_url(url, options = {})
      options[:data] = { action: 'edit' }
      options[:class] = 'btn btn-light btn-sm'
      link_to_with_icon('edit.svg', Spree.t(:edit), url, options)
    end
    
    
    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, options = {})
      options[:class] = (options[:class].to_s + " icon-link with-tip action-#{icon_name}").strip
      options[:title] = text if options[:no_text]
      text = options[:no_text] ? '' : (:span, text)
      options.delete(:no_text)
      options[:width] ||= ICON_SIZE
      options[: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: options[:width], height: options[:height])
               else
                 (:span, '', class: "#{'mr-2' unless text.empty?} icon icon-#{icon_name}")
               end
        text = "#{icon} #{text}"
      end
      link_to(text.html_safe, url, options)
    end
    
    
    309
    310
    311
    # File 'app/helpers/spree/admin/navigation_helper.rb', line 309
    
    def main_menu
      Rails.application.config.spree_backend.main_menu
    end

    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 main_menu_item(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) +
            (: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
          (:span, nil, class: "icon text-muted icon-#{icon} mr-2") +
            (: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 cookies['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 main_sidebar_classes
      Spree::Deprecation.warn(<<-DEPRECATION, caller)
        Admin::NavigationHelper#main_sidebar_classes is deprecated and will be removed in Spree 5.0.
      DEPRECATION
      if cookies['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 page_header_back_button(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
        per_page_options = []
        5.times do |amount|
          per_page_options << (amount + 1) * Spree::Backend::Config.admin_products_per_page
        end
      else
        per_page_default = Spree::Backend::Config.admin_orders_per_page
        per_page_options = %w{25 50 75}
      end
    
      selected_option = params[:per_page].try(:to_i) || per_page_default
    
      select_tag(:per_page,
                 options_for_select(per_page_options, 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 ? (: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)
      options = { 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)
    
      options = options.merge(args.pop) if args.last.is_a?(Hash)
      options[:route] ||= "admin_#{args.first}"
    
      destination_url = options[:url] || spree.send("#{options[:route]}_path")
    
      if options[:do_not_titleize] == true
        titleized_label = options[:label]
      else
        titleized_label = Spree.t(options[:label], default: options[:label], scope: [:admin, :tab]).titleize
      end
    
      css_classes = ['sidebar-menu-item d-block w-100 position-relative']
    
      if (selected = options[:selected]).nil?
        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
      end
    
      link = if options[:icon]
               link_to_with_icon(
                 options[: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 << options[:css_class] if options[:css_class]
      ('li', link, class: css_classes.join(' '))
    end

    #user_actions ⇒ Object

    
    
    337
    338
    339
    # File 'app/helpers/spree/admin/navigation_helper.rb', line 337
    
    def 
      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 cookies['sidebar-minimized'] == 'true'
    end