From 6d755154c06968f1b49d63979a35fe22218a0b19 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Fri, 15 Nov 2024 21:43:31 +0100 Subject: [PATCH] Fix(Admin menus): Stable promotion menu items This makes sure that the promotion menus are always below the product menu item, in both new admin and the backend. It also changes the nomenclature to be "Promotions" for the legacy promotion system and "Promotions (new)" for the new promotion system. --- legacy_promotions/config/locales/en.yml | 8 +++- .../lib/solidus_legacy_promotions/engine.rb | 25 ++++++++++--- promotions/config/locales/en.yml | 8 +++- promotions/lib/solidus_promotions/engine.rb | 37 +++++++++++++++++-- .../backend/main_menu_spec.rb | 12 +++--- 5 files changed, 70 insertions(+), 20 deletions(-) diff --git a/legacy_promotions/config/locales/en.yml b/legacy_promotions/config/locales/en.yml index b83109c7def..906570b727c 100644 --- a/legacy_promotions/config/locales/en.yml +++ b/legacy_promotions/config/locales/en.yml @@ -1,4 +1,8 @@ en: + solidus_admin: + menu_item: + legacy_promotions: Promotions + legacy_promotion_categories: Promotion Categories spree: admin: promotion_status: @@ -23,8 +27,8 @@ en: general: General starts_at_placeholder: Immediately tab: - promotions: Legacy Promotions - promotion_categories: Legacy Promotion Categories + legacy_promotions: Promotions + legacy_promotion_categories: Promotion Categories back_to_promotion_categories_list: Back To Promotions Categories List back_to_promotions_list: Back To Promotions List base_amount: Base Amount diff --git a/legacy_promotions/lib/solidus_legacy_promotions/engine.rb b/legacy_promotions/lib/solidus_legacy_promotions/engine.rb index 4c34fc3252e..ba7fe49583e 100644 --- a/legacy_promotions/lib/solidus_legacy_promotions/engine.rb +++ b/legacy_promotions/lib/solidus_legacy_promotions/engine.rb @@ -9,7 +9,7 @@ class Engine < ::Rails::Engine initializer "solidus_legacy_promotions.add_backend_menu_item" do if SolidusSupport.backend_available? promotions_menu_item = Spree::BackendConfiguration::MenuItem.new( - label: :promotions, + label: :legacy_promotions, icon: Spree::Backend::Config.admin_updated_navbar ? "ri-megaphone-line" : "bullhorn", partial: "spree/admin/shared/promotion_sub_menu", condition: -> { can?(:admin, Spree::Promotion) }, @@ -17,11 +17,12 @@ class Engine < ::Rails::Engine data_hook: :admin_promotion_sub_tabs, children: [ Spree::BackendConfiguration::MenuItem.new( - label: :promotions, - condition: -> { can?(:admin, Spree::Promotion) } + label: :legacy_promotions, + condition: -> { can?(:admin, Spree::Promotion) }, + url: :admin_promotions_path ), Spree::BackendConfiguration::MenuItem.new( - label: :promotion_categories, + label: :legacy_promotion_categories, condition: -> { can?(:admin, Spree::PromotionCategory) }, url: -> { Spree::Core::Engine.routes.url_helpers.admin_promotion_categories_path }, ) @@ -42,10 +43,22 @@ class Engine < ::Rails::Engine if SolidusSupport.admin_available? SolidusAdmin::Config.configure do |config| config.menu_items << { - key: "promotions", + key: "legacy_promotions", route: -> { spree.admin_promotions_path }, icon: "megaphone-line", - position: 30 + position: 1.5, + children: [ + { + key: "legacy_promotions", + route: -> { spree.admin_promotions_path }, + position: 1 + }, + { + key: "legacy_promotion_categories", + route: -> { spree.admin_promotion_categories_path }, + position: 2 + } + ] } end end diff --git a/promotions/config/locales/en.yml b/promotions/config/locales/en.yml index 06d3f481825..d4b56d3ccb8 100644 --- a/promotions/config/locales/en.yml +++ b/promotions/config/locales/en.yml @@ -2,11 +2,15 @@ # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points. en: + solidus_admin: + menu_item: + promotions: Promotions (new) + promotion_categories: Promotion Categories (new) spree: admin: tab: - solidus_promotions: Promotions - solidus_promotion_categories: Promotion Categories + promotions: Promotions (new) + promotion_categories: Promotion Categories (new) hints: solidus_promotions/promotion: expires_at: This determines when the promotion expires.
If no value is specified, the promotion will never expire. diff --git a/promotions/lib/solidus_promotions/engine.rb b/promotions/lib/solidus_promotions/engine.rb index 4170581ec02..2225ee1cda6 100644 --- a/promotions/lib/solidus_promotions/engine.rb +++ b/promotions/lib/solidus_promotions/engine.rb @@ -60,30 +60,59 @@ class Engine < Rails::Engine end end + initializer "solidus_promotions.add_solidus_admin_menu_items", after: "spree.load_config_initializers" do + if SolidusSupport.admin_available? + SolidusAdmin::Config.configure do |config| + config.menu_items << { + key: "promotions", + route: -> { solidus_promotions.admin_promotions_path }, + icon: "megaphone-line", + position: 1.6, + children: [ + { + key: "promotions", + route: -> { solidus_promotions.admin_promotions_path }, + position: 1 + }, + { + key: "promotion_categories", + route: -> { solidus_promotions.admin_promotion_categories_path }, + position: 1 + } + ] + } + end + end + end + initializer "solidus_promotions.add_backend_menus", after: "spree.backend.environment" do if SolidusSupport.backend_available? promotions_menu_item = Spree::BackendConfiguration::MenuItem.new( - label: :solidus_promotions, + label: :promotions, icon: Spree::Backend::Config.admin_updated_navbar ? "ri-megaphone-line" : "bullhorn", condition: -> { can?(:admin, SolidusPromotions::Promotion) }, url: -> { SolidusPromotions::Engine.routes.url_helpers.admin_promotions_path }, data_hook: :admin_solidus_promotion_sub_tabs, children: [ Spree::BackendConfiguration::MenuItem.new( - label: :solidus_promotions, + label: :promotions, url: -> { SolidusPromotions::Engine.routes.url_helpers.admin_promotions_path }, condition: -> { can?(:admin, SolidusPromotions::Promotion) } ), Spree::BackendConfiguration::MenuItem.new( - label: :solidus_promotion_categories, + label: :promotion_categories, url: -> { SolidusPromotions::Engine.routes.url_helpers.admin_promotion_categories_path }, condition: -> { can?(:admin, SolidusPromotions::PromotionCategory) } ) ] ) + # We want to appear after the legacy promotions menu item if it exists, otherwise after the products menu item product_menu_item_index = Spree::Backend::Config.menu_items.find_index { |item| item.label == :products } - Spree::Backend::Config.menu_items.insert(product_menu_item_index + 1, promotions_menu_item) + legacy_promotions_menu_item = Spree::Backend::Config.menu_items.find_index { |item| item.label == :legacy_promotions } + promotions_menu_index = [product_menu_item_index, legacy_promotions_menu_item].compact.max + 1 + + Spree::Backend::Config.menu_items.insert(promotions_menu_index, promotions_menu_item) end end end diff --git a/promotions/spec/system/solidus_promotions/backend/main_menu_spec.rb b/promotions/spec/system/solidus_promotions/backend/main_menu_spec.rb index 73021eba6aa..788365fcc98 100644 --- a/promotions/spec/system/solidus_promotions/backend/main_menu_spec.rb +++ b/promotions/spec/system/solidus_promotions/backend/main_menu_spec.rb @@ -12,10 +12,10 @@ end it "should have a link to promotions" do - expect(page).to have_link("Promotions", href: solidus_promotions.admin_promotions_path, count: 2) + expect(page).to have_link("Promotions (new)", href: solidus_promotions.admin_promotions_path, count: 2) end it "should have a link to legacy promotions" do - expect(page).to have_link("Legacy Promotions", href: spree.admin_promotions_path, count: 2) + expect(page).to have_link("Promotions", href: spree.admin_promotions_path, count: 2) end end @@ -25,11 +25,11 @@ end it "should have a link to promotions" do - within(".selected .admin-subnav") { expect(page).to have_link("Promotions", href: solidus_promotions.admin_promotions_path) } + within(".selected .admin-subnav") { expect(page).to have_link("Promotions (new)", href: solidus_promotions.admin_promotions_path) } end it "should have a link to promotion categories" do - within(".selected .admin-subnav") { expect(page).to have_link("Promotion Categories", href: solidus_promotions.admin_promotion_categories_path) } + within(".selected .admin-subnav") { expect(page).to have_link("Promotion Categories (new)", href: solidus_promotions.admin_promotion_categories_path) } end end @@ -39,11 +39,11 @@ end it "should have a link to promotions" do - within(".selected .admin-subnav") { expect(page).to have_link("Legacy Promotions", href: spree.admin_promotions_path) } + within(".selected .admin-subnav") { expect(page).to have_link("Promotions", href: spree.admin_promotions_path) } end it "should have a link to promotion categories" do - within(".selected .admin-subnav") { expect(page).to have_link("Legacy Promotion Categories", href: spree.admin_promotion_categories_path) } + within(".selected .admin-subnav") { expect(page).to have_link("Promotion Categories", href: spree.admin_promotion_categories_path) } end end end