From d29d00b619a6f5c5b38a96ef778f305c8ba4d949 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 6 Jun 2024 16:10:49 +0200 Subject: [PATCH] Create custom orders index component for solidus_legacy_promotions The new admin allows orders to be filtered by promotion name. However, with the legacy promotion system extracted into an extension, we can't rely on `Spree::Promotion` to exist in `solidus_admin`. In order to get around this issue, we create a new index component for the admin orders page that includes the filter, and register it in an initializer. This also adds a spec for filtering by promotion name. --- .../solidus_admin/orders/index/component.rb | 9 +------- .../orders/index/component.rb | 15 +++++++++++++ .../orders/index/component.yml | 3 +++ .../lib/solidus_legacy_promotions/engine.rb | 8 +++++++ .../solidus_admin/orders/index_spec.rb | 21 +++++++++++++++++++ 5 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 legacy_promotions/lib/components/admin/solidus_legacy_promotions/orders/index/component.rb create mode 100644 legacy_promotions/lib/components/admin/solidus_legacy_promotions/orders/index/component.yml create mode 100644 legacy_promotions/spec/features/solidus_admin/orders/index_spec.rb diff --git a/admin/app/components/solidus_admin/orders/index/component.rb b/admin/app/components/solidus_admin/orders/index/component.rb index 5c412db937..92e37feadc 100644 --- a/admin/app/components/solidus_admin/orders/index/component.rb +++ b/admin/app/components/solidus_admin/orders/index/component.rb @@ -77,14 +77,7 @@ def filters option ] end - }, - { - label: t('.filters.promotions'), - combinator: 'or', - attribute: "promotions_id", - predicate: "in", - options: Spree::Promotion.all.pluck(:name, :id), - }, + } ] end diff --git a/legacy_promotions/lib/components/admin/solidus_legacy_promotions/orders/index/component.rb b/legacy_promotions/lib/components/admin/solidus_legacy_promotions/orders/index/component.rb new file mode 100644 index 0000000000..96bebec87f --- /dev/null +++ b/legacy_promotions/lib/components/admin/solidus_legacy_promotions/orders/index/component.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class SolidusLegacyPromotions::Orders::Index::Component < SolidusAdmin::Orders::Index::Component + def filters + super + [ + { + label: t('.filters.promotions'), + combinator: 'or', + attribute: "promotions_id", + predicate: "in", + options: Spree::Promotion.all.pluck(:name, :id), + } + ] + end +end diff --git a/legacy_promotions/lib/components/admin/solidus_legacy_promotions/orders/index/component.yml b/legacy_promotions/lib/components/admin/solidus_legacy_promotions/orders/index/component.yml new file mode 100644 index 0000000000..95af3f495d --- /dev/null +++ b/legacy_promotions/lib/components/admin/solidus_legacy_promotions/orders/index/component.yml @@ -0,0 +1,3 @@ +en: + filters: + promotions: Promotions diff --git a/legacy_promotions/lib/solidus_legacy_promotions/engine.rb b/legacy_promotions/lib/solidus_legacy_promotions/engine.rb index 2dc0aeb1bf..6ec4c6cf9f 100644 --- a/legacy_promotions/lib/solidus_legacy_promotions/engine.rb +++ b/legacy_promotions/lib/solidus_legacy_promotions/engine.rb @@ -29,6 +29,14 @@ class Engine < ::Rails::Engine end end + initializer "solidus_legacy_promotions.add_admin_order_index_component" do + if SolidusSupport.admin_available? + config.to_prepare do + SolidusAdmin::Config.components["orders/index"] = SolidusLegacyPromotions::Orders::Index::Component + end + end + end + initializer "solidus_legacy_promotions.add_solidus_admin_menu_items" do if SolidusSupport.admin_available? SolidusAdmin::Config.configure do |config| diff --git a/legacy_promotions/spec/features/solidus_admin/orders/index_spec.rb b/legacy_promotions/spec/features/solidus_admin/orders/index_spec.rb new file mode 100644 index 0000000000..e3f77611d2 --- /dev/null +++ b/legacy_promotions/spec/features/solidus_admin/orders/index_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe "Orders", type: :feature, solidus_admin: true do + let(:promotion) { create(:promotion, name: "10OFF") } + let!(:order_with_promotion) { create(:completed_order_with_promotion, number: "R123456789", promotion: promotion) } + let!(:order_without_promotion) { create(:completed_order_with_totals, number: "R987654321") } + + before { sign_in create(:admin_user, email: 'admin@example.com') } + + it "lists products", :js do + visit "/admin/orders" + find('button[aria-label=Filter]').click + find(:xpath, "//summary[normalize-space(text())='Promotions']").click + check "10OFF" + expect(page).to have_content("R123456789") + expect(page).not_to have_content("R987654321") + expect(page).to be_axe_clean + end +end