From fea32b820140d09e0b5d2056a68d4b2b3f307249 Mon Sep 17 00:00:00 2001 From: jacobherrington Date: Sun, 11 Nov 2018 12:55:30 -0600 Subject: [PATCH 1/3] Add ordering to menu items This change allows menu items to be ordered. If the order value is nil the menu item will come at the end of the list. --- backend/app/models/spree/backend_configuration.rb | 15 ++++++++++++--- .../app/views/spree/admin/shared/_tabs.html.erb | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/backend/app/models/spree/backend_configuration.rb b/backend/app/models/spree/backend_configuration.rb index 831a941f4d5..9ba1636a921 100644 --- a/backend/app/models/spree/backend_configuration.rb +++ b/backend/app/models/spree/backend_configuration.rb @@ -22,7 +22,7 @@ class BackendConfiguration < Preferences::Configuration # An item which should be drawn in the admin menu class MenuItem - attr_reader :icon, :label, :partial, :condition, :sections, :url + attr_reader :icon, :label, :partial, :condition, :sections, :url, :position # @param sections [Array] The sections which are contained within # this admin menu section. @@ -35,13 +35,16 @@ class MenuItem # @param partial [String] A partial to draw within this menu item for use # in declaring a submenu # @param url [String] A url where this link should send the user to + # @param position [Integer] The position in which the menu item should render + # nil will cause the item to render last def initialize( sections, icon, condition: nil, label: nil, partial: nil, - url: nil + url: nil, + position: nil ) @condition = condition || -> { true } @@ -50,6 +53,7 @@ def initialize( @label = label || sections.first @partial = partial @url = url + @position = position end end @@ -65,6 +69,11 @@ def initialize( # # @!attribute menu_items # @return [Array] + # + # Positioning can be determined by setting the position attribute to + # an Integer or nil. Menu Items will be rendered with smaller lower values + # first and higher values last. A position value of nil will cause the menu + # item to be rendered at the end of the list. attr_writer :menu_items # Return the menu items which should be drawn in the menu @@ -76,7 +85,7 @@ def menu_items MenuItem.new( ORDER_TABS, 'shopping-cart', - condition: -> { can?(:admin, Spree::Order) }, + condition: -> { can?(:admin, Spree::Order) } ), MenuItem.new( PRODUCT_TABS, diff --git a/backend/app/views/spree/admin/shared/_tabs.html.erb b/backend/app/views/spree/admin/shared/_tabs.html.erb index ec7604d9f00..7be1b6e943f 100644 --- a/backend/app/views/spree/admin/shared/_tabs.html.erb +++ b/backend/app/views/spree/admin/shared/_tabs.html.erb @@ -1,4 +1,4 @@ -<% Spree::Backend::Config.menu_items.each do |menu_item| %> +<% Spree::Backend::Config.menu_items.sort_by { |item| item.position || Float::INFINITY }.each do |menu_item| %> <% if instance_exec(&menu_item.condition) %> <%= tab( From a8d10f91b331ae63a458aa22ac66c3ed4ecd44b2 Mon Sep 17 00:00:00 2001 From: jacobherrington Date: Sun, 11 Nov 2018 12:57:03 -0600 Subject: [PATCH 2/3] Reorder menu items --- .../app/models/spree/backend_configuration.rb | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/backend/app/models/spree/backend_configuration.rb b/backend/app/models/spree/backend_configuration.rb index 9ba1636a921..8aad456ddf4 100644 --- a/backend/app/models/spree/backend_configuration.rb +++ b/backend/app/models/spree/backend_configuration.rb @@ -85,41 +85,47 @@ def menu_items MenuItem.new( ORDER_TABS, 'shopping-cart', - condition: -> { can?(:admin, Spree::Order) } + condition: -> { can?(:admin, Spree::Order) }, + position: 0 ), MenuItem.new( PRODUCT_TABS, 'th-large', condition: -> { can?(:admin, Spree::Product) }, - partial: 'spree/admin/shared/product_sub_menu' - ), - MenuItem.new( - CONFIGURATION_TABS, - 'wrench', - condition: -> { can?(:admin, Spree::Store) }, - label: :settings, - partial: 'spree/admin/shared/settings_sub_menu', - url: :admin_stores_path + partial: 'spree/admin/shared/product_sub_menu', + position: 1 ), MenuItem.new( PROMOTION_TABS, 'bullhorn', partial: 'spree/admin/shared/promotion_sub_menu', condition: -> { can?(:admin, Spree::Promotion) }, - url: :admin_promotions_path + url: :admin_promotions_path, + position: 2 ), MenuItem.new( STOCK_TABS, 'cubes', condition: -> { can?(:admin, Spree::StockItem) }, label: :stock, - url: :admin_stock_items_path + url: :admin_stock_items_path, + position: 3 ), MenuItem.new( USER_TABS, 'user', condition: -> { Spree.user_class && can?(:admin, Spree.user_class) }, - url: :admin_users_path + url: :admin_users_path, + position: 4 + ), + MenuItem.new( + CONFIGURATION_TABS, + 'wrench', + condition: -> { can?(:admin, Spree::Store) }, + label: :settings, + partial: 'spree/admin/shared/settings_sub_menu', + url: :admin_stores_path, + position: 5 ) ] end From b6540bf685e53fec86a924bc207f9d7d7c9b0db9 Mon Sep 17 00:00:00 2001 From: jacobherrington Date: Sun, 11 Nov 2018 14:08:13 -0600 Subject: [PATCH 3/3] Add attr_accessor for position --- backend/app/models/spree/backend_configuration.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/app/models/spree/backend_configuration.rb b/backend/app/models/spree/backend_configuration.rb index 8aad456ddf4..f6ba1d8d6fd 100644 --- a/backend/app/models/spree/backend_configuration.rb +++ b/backend/app/models/spree/backend_configuration.rb @@ -22,7 +22,9 @@ class BackendConfiguration < Preferences::Configuration # An item which should be drawn in the admin menu class MenuItem - attr_reader :icon, :label, :partial, :condition, :sections, :url, :position + attr_reader :icon, :label, :partial, :condition, :sections, :url + + attr_accessor :position # @param sections [Array] The sections which are contained within # this admin menu section.