Skip to content

Commit

Permalink
Fix(Admin menus): Stable promotion menu items
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mamhoff committed Nov 18, 2024
1 parent 2632096 commit 6d75515
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 20 deletions.
8 changes: 6 additions & 2 deletions legacy_promotions/config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
en:
solidus_admin:
menu_item:
legacy_promotions: Promotions
legacy_promotion_categories: Promotion Categories
spree:
admin:
promotion_status:
Expand All @@ -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
Expand Down
25 changes: 19 additions & 6 deletions legacy_promotions/lib/solidus_legacy_promotions/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ 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) },
url: :admin_promotions_path,
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 },
)
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions promotions/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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. <br> If no value is specified, the promotion will never expire.
Expand Down
37 changes: 33 additions & 4 deletions promotions/lib/solidus_promotions/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 6d75515

Please sign in to comment.