From 4002e57463ad0cff6a2abba24081b29cb497b396 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Sun, 24 Sep 2023 07:58:39 +0200 Subject: [PATCH 01/98] Lay groundwork for order tabs builder --- .../resources/order_default_tabs_builder.rb | 11 +++++++ .../order_default_tabs_builder_spec.rb | 30 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 app/models/spree/admin/resources/order_default_tabs_builder.rb create mode 100644 spec/models/spree/admin/resources/order_default_tabs_builder_spec.rb diff --git a/app/models/spree/admin/resources/order_default_tabs_builder.rb b/app/models/spree/admin/resources/order_default_tabs_builder.rb new file mode 100644 index 0000000000..c7ab411a4a --- /dev/null +++ b/app/models/spree/admin/resources/order_default_tabs_builder.rb @@ -0,0 +1,11 @@ +module Spree + module Admin + module Resources + class OrderDefaultTabsBuilder + def build + + end + end + end + end +end diff --git a/spec/models/spree/admin/resources/order_default_tabs_builder_spec.rb b/spec/models/spree/admin/resources/order_default_tabs_builder_spec.rb new file mode 100644 index 0000000000..bef2b8de0a --- /dev/null +++ b/spec/models/spree/admin/resources/order_default_tabs_builder_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +module Spree + module Admin + describe Resources::OrderDefaultTabsBuilder, type: :model do + let(:builder) { described_class.new } + let(:default_tabs) do + %w(cart + channel + customer + shipments + adjustments + payments + return_authorizations + customer_returns + state_changes) + end + + describe '#build' do + subject { builder.build } + + it 'builds default tabs' do + # this means that tab items will need to respond to 'text' message, + # just as section items respond to 'key' message + expect(subject.items.map(&:text)).to match(default_tabs) + end + end + end + end +end From 9ee43d7f69f365611fc68330fc856463cc675f18 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Sun, 24 Sep 2023 19:48:48 +0200 Subject: [PATCH 02/98] Lay groundwork for tab's root item --- app/models/spree/admin/resources/root.rb | 25 +++++++++ .../models/spree/admin/resources/root_spec.rb | 52 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 app/models/spree/admin/resources/root.rb create mode 100644 spec/models/spree/admin/resources/root_spec.rb diff --git a/app/models/spree/admin/resources/root.rb b/app/models/spree/admin/resources/root.rb new file mode 100644 index 0000000000..1091dcfeeb --- /dev/null +++ b/app/models/spree/admin/resources/root.rb @@ -0,0 +1,25 @@ +module Spree + module Admin + module Resources + class Root + attr_reader :items + + def initialize + @items = [] + end + + def add(item) + raise KeyError, "Item with key #{item.key} already exists" if index_for_key(item.key) + + @items << item + end + + private + + def index_for_key(key) + @items.index { |e| e.key == key } + end + end + end + end +end diff --git a/spec/models/spree/admin/resources/root_spec.rb b/spec/models/spree/admin/resources/root_spec.rb new file mode 100644 index 0000000000..32df72a3b7 --- /dev/null +++ b/spec/models/spree/admin/resources/root_spec.rb @@ -0,0 +1,52 @@ +require 'spec_helper' + +module Spree + module Admin + describe Resources::Root, type: :model do + let(:root) { described_class.new } + + describe '#key' do + subject { root.key } + + it 'returns root' do + expect(subject).to eq('root') + end + end + + describe '#add(item)' do + let(:item) { double(key: 'test') } + + context "when there's no item with a particular key" do + it 'appends an item' do + root.add(item) + expect(root.items).to include(item) + end + end + + # context "when there is an item with a particular key" + # it 'raises an error' do + + # end + # end + end + + describe '#children?' do + subject { root.children? } + + context 'when there are child items' do + before { root.add(double(key: 'test')) } + + it 'returns true' do + expect(subject).to be(true) + end + end + + context 'when there are no child items' do + it 'returns false' do + expect(subject).to be(false) + end + end + end + end + end +end From b90bcb68fa0182c071164f496bbc2e1f40e2d63b Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Sun, 24 Sep 2023 19:58:06 +0200 Subject: [PATCH 03/98] Add test for adding to root already appended item --- spec/models/spree/admin/resources/root_spec.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/spec/models/spree/admin/resources/root_spec.rb b/spec/models/spree/admin/resources/root_spec.rb index 32df72a3b7..9df0970841 100644 --- a/spec/models/spree/admin/resources/root_spec.rb +++ b/spec/models/spree/admin/resources/root_spec.rb @@ -23,11 +23,13 @@ module Admin end end - # context "when there is an item with a particular key" - # it 'raises an error' do - - # end - # end + context "when there is an item with a particular key" do + before { root.add(item) } + + it 'raises an error' do + expect { root.add(item) }.to raise_error(KeyError) + end + end end describe '#children?' do From 38fa683a0fb546f72bbfd571513dbd23136c21ae Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Sun, 24 Sep 2023 20:03:00 +0200 Subject: [PATCH 04/98] Remove unnecessary tests --- .../models/spree/admin/resources/root_spec.rb | 26 ------------------- 1 file changed, 26 deletions(-) diff --git a/spec/models/spree/admin/resources/root_spec.rb b/spec/models/spree/admin/resources/root_spec.rb index 9df0970841..d85889d615 100644 --- a/spec/models/spree/admin/resources/root_spec.rb +++ b/spec/models/spree/admin/resources/root_spec.rb @@ -5,14 +5,6 @@ module Admin describe Resources::Root, type: :model do let(:root) { described_class.new } - describe '#key' do - subject { root.key } - - it 'returns root' do - expect(subject).to eq('root') - end - end - describe '#add(item)' do let(:item) { double(key: 'test') } @@ -31,24 +23,6 @@ module Admin end end end - - describe '#children?' do - subject { root.children? } - - context 'when there are child items' do - before { root.add(double(key: 'test')) } - - it 'returns true' do - expect(subject).to be(true) - end - end - - context 'when there are no child items' do - it 'returns false' do - expect(subject).to be(false) - end - end - end end end end From 485ee96fea9b034d7e557502457c2ff10d06ffea Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Mon, 25 Sep 2023 11:31:00 +0200 Subject: [PATCH 05/98] Add test for tab class. Add tab class. --- app/models/spree/admin/resources/tab.rb | 20 ++++++++ spec/models/spree/admin/resources/tab_spec.rb | 48 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 app/models/spree/admin/resources/tab.rb create mode 100644 spec/models/spree/admin/resources/tab_spec.rb diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb new file mode 100644 index 0000000000..638d8eae7b --- /dev/null +++ b/app/models/spree/admin/resources/tab.rb @@ -0,0 +1,20 @@ +module Spree + module Admin + module Resources + class Tab + attr_reader :icon_name, :availability_check + + def initialize(icon_name, availability_check, options = {}) + @icon_name = icon_name + @availability_check = availability_check + end + + def available?(current_ability, current_store) + return true unless @availability_check.present? + + @availability_check.call(current_ability, current_store) + end + end + end + end +end diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/resources/tab_spec.rb new file mode 100644 index 0000000000..49868f411a --- /dev/null +++ b/spec/models/spree/admin/resources/tab_spec.rb @@ -0,0 +1,48 @@ +require 'spec_helper' + +module Spree + module Admin + describe Resources::Tab, type: :model do + let(:tab) { described_class.new(icon_name, availability_check) } + let(:icon_name) { 'cart-check.svg' } + let(:availability_check) { nil } + + describe '#icon_name' do + subject { tab.icon_name } + + it 'returns icon_name' do + expect(subject).to eq(icon_name) + end + end + + describe '#available?' do + subject { tab.available?(ability, store) } + + let(:ability) { double } + let(:store) { double } + + context 'when availability check is not set' do + it 'is returns true' do + expect(subject).to be(true) + end + end + + context 'when availability check returns true' do + let(:availability_check) { ->(_ability, _store) { true } } + + it 'returns true' do + expect(subject).to be(true) + end + end + + context 'when availability check returns false' do + let(:availability_check) { ->(_ability, _store) { false } } + + it 'returns true' do + expect(subject).to be(false) + end + end + end + end + end +end From cbe0e80403e71d3c1547b05aa59a6afde8152558 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Mon, 25 Sep 2023 11:35:32 +0200 Subject: [PATCH 06/98] Accommodate text variable in tab class --- app/models/spree/admin/resources/tab.rb | 5 +++-- spec/models/spree/admin/resources/tab_spec.rb | 11 ++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb index 638d8eae7b..9ffba5cda4 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/resources/tab.rb @@ -2,10 +2,11 @@ module Spree module Admin module Resources class Tab - attr_reader :icon_name, :availability_check + attr_reader :icon_name, :text, :availability_check - def initialize(icon_name, availability_check, options = {}) + def initialize(icon_name, text, availability_check, options = {}) @icon_name = icon_name + @text = text @availability_check = availability_check end diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/resources/tab_spec.rb index 49868f411a..a342478ad2 100644 --- a/spec/models/spree/admin/resources/tab_spec.rb +++ b/spec/models/spree/admin/resources/tab_spec.rb @@ -3,8 +3,9 @@ module Spree module Admin describe Resources::Tab, type: :model do - let(:tab) { described_class.new(icon_name, availability_check) } + let(:tab) { described_class.new(icon_name, text, availability_check) } let(:icon_name) { 'cart-check.svg' } + let(:text) { 'Cart' } let(:availability_check) { nil } describe '#icon_name' do @@ -14,6 +15,14 @@ module Admin expect(subject).to eq(icon_name) end end + + describe '#text' do + subject { tab.text } + + it 'returns text' do + expect(subject).to eq(text) + end + end describe '#available?' do subject { tab.available?(ability, store) } From d0cade76491b10a7e7c6a43ca50fe280003ee39b Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Mon, 25 Sep 2023 13:40:05 +0200 Subject: [PATCH 07/98] Facilitate tab url --- app/models/spree/admin/resources/tab.rb | 5 +++-- spec/models/spree/admin/resources/tab_spec.rb | 11 ++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb index 9ffba5cda4..668ebc9164 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/resources/tab.rb @@ -2,11 +2,12 @@ module Spree module Admin module Resources class Tab - attr_reader :icon_name, :text, :availability_check + attr_reader :icon_name, :text, :url, :availability_check - def initialize(icon_name, text, availability_check, options = {}) + def initialize(icon_name, text, url, availability_check, options = {}) @icon_name = icon_name @text = text + @url = url @availability_check = availability_check end diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/resources/tab_spec.rb index a342478ad2..3b09ce1627 100644 --- a/spec/models/spree/admin/resources/tab_spec.rb +++ b/spec/models/spree/admin/resources/tab_spec.rb @@ -3,9 +3,10 @@ module Spree module Admin describe Resources::Tab, type: :model do - let(:tab) { described_class.new(icon_name, text, availability_check) } + let(:tab) { described_class.new(icon_name, text, url, availability_check) } let(:icon_name) { 'cart-check.svg' } let(:text) { 'Cart' } + let(:url) { '/cart' } let(:availability_check) { nil } describe '#icon_name' do @@ -23,6 +24,14 @@ module Admin expect(subject).to eq(text) end end + + describe '#url' do + subject { tab.url } + + it 'returns url' do + expect(subject).to eq(url) + end + end describe '#available?' do subject { tab.available?(ability, store) } From 941d18695fab9271ed5514786db2fb5ea6256c15 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Mon, 25 Sep 2023 17:37:46 +0200 Subject: [PATCH 08/98] Add partial_name to tabs attributes --- app/models/spree/admin/resources/tab.rb | 5 +++-- spec/models/spree/admin/resources/tab_spec.rb | 14 +++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb index 668ebc9164..979c830f67 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/resources/tab.rb @@ -2,12 +2,13 @@ module Spree module Admin module Resources class Tab - attr_reader :icon_name, :text, :url, :availability_check + attr_reader :icon_name, :text, :url, :partial_name, :availability_check - def initialize(icon_name, text, url, availability_check, options = {}) + def initialize(icon_name, text, url, partial_name, availability_check, options = {}) @icon_name = icon_name @text = text @url = url + @partial_name = partial_name @availability_check = availability_check end diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/resources/tab_spec.rb index 3b09ce1627..847bdc1eb2 100644 --- a/spec/models/spree/admin/resources/tab_spec.rb +++ b/spec/models/spree/admin/resources/tab_spec.rb @@ -3,10 +3,11 @@ module Spree module Admin describe Resources::Tab, type: :model do - let(:tab) { described_class.new(icon_name, text, url, availability_check) } + let(:tab) { described_class.new(icon_name, text, url, partial_name, availability_check) } let(:icon_name) { 'cart-check.svg' } let(:text) { 'Cart' } let(:url) { '/cart' } + let(:partial_name) { 'Cart' } let(:availability_check) { nil } describe '#icon_name' do @@ -32,6 +33,17 @@ module Admin expect(subject).to eq(url) end end + + # TO-DO: If partial names would match the contents of 'text' - this can be removed. + # For example, there's only one mismatch in _orders_tabs partial, and it's + # :customer_details not matching :customer + describe '#partial_name' do + subject { tab.partial_name } + + it 'returns partial_name' do + expect(subject).to eq(partial_name) + end + end describe '#available?' do subject { tab.available?(ability, store) } From 65de37e7345316550210822881ad7dbdc0f61357 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 26 Sep 2023 08:03:17 +0200 Subject: [PATCH 09/98] Add css classes to tab attributes --- app/models/spree/admin/resources/tab.rb | 5 +++-- spec/models/spree/admin/resources/tab_spec.rb | 13 ++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb index 979c830f67..7447f4d0a9 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/resources/tab.rb @@ -2,13 +2,14 @@ module Spree module Admin module Resources class Tab - attr_reader :icon_name, :text, :url, :partial_name, :availability_check + attr_reader :icon_name, :text, :url, :partial_name, :classes, :availability_check - def initialize(icon_name, text, url, partial_name, availability_check, options = {}) + def initialize(icon_name, text, url, partial_name, classes, availability_check, options = {}) @icon_name = icon_name @text = text @url = url @partial_name = partial_name + @classes = classes @availability_check = availability_check end diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/resources/tab_spec.rb index 847bdc1eb2..05261dbc73 100644 --- a/spec/models/spree/admin/resources/tab_spec.rb +++ b/spec/models/spree/admin/resources/tab_spec.rb @@ -3,11 +3,14 @@ module Spree module Admin describe Resources::Tab, type: :model do - let(:tab) { described_class.new(icon_name, text, url, partial_name, availability_check) } + let(:tab) { described_class.new(icon_name, text, url, partial_name, classes, availability_check) } let(:icon_name) { 'cart-check.svg' } let(:text) { 'Cart' } let(:url) { '/cart' } let(:partial_name) { 'Cart' } + let(:classes) do + { class: 'nav-link active' } + end let(:availability_check) { nil } describe '#icon_name' do @@ -44,6 +47,14 @@ module Admin expect(subject).to eq(partial_name) end end + + describe '#classes' do + subject { tab.classes } + + it 'returns classes' do + expect(subject).to eq(classes) + end + end describe '#available?' do subject { tab.available?(ability, store) } From 55abda62ad9090767020947375374d68683d477d Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 26 Sep 2023 09:43:09 +0200 Subject: [PATCH 10/98] Add methods for building particular tabs --- .../resources/order_default_tabs_builder.rb | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/app/models/spree/admin/resources/order_default_tabs_builder.rb b/app/models/spree/admin/resources/order_default_tabs_builder.rb index c7ab411a4a..c90ded33e5 100644 --- a/app/models/spree/admin/resources/order_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/order_default_tabs_builder.rb @@ -2,9 +2,50 @@ module Spree module Admin module Resources class OrderDefaultTabsBuilder + include Spree::Core::Engine.routes.url_helpers + def build - + root = Root.new + add_cart_tab(root) + add_channel_tab(root) + add_customer_tab(root) + add_shipments_tab(root) + add_adjustments_tab(root) + add_payments_tab(root) + add_return_authorizations_tab(root) + add_customer_returns_tab(root) + add_state_changes_tab(root) end + + private + + def add_cart_tab(root) + end + + def add_channel_tab(root) + end + + def add_customer_tab(root) + end + + def add_shipments_tab(root) + end + + def add_adjustments_tab(root) + end + + def add_payments_tab(root) + end + + def add_return_authorizations_tab(root) + end + + def add_customer_returns_tab(root) + end + + def add_state_changes_tab(root) + end + end end end From 8eca944ea22dce3253b0e16294653d64ab4e1502 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 26 Sep 2023 10:34:10 +0200 Subject: [PATCH 11/98] Add Proc handling for url attribute --- app/models/spree/admin/resources/tab.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb index 7447f4d0a9..be0f19aa06 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/resources/tab.rb @@ -2,7 +2,7 @@ module Spree module Admin module Resources class Tab - attr_reader :icon_name, :text, :url, :partial_name, :classes, :availability_check + attr_reader :icon_name, :text, :partial_name, :classes, :availability_check def initialize(icon_name, text, url, partial_name, classes, availability_check, options = {}) @icon_name = icon_name @@ -18,6 +18,10 @@ def available?(current_ability, current_store) @availability_check.call(current_ability, current_store) end + + def url(resource) + @url.is_a?(Proc) ? @url.call(resource) : @url + end end end end From 33707689adc6ff3ece6e5d1ea06d1728c7478fac Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 26 Sep 2023 13:06:14 +0200 Subject: [PATCH 12/98] Facilitate check for active css class. Fix url attribute definition. --- app/models/spree/admin/resources/active_checker.rb | 12 ++++++++++++ app/models/spree/admin/resources/tab.rb | 8 +++++++- spec/models/spree/admin/resources/tab_spec.rb | 4 +--- 3 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 app/models/spree/admin/resources/active_checker.rb diff --git a/app/models/spree/admin/resources/active_checker.rb b/app/models/spree/admin/resources/active_checker.rb new file mode 100644 index 0000000000..b0926f42cd --- /dev/null +++ b/app/models/spree/admin/resources/active_checker.rb @@ -0,0 +1,12 @@ +module Spree + module Admin + module Resources + module ActiveChecker + def with_active_check + @active_check = ->(current_tab, partial_name) { current_tab == partial_name } + self + end + end + end + end +end diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb index be0f19aa06..ea46217b11 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/resources/tab.rb @@ -2,6 +2,8 @@ module Spree module Admin module Resources class Tab + include ActiveChecker + attr_reader :icon_name, :text, :partial_name, :classes, :availability_check def initialize(icon_name, text, url, partial_name, classes, availability_check, options = {}) @@ -19,9 +21,13 @@ def available?(current_ability, current_store) @availability_check.call(current_ability, current_store) end - def url(resource) + def url(resource = nil) @url.is_a?(Proc) ? @url.call(resource) : @url end + + def active?(current_tab, partial_name) + @active_check.call(current_tab, partial_name) + end end end end diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/resources/tab_spec.rb index 05261dbc73..3e29d72a30 100644 --- a/spec/models/spree/admin/resources/tab_spec.rb +++ b/spec/models/spree/admin/resources/tab_spec.rb @@ -8,9 +8,7 @@ module Admin let(:text) { 'Cart' } let(:url) { '/cart' } let(:partial_name) { 'Cart' } - let(:classes) do - { class: 'nav-link active' } - end + let(:classes) { 'nav-link' } let(:availability_check) { nil } describe '#icon_name' do From a27ea5e29eac8b7e6ecc2f7dea1555564075d301 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 26 Sep 2023 13:40:00 +0200 Subject: [PATCH 13/98] Include module providing availability_check --- .../admin/resources/availability_builder_methods.rb | 12 ++++++++++++ app/models/spree/admin/resources/tab.rb | 1 + 2 files changed, 13 insertions(+) create mode 100644 app/models/spree/admin/resources/availability_builder_methods.rb diff --git a/app/models/spree/admin/resources/availability_builder_methods.rb b/app/models/spree/admin/resources/availability_builder_methods.rb new file mode 100644 index 0000000000..d3c6b718f5 --- /dev/null +++ b/app/models/spree/admin/resources/availability_builder_methods.rb @@ -0,0 +1,12 @@ +module Spree + module Admin + module Resources + module AvailabilityBuilderMethods + def with_update_availability_check + @availability_check = ->(ability, resource) { ability.can?(:update, resource) } + self + end + end + end + end +end diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb index ea46217b11..6baaac619f 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/resources/tab.rb @@ -3,6 +3,7 @@ module Admin module Resources class Tab include ActiveChecker + include AvailabilityBuilderMethods attr_reader :icon_name, :text, :partial_name, :classes, :availability_check From c47c921288f2d8fd5f963b98ca21b0265cffa620 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 26 Sep 2023 13:41:22 +0200 Subject: [PATCH 14/98] Initialize cart tab --- .../spree/admin/resources/order_default_tabs_builder.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/models/spree/admin/resources/order_default_tabs_builder.rb b/app/models/spree/admin/resources/order_default_tabs_builder.rb index c90ded33e5..b29d8f094e 100644 --- a/app/models/spree/admin/resources/order_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/order_default_tabs_builder.rb @@ -20,6 +20,15 @@ def build private def add_cart_tab(root) + Tab.new( + 'cart-check.svg', + :cart, + ->(resource) { cart_admin_order_url(resource) }, + :cart, + 'nav-link', + ) + .with_active_check + .with_update_availability_check end def add_channel_tab(root) From 9e33d66a693f9c3d06b9ca3fb5f656114ad55308 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 26 Sep 2023 15:17:04 +0200 Subject: [PATCH 15/98] Build cart tab --- .../admin/resources/availability_builder_methods.rb | 5 +++++ .../admin/resources/order_default_tabs_builder.rb | 12 ++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/models/spree/admin/resources/availability_builder_methods.rb b/app/models/spree/admin/resources/availability_builder_methods.rb index d3c6b718f5..6654386cd8 100644 --- a/app/models/spree/admin/resources/availability_builder_methods.rb +++ b/app/models/spree/admin/resources/availability_builder_methods.rb @@ -2,6 +2,11 @@ module Spree module Admin module Resources module AvailabilityBuilderMethods + def with_availability_check(availability_check) + @availability_check = availability_check + self + end + def with_update_availability_check @availability_check = ->(ability, resource) { ability.can?(:update, resource) } self diff --git a/app/models/spree/admin/resources/order_default_tabs_builder.rb b/app/models/spree/admin/resources/order_default_tabs_builder.rb index b29d8f094e..9545ab57ac 100644 --- a/app/models/spree/admin/resources/order_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/order_default_tabs_builder.rb @@ -20,7 +20,7 @@ def build private def add_cart_tab(root) - Tab.new( + tab = Tab.new( 'cart-check.svg', :cart, ->(resource) { cart_admin_order_url(resource) }, @@ -28,7 +28,15 @@ def add_cart_tab(root) 'nav-link', ) .with_active_check - .with_update_availability_check + .with_availability_check( + # An abstract module should not be aware of resource's internal structure. + # If these checks are elaborate, it's better to have this complexity declared explicitly here. + ->(ability, resource) do + ability.can?(:update, resource) + && (resource.shipments.size.zero? || resource.shipments.shipped.size.zero?) + end + ) + root.add(tab) end def add_channel_tab(root) From 38b2f1c344707aa1b952c439c3a107a3a73c01d1 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 26 Sep 2023 15:27:04 +0200 Subject: [PATCH 16/98] Fix initialization --- .../spree/admin/resources/order_default_tabs_builder.rb | 4 +--- app/models/spree/admin/resources/tab.rb | 5 ++--- spec/models/spree/admin/resources/tab_spec.rb | 3 +-- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/app/models/spree/admin/resources/order_default_tabs_builder.rb b/app/models/spree/admin/resources/order_default_tabs_builder.rb index 9545ab57ac..d116147d39 100644 --- a/app/models/spree/admin/resources/order_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/order_default_tabs_builder.rb @@ -32,11 +32,9 @@ def add_cart_tab(root) # An abstract module should not be aware of resource's internal structure. # If these checks are elaborate, it's better to have this complexity declared explicitly here. ->(ability, resource) do - ability.can?(:update, resource) - && (resource.shipments.size.zero? || resource.shipments.shipped.size.zero?) + ability.can?(:update, resource) && (resource.shipments.size.zero? || resource.shipments.shipped.size.zero?) end ) - root.add(tab) end def add_channel_tab(root) diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb index 6baaac619f..cad151503a 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/resources/tab.rb @@ -5,15 +5,14 @@ class Tab include ActiveChecker include AvailabilityBuilderMethods - attr_reader :icon_name, :text, :partial_name, :classes, :availability_check + attr_reader :icon_name, :text, :partial_name, :classes - def initialize(icon_name, text, url, partial_name, classes, availability_check, options = {}) + def initialize(icon_name, text, url, partial_name, classes, options = {}) @icon_name = icon_name @text = text @url = url @partial_name = partial_name @classes = classes - @availability_check = availability_check end def available?(current_ability, current_store) diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/resources/tab_spec.rb index 3e29d72a30..cf99b631d4 100644 --- a/spec/models/spree/admin/resources/tab_spec.rb +++ b/spec/models/spree/admin/resources/tab_spec.rb @@ -3,13 +3,12 @@ module Spree module Admin describe Resources::Tab, type: :model do - let(:tab) { described_class.new(icon_name, text, url, partial_name, classes, availability_check) } + let(:tab) { described_class.new(icon_name, text, url, partial_name, classes) } let(:icon_name) { 'cart-check.svg' } let(:text) { 'Cart' } let(:url) { '/cart' } let(:partial_name) { 'Cart' } let(:classes) { 'nav-link' } - let(:availability_check) { nil } describe '#icon_name' do subject { tab.icon_name } From 9699a26986c4f0d64880de2e9503884445d2b8f6 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 26 Sep 2023 15:38:31 +0200 Subject: [PATCH 17/98] Fix - use existing attribute --- app/models/spree/admin/resources/root.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/spree/admin/resources/root.rb b/app/models/spree/admin/resources/root.rb index 1091dcfeeb..cb19b7b5c4 100644 --- a/app/models/spree/admin/resources/root.rb +++ b/app/models/spree/admin/resources/root.rb @@ -9,7 +9,7 @@ def initialize end def add(item) - raise KeyError, "Item with key #{item.key} already exists" if index_for_key(item.key) + raise KeyError, "Item with key #{item.text} already exists" if index_for_key(item.text) @items << item end From 413e034073e53841feeb2a22c55209904741cfb2 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 27 Sep 2023 07:13:03 +0200 Subject: [PATCH 18/98] Improve variable naming --- app/models/spree/admin/resources/tab.rb | 4 ++-- spec/models/spree/admin/resources/tab_spec.rb | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb index cad151503a..1cf6866ad7 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/resources/tab.rb @@ -15,10 +15,10 @@ def initialize(icon_name, text, url, partial_name, classes, options = {}) @classes = classes end - def available?(current_ability, current_store) + def available?(current_ability, resource) return true unless @availability_check.present? - @availability_check.call(current_ability, current_store) + @availability_check.call(current_ability, resource) end def url(resource = nil) diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/resources/tab_spec.rb index cf99b631d4..b0a23774d5 100644 --- a/spec/models/spree/admin/resources/tab_spec.rb +++ b/spec/models/spree/admin/resources/tab_spec.rb @@ -54,10 +54,10 @@ module Admin end describe '#available?' do - subject { tab.available?(ability, store) } + subject { tab.available?(ability, resource) } let(:ability) { double } - let(:store) { double } + let(:resource) { double } context 'when availability check is not set' do it 'is returns true' do @@ -66,7 +66,7 @@ module Admin end context 'when availability check returns true' do - let(:availability_check) { ->(_ability, _store) { true } } + let(:availability_check) { ->(_ability, _resource) { true } } it 'returns true' do expect(subject).to be(true) @@ -74,7 +74,7 @@ module Admin end context 'when availability check returns false' do - let(:availability_check) { ->(_ability, _store) { false } } + let(:availability_check) { ->(_ability, _resource) { false } } it 'returns true' do expect(subject).to be(false) From 2911f8477d583e3191f2f8d607e7f08e15191fd1 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 27 Sep 2023 07:52:15 +0200 Subject: [PATCH 19/98] Fix test setup --- spec/models/spree/admin/resources/tab_spec.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/resources/tab_spec.rb index b0a23774d5..70da7e643f 100644 --- a/spec/models/spree/admin/resources/tab_spec.rb +++ b/spec/models/spree/admin/resources/tab_spec.rb @@ -66,7 +66,10 @@ module Admin end context 'when availability check returns true' do - let(:availability_check) { ->(_ability, _resource) { true } } + + before do + tab.with_availability_check(->(_ability, _resource) { true }) + end it 'returns true' do expect(subject).to be(true) @@ -74,7 +77,10 @@ module Admin end context 'when availability check returns false' do - let(:availability_check) { ->(_ability, _resource) { false } } + + before do + tab.with_availability_check(->(_ability, _resource) { false }) + end it 'returns true' do expect(subject).to be(false) From 0a8ffddf9b2a0664611219bf95534b760e643e71 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 27 Sep 2023 08:28:06 +0200 Subject: [PATCH 20/98] Fix test description --- spec/models/spree/admin/resources/tab_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/resources/tab_spec.rb index 70da7e643f..dc28495481 100644 --- a/spec/models/spree/admin/resources/tab_spec.rb +++ b/spec/models/spree/admin/resources/tab_spec.rb @@ -82,7 +82,7 @@ module Admin tab.with_availability_check(->(_ability, _resource) { false }) end - it 'returns true' do + it 'returns false' do expect(subject).to be(false) end end From 743a569d5f4358026f4c336f8586cffe3f224ca4 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 27 Sep 2023 08:44:41 +0200 Subject: [PATCH 21/98] Add test for checking if tab is active --- app/models/spree/admin/resources/tab.rb | 2 +- spec/models/spree/admin/resources/tab_spec.rb | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb index 1cf6866ad7..d7e2a05d35 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/resources/tab.rb @@ -25,7 +25,7 @@ def url(resource = nil) @url.is_a?(Proc) ? @url.call(resource) : @url end - def active?(current_tab, partial_name) + def active?(current_tab) @active_check.call(current_tab, partial_name) end end diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/resources/tab_spec.rb index dc28495481..4dee327b71 100644 --- a/spec/models/spree/admin/resources/tab_spec.rb +++ b/spec/models/spree/admin/resources/tab_spec.rb @@ -87,6 +87,28 @@ module Admin end end end + + describe '#active?' do + subject { tab.active?(current_tab) } + + before { tab.with_active_check } + + context 'when tab matches the current tab' do + let(:current_tab) { partial_name } + + it "returns true" do + expect(subject).to be(true) + end + end + + context 'when tab does not match the current tab' do + let(:current_tab) { 'non-matching' } + + it "returns false" do + expect(subject).to be(false) + end + end + end end end end From 9c988fba1d874cf02d2ca90eecf85ee1bdf1e5f1 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 27 Sep 2023 11:15:03 +0200 Subject: [PATCH 22/98] Add method for checking access to class index --- .../spree/admin/resources/availability_builder_methods.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/models/spree/admin/resources/availability_builder_methods.rb b/app/models/spree/admin/resources/availability_builder_methods.rb index 6654386cd8..a78f6bba11 100644 --- a/app/models/spree/admin/resources/availability_builder_methods.rb +++ b/app/models/spree/admin/resources/availability_builder_methods.rb @@ -11,6 +11,11 @@ def with_update_availability_check @availability_check = ->(ability, resource) { ability.can?(:update, resource) } self end + + def with_index_availability_check(klass) + @availability_check = ->(ability, _resource) { ability.can?(:index, klaas) } + self + end end end end From 5eebf1c9906744301247f8c2c1330e9095a2d022 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 27 Sep 2023 11:15:45 +0200 Subject: [PATCH 23/98] Populate tab building steps --- .../resources/order_default_tabs_builder.rb | 77 ++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/app/models/spree/admin/resources/order_default_tabs_builder.rb b/app/models/spree/admin/resources/order_default_tabs_builder.rb index d116147d39..b37b03fdc2 100644 --- a/app/models/spree/admin/resources/order_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/order_default_tabs_builder.rb @@ -38,29 +38,104 @@ def add_cart_tab(root) end def add_channel_tab(root) + tab = Tab.new( + 'funnel.svg', + :channel, + ->(resource) { channel_admin_order_url(resource) }, + :channel, + 'nav-link', + ) + .with_active_check + .with_update_availability_check end def add_customer_tab(root) + tab = Tab.new( + 'person-lines-fill.svg', + :customer, + ->(resource) { admin_order_customer_url(resource) }, + :customer_details, + 'nav-link', + ) + .with_active_check + .with_availability_check( + ->(ability, resource) do + ability.can?(:update, resource) && resource.checkout_steps.include?("address") + end + ) end def add_shipments_tab(root) + tab = Tab.new( + 'truck.svg', + :shipments, + ->(resource) { edit_admin_order_url(resource) }, + :shipments, + 'nav-link', + ) + .with_active_check + .with_update_availability_check end def add_adjustments_tab(root) + tab = Tab.new( + 'adjust.svg', + :adjustments, + ->(resource) { admin_order_adjustments_url(resource) }, + :adjustments, + 'nav-link', + ) + .with_active_check + .with_index_availability_check(Spree::Adjustment) end def add_payments_tab(root) + tab = Tab.new( + 'credit-card.svg', + :payments, + ->(resource) { admin_order_payments_url(resource) }, + :payments, + 'nav-link', + ) + .with_active_check + .with_index_availability_check(Spree::Payment) end def add_return_authorizations_tab(root) + tab = Tab.new( + 'enter.svg', + :return_authorizations, + ->(resource) { admin_order_return_authorizations_url(resource) }, + :return_authorizations, + 'nav-link', + ) + .with_active_check + .with_index_availability_check(Spree::ReturnAuthorization) end def add_customer_returns_tab(root) + tab = Tab.new( + 'returns.svg', + :customer_returns, + ->(resource) { admin_order_customer_returns_url(resource) }, + :customer_returns, + 'nav-link', + ) + .with_active_check + .with_index_availability_check(Spree::CustomerReturn) end def add_state_changes_tab(root) + tab = Tab.new( + 'calendar3.svg', + :state_changes, + ->(resource) { admin_order_state_changes_url(resource) }, + :state_changes, + 'nav-link', + ) + .with_active_check + .with_update_availability_check end - end end end From 94b9950b566015d01d3e04902615eb061a0a7a5c Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 27 Sep 2023 12:03:31 +0200 Subject: [PATCH 24/98] Unify naming of currently selected partials --- app/views/spree/admin/orders/customer_details/edit.html.erb | 2 +- app/views/spree/admin/shared/_order_tabs.html.erb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/spree/admin/orders/customer_details/edit.html.erb b/app/views/spree/admin/orders/customer_details/edit.html.erb index adea338af2..24fbdb240f 100644 --- a/app/views/spree/admin/orders/customer_details/edit.html.erb +++ b/app/views/spree/admin/orders/customer_details/edit.html.erb @@ -1,4 +1,4 @@ -<%= render 'spree/admin/shared/order_tabs', current: :customer_details %> +<%= render 'spree/admin/shared/order_tabs', current: :customer %> <% if can? :edit, @order.user %>
diff --git a/app/views/spree/admin/shared/_order_tabs.html.erb b/app/views/spree/admin/shared/_order_tabs.html.erb index 91d0fa80ea..cddac38107 100644 --- a/app/views/spree/admin/shared/_order_tabs.html.erb +++ b/app/views/spree/admin/shared/_order_tabs.html.erb @@ -27,7 +27,7 @@ <%= link_to_with_icon 'person-lines-fill.svg', Spree.t(:customer), spree.admin_order_customer_url(@order), - class: "#{'active' if current == :customer_details} nav-link" %> + class: "#{'active' if current == :customer} nav-link" %> <% end %> From d6fd48cf11c230ba438ee0477086774ade0610fe Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 27 Sep 2023 12:04:50 +0200 Subject: [PATCH 25/98] Remove dependency on unnecessary attribute --- app/models/spree/admin/resources/active_checker.rb | 2 +- .../admin/resources/order_default_tabs_builder.rb | 9 --------- app/models/spree/admin/resources/tab.rb | 7 +++---- spec/models/spree/admin/resources/tab_spec.rb | 11 ----------- 4 files changed, 4 insertions(+), 25 deletions(-) diff --git a/app/models/spree/admin/resources/active_checker.rb b/app/models/spree/admin/resources/active_checker.rb index b0926f42cd..3a92950e92 100644 --- a/app/models/spree/admin/resources/active_checker.rb +++ b/app/models/spree/admin/resources/active_checker.rb @@ -3,7 +3,7 @@ module Admin module Resources module ActiveChecker def with_active_check - @active_check = ->(current_tab, partial_name) { current_tab == partial_name } + @active_check = ->(current_tab, text) { current_tab == text } self end end diff --git a/app/models/spree/admin/resources/order_default_tabs_builder.rb b/app/models/spree/admin/resources/order_default_tabs_builder.rb index b37b03fdc2..b99d5f791f 100644 --- a/app/models/spree/admin/resources/order_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/order_default_tabs_builder.rb @@ -24,7 +24,6 @@ def add_cart_tab(root) 'cart-check.svg', :cart, ->(resource) { cart_admin_order_url(resource) }, - :cart, 'nav-link', ) .with_active_check @@ -42,7 +41,6 @@ def add_channel_tab(root) 'funnel.svg', :channel, ->(resource) { channel_admin_order_url(resource) }, - :channel, 'nav-link', ) .with_active_check @@ -54,7 +52,6 @@ def add_customer_tab(root) 'person-lines-fill.svg', :customer, ->(resource) { admin_order_customer_url(resource) }, - :customer_details, 'nav-link', ) .with_active_check @@ -70,7 +67,6 @@ def add_shipments_tab(root) 'truck.svg', :shipments, ->(resource) { edit_admin_order_url(resource) }, - :shipments, 'nav-link', ) .with_active_check @@ -82,7 +78,6 @@ def add_adjustments_tab(root) 'adjust.svg', :adjustments, ->(resource) { admin_order_adjustments_url(resource) }, - :adjustments, 'nav-link', ) .with_active_check @@ -94,7 +89,6 @@ def add_payments_tab(root) 'credit-card.svg', :payments, ->(resource) { admin_order_payments_url(resource) }, - :payments, 'nav-link', ) .with_active_check @@ -106,7 +100,6 @@ def add_return_authorizations_tab(root) 'enter.svg', :return_authorizations, ->(resource) { admin_order_return_authorizations_url(resource) }, - :return_authorizations, 'nav-link', ) .with_active_check @@ -118,7 +111,6 @@ def add_customer_returns_tab(root) 'returns.svg', :customer_returns, ->(resource) { admin_order_customer_returns_url(resource) }, - :customer_returns, 'nav-link', ) .with_active_check @@ -130,7 +122,6 @@ def add_state_changes_tab(root) 'calendar3.svg', :state_changes, ->(resource) { admin_order_state_changes_url(resource) }, - :state_changes, 'nav-link', ) .with_active_check diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb index d7e2a05d35..e67dc0a1c7 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/resources/tab.rb @@ -5,13 +5,12 @@ class Tab include ActiveChecker include AvailabilityBuilderMethods - attr_reader :icon_name, :text, :partial_name, :classes + attr_reader :icon_name, :text, :classes - def initialize(icon_name, text, url, partial_name, classes, options = {}) + def initialize(icon_name, text, url, classes, options = {}) @icon_name = icon_name @text = text @url = url - @partial_name = partial_name @classes = classes end @@ -26,7 +25,7 @@ def url(resource = nil) end def active?(current_tab) - @active_check.call(current_tab, partial_name) + @active_check.call(current_tab, text) end end end diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/resources/tab_spec.rb index 4dee327b71..e12d3a0e4e 100644 --- a/spec/models/spree/admin/resources/tab_spec.rb +++ b/spec/models/spree/admin/resources/tab_spec.rb @@ -34,17 +34,6 @@ module Admin end end - # TO-DO: If partial names would match the contents of 'text' - this can be removed. - # For example, there's only one mismatch in _orders_tabs partial, and it's - # :customer_details not matching :customer - describe '#partial_name' do - subject { tab.partial_name } - - it 'returns partial_name' do - expect(subject).to eq(partial_name) - end - end - describe '#classes' do subject { tab.classes } From c93c19276cadb6644649047195c25a2e007af70f Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 27 Sep 2023 12:14:15 +0200 Subject: [PATCH 26/98] Cleanup test setup --- spec/models/spree/admin/resources/tab_spec.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/resources/tab_spec.rb index e12d3a0e4e..bbf1de69ed 100644 --- a/spec/models/spree/admin/resources/tab_spec.rb +++ b/spec/models/spree/admin/resources/tab_spec.rb @@ -3,11 +3,10 @@ module Spree module Admin describe Resources::Tab, type: :model do - let(:tab) { described_class.new(icon_name, text, url, partial_name, classes) } + let(:tab) { described_class.new(icon_name, text, url, classes) } let(:icon_name) { 'cart-check.svg' } let(:text) { 'Cart' } let(:url) { '/cart' } - let(:partial_name) { 'Cart' } let(:classes) { 'nav-link' } describe '#icon_name' do @@ -83,7 +82,7 @@ module Admin before { tab.with_active_check } context 'when tab matches the current tab' do - let(:current_tab) { partial_name } + let(:current_tab) { text } it "returns true" do expect(subject).to be(true) From e013c0464e4ff5671384aa4bd45fe0f61a993143 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 27 Sep 2023 14:24:36 +0200 Subject: [PATCH 27/98] Facilitate .complete? check. Rename module. Fix lint. --- ...tive_checker.rb => conditional_checker.rb} | 7 +++- app/models/spree/admin/resources/tab.rb | 8 ++++- spec/models/spree/admin/resources/tab_spec.rb | 35 +++++++++++++++++-- 3 files changed, 46 insertions(+), 4 deletions(-) rename app/models/spree/admin/resources/{active_checker.rb => conditional_checker.rb} (56%) diff --git a/app/models/spree/admin/resources/active_checker.rb b/app/models/spree/admin/resources/conditional_checker.rb similarity index 56% rename from app/models/spree/admin/resources/active_checker.rb rename to app/models/spree/admin/resources/conditional_checker.rb index 3a92950e92..e5d5bbd09f 100644 --- a/app/models/spree/admin/resources/active_checker.rb +++ b/app/models/spree/admin/resources/conditional_checker.rb @@ -1,11 +1,16 @@ module Spree module Admin module Resources - module ActiveChecker + module ConditionalChecker def with_active_check @active_check = ->(current_tab, text) { current_tab == text } self end + + def with_completed_check + @completed_check = ->(resource) { resource.completed? } + self + end end end end diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb index e67dc0a1c7..e4151a0b13 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/resources/tab.rb @@ -2,7 +2,7 @@ module Spree module Admin module Resources class Tab - include ActiveChecker + include ConditionalChecker include AvailabilityBuilderMethods attr_reader :icon_name, :text, :classes @@ -27,6 +27,12 @@ def url(resource = nil) def active?(current_tab) @active_check.call(current_tab, text) end + + def complete?(resource) + return true unless @completed_check.present? + + @completed_check.call(resource) + end end end end diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/resources/tab_spec.rb index bbf1de69ed..d12b6dbeda 100644 --- a/spec/models/spree/admin/resources/tab_spec.rb +++ b/spec/models/spree/admin/resources/tab_spec.rb @@ -54,7 +54,6 @@ module Admin end context 'when availability check returns true' do - before do tab.with_availability_check(->(_ability, _resource) { true }) end @@ -65,7 +64,6 @@ module Admin end context 'when availability check returns false' do - before do tab.with_availability_check(->(_ability, _resource) { false }) end @@ -97,6 +95,39 @@ module Admin end end end + + describe '#complete?' do + subject { tab.complete?(resource) } + let(:resource) { double } + + context 'when complete check is not set' do + it 'is returns true' do + expect(subject).to be(true) + end + end + + context 'when complete check returns true' do + before do + tab.with_completed_check + allow(resource).to receive(:completed?).and_return(true) + end + + it 'returns true' do + expect(subject).to be(true) + end + end + + context 'when complete check returns false' do + before do + tab.with_completed_check + allow(resource).to receive(:completed?).and_return(false) + end + + it 'returns false' do + expect(subject).to be(false) + end + end + end end end end From 059f77af40e899b2d33b48b998e93843ba26b2a5 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 27 Sep 2023 15:13:25 +0200 Subject: [PATCH 28/98] Fix variable naming in root --- app/models/spree/admin/resources/root.rb | 6 +++--- spec/models/spree/admin/resources/root_spec.rb | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/models/spree/admin/resources/root.rb b/app/models/spree/admin/resources/root.rb index cb19b7b5c4..8507b89416 100644 --- a/app/models/spree/admin/resources/root.rb +++ b/app/models/spree/admin/resources/root.rb @@ -9,15 +9,15 @@ def initialize end def add(item) - raise KeyError, "Item with key #{item.text} already exists" if index_for_key(item.text) + raise KeyError, "Item with key #{item.text} already exists" if index_for_text(item.text) @items << item end private - def index_for_key(key) - @items.index { |e| e.key == key } + def index_for_text(text) + @items.index { |e| e.text == text } end end end diff --git a/spec/models/spree/admin/resources/root_spec.rb b/spec/models/spree/admin/resources/root_spec.rb index d85889d615..c36c746e6f 100644 --- a/spec/models/spree/admin/resources/root_spec.rb +++ b/spec/models/spree/admin/resources/root_spec.rb @@ -6,9 +6,10 @@ module Admin let(:root) { described_class.new } describe '#add(item)' do - let(:item) { double(key: 'test') } + let(:item) { double(text: 'test') } context "when there's no item with a particular key" do + it 'appends an item' do root.add(item) expect(root.items).to include(item) From a37a98b56738aabc4118254ede59ad8d558d0e21 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 27 Sep 2023 15:16:48 +0200 Subject: [PATCH 29/98] Populate root with tabs. Fix test setup. --- .../resources/order_default_tabs_builder.rb | 19 +++++++++++++++++++ .../order_default_tabs_builder_spec.rb | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/models/spree/admin/resources/order_default_tabs_builder.rb b/app/models/spree/admin/resources/order_default_tabs_builder.rb index b99d5f791f..94251f6f8d 100644 --- a/app/models/spree/admin/resources/order_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/order_default_tabs_builder.rb @@ -15,6 +15,7 @@ def build add_return_authorizations_tab(root) add_customer_returns_tab(root) add_state_changes_tab(root) + root end private @@ -34,6 +35,8 @@ def add_cart_tab(root) ability.can?(:update, resource) && (resource.shipments.size.zero? || resource.shipments.shipped.size.zero?) end ) + + root.add(tab) end def add_channel_tab(root) @@ -45,6 +48,8 @@ def add_channel_tab(root) ) .with_active_check .with_update_availability_check + + root.add(tab) end def add_customer_tab(root) @@ -60,6 +65,8 @@ def add_customer_tab(root) ability.can?(:update, resource) && resource.checkout_steps.include?("address") end ) + + root.add(tab) end def add_shipments_tab(root) @@ -71,6 +78,8 @@ def add_shipments_tab(root) ) .with_active_check .with_update_availability_check + + root.add(tab) end def add_adjustments_tab(root) @@ -82,6 +91,8 @@ def add_adjustments_tab(root) ) .with_active_check .with_index_availability_check(Spree::Adjustment) + + root.add(tab) end def add_payments_tab(root) @@ -93,6 +104,8 @@ def add_payments_tab(root) ) .with_active_check .with_index_availability_check(Spree::Payment) + + root.add(tab) end def add_return_authorizations_tab(root) @@ -104,6 +117,8 @@ def add_return_authorizations_tab(root) ) .with_active_check .with_index_availability_check(Spree::ReturnAuthorization) + + root.add(tab) end def add_customer_returns_tab(root) @@ -115,6 +130,8 @@ def add_customer_returns_tab(root) ) .with_active_check .with_index_availability_check(Spree::CustomerReturn) + + root.add(tab) end def add_state_changes_tab(root) @@ -126,6 +143,8 @@ def add_state_changes_tab(root) ) .with_active_check .with_update_availability_check + + root.add(tab) end end end diff --git a/spec/models/spree/admin/resources/order_default_tabs_builder_spec.rb b/spec/models/spree/admin/resources/order_default_tabs_builder_spec.rb index bef2b8de0a..58e6911b0d 100644 --- a/spec/models/spree/admin/resources/order_default_tabs_builder_spec.rb +++ b/spec/models/spree/admin/resources/order_default_tabs_builder_spec.rb @@ -5,7 +5,7 @@ module Admin describe Resources::OrderDefaultTabsBuilder, type: :model do let(:builder) { described_class.new } let(:default_tabs) do - %w(cart + %i(cart channel customer shipments From d35cfff386f9b7a643a383e192f82885fd482c84 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 27 Sep 2023 19:10:29 +0200 Subject: [PATCH 30/98] Fix variable naming --- .../spree/admin/resources/availability_builder_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/spree/admin/resources/availability_builder_methods.rb b/app/models/spree/admin/resources/availability_builder_methods.rb index a78f6bba11..af47f99340 100644 --- a/app/models/spree/admin/resources/availability_builder_methods.rb +++ b/app/models/spree/admin/resources/availability_builder_methods.rb @@ -13,7 +13,7 @@ def with_update_availability_check end def with_index_availability_check(klass) - @availability_check = ->(ability, _resource) { ability.can?(:index, klaas) } + @availability_check = ->(ability, _resource) { ability.can?(:index, klass) } self end end From 456365796e42eb95e083c5d23a10b84a454001f7 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 27 Sep 2023 19:30:29 +0200 Subject: [PATCH 31/98] Lint fixes --- .../resources/order_default_tabs_builder.rb | 92 +++++++++---------- app/models/spree/admin/resources/tab.rb | 4 +- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/app/models/spree/admin/resources/order_default_tabs_builder.rb b/app/models/spree/admin/resources/order_default_tabs_builder.rb index 94251f6f8d..8752d17af9 100644 --- a/app/models/spree/admin/resources/order_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/order_default_tabs_builder.rb @@ -25,16 +25,16 @@ def add_cart_tab(root) 'cart-check.svg', :cart, ->(resource) { cart_admin_order_url(resource) }, - 'nav-link', - ) - .with_active_check - .with_availability_check( - # An abstract module should not be aware of resource's internal structure. - # If these checks are elaborate, it's better to have this complexity declared explicitly here. - ->(ability, resource) do - ability.can?(:update, resource) && (resource.shipments.size.zero? || resource.shipments.shipped.size.zero?) - end - ) + 'nav-link' + ). + with_active_check. + with_availability_check( + # An abstract module should not be aware of resource's internal structure. + # If these checks are elaborate, it's better to have this complexity declared explicitly here. + lambda do |ability, resource| + ability.can?(:update, resource) && (resource.shipments.empty? || resource.shipments.shipped.empty?) + end + ) root.add(tab) end @@ -44,10 +44,10 @@ def add_channel_tab(root) 'funnel.svg', :channel, ->(resource) { channel_admin_order_url(resource) }, - 'nav-link', - ) - .with_active_check - .with_update_availability_check + 'nav-link' + ). + with_active_check. + with_update_availability_check root.add(tab) end @@ -57,14 +57,14 @@ def add_customer_tab(root) 'person-lines-fill.svg', :customer, ->(resource) { admin_order_customer_url(resource) }, - 'nav-link', - ) - .with_active_check - .with_availability_check( - ->(ability, resource) do - ability.can?(:update, resource) && resource.checkout_steps.include?("address") - end - ) + 'nav-link' + ). + with_active_check. + with_availability_check( + lambda do |ability, resource| + ability.can?(:update, resource) && resource.checkout_steps.include?('address') + end + ) root.add(tab) end @@ -74,10 +74,10 @@ def add_shipments_tab(root) 'truck.svg', :shipments, ->(resource) { edit_admin_order_url(resource) }, - 'nav-link', - ) - .with_active_check - .with_update_availability_check + 'nav-link' + ). + with_active_check. + with_update_availability_check root.add(tab) end @@ -87,10 +87,10 @@ def add_adjustments_tab(root) 'adjust.svg', :adjustments, ->(resource) { admin_order_adjustments_url(resource) }, - 'nav-link', - ) - .with_active_check - .with_index_availability_check(Spree::Adjustment) + 'nav-link' + ). + with_active_check. + with_index_availability_check(Spree::Adjustment) root.add(tab) end @@ -100,10 +100,10 @@ def add_payments_tab(root) 'credit-card.svg', :payments, ->(resource) { admin_order_payments_url(resource) }, - 'nav-link', - ) - .with_active_check - .with_index_availability_check(Spree::Payment) + 'nav-link' + ). + with_active_check. + with_index_availability_check(Spree::Payment) root.add(tab) end @@ -113,10 +113,10 @@ def add_return_authorizations_tab(root) 'enter.svg', :return_authorizations, ->(resource) { admin_order_return_authorizations_url(resource) }, - 'nav-link', - ) - .with_active_check - .with_index_availability_check(Spree::ReturnAuthorization) + 'nav-link' + ). + with_active_check. + with_index_availability_check(Spree::ReturnAuthorization) root.add(tab) end @@ -126,10 +126,10 @@ def add_customer_returns_tab(root) 'returns.svg', :customer_returns, ->(resource) { admin_order_customer_returns_url(resource) }, - 'nav-link', - ) - .with_active_check - .with_index_availability_check(Spree::CustomerReturn) + 'nav-link' + ). + with_active_check. + with_index_availability_check(Spree::CustomerReturn) root.add(tab) end @@ -139,10 +139,10 @@ def add_state_changes_tab(root) 'calendar3.svg', :state_changes, ->(resource) { admin_order_state_changes_url(resource) }, - 'nav-link', - ) - .with_active_check - .with_update_availability_check + 'nav-link' + ). + with_active_check. + with_update_availability_check root.add(tab) end diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb index e4151a0b13..ff80869e0f 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/resources/tab.rb @@ -5,9 +5,9 @@ class Tab include ConditionalChecker include AvailabilityBuilderMethods - attr_reader :icon_name, :text, :classes + attr_reader :icon_name, :text, :classes - def initialize(icon_name, text, url, classes, options = {}) + def initialize(icon_name, text, url, classes) @icon_name = icon_name @text = text @url = url From a70061034a2500efbfe2853d162a7cc0c2c20bd6 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Thu, 28 Sep 2023 07:43:23 +0200 Subject: [PATCH 32/98] Lint fixes --- .../resources/order_default_tabs_builder.rb | 117 ++++++++++-------- 1 file changed, 63 insertions(+), 54 deletions(-) diff --git a/app/models/spree/admin/resources/order_default_tabs_builder.rb b/app/models/spree/admin/resources/order_default_tabs_builder.rb index 8752d17af9..836503fd5a 100644 --- a/app/models/spree/admin/resources/order_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/order_default_tabs_builder.rb @@ -21,12 +21,13 @@ def build private def add_cart_tab(root) - tab = Tab.new( - 'cart-check.svg', - :cart, - ->(resource) { cart_admin_order_url(resource) }, - 'nav-link' - ). + tab = + Tab.new( + 'cart-check.svg', + :cart, + ->(resource) { cart_admin_order_url(resource) }, + 'nav-link' + ). with_active_check. with_availability_check( # An abstract module should not be aware of resource's internal structure. @@ -40,12 +41,13 @@ def add_cart_tab(root) end def add_channel_tab(root) - tab = Tab.new( - 'funnel.svg', - :channel, - ->(resource) { channel_admin_order_url(resource) }, - 'nav-link' - ). + tab = + Tab.new( + 'funnel.svg', + :channel, + ->(resource) { channel_admin_order_url(resource) }, + 'nav-link' + ). with_active_check. with_update_availability_check @@ -53,12 +55,13 @@ def add_channel_tab(root) end def add_customer_tab(root) - tab = Tab.new( - 'person-lines-fill.svg', - :customer, - ->(resource) { admin_order_customer_url(resource) }, - 'nav-link' - ). + tab = + Tab.new( + 'person-lines-fill.svg', + :customer, + ->(resource) { admin_order_customer_url(resource) }, + 'nav-link' + ). with_active_check. with_availability_check( lambda do |ability, resource| @@ -70,12 +73,13 @@ def add_customer_tab(root) end def add_shipments_tab(root) - tab = Tab.new( - 'truck.svg', - :shipments, - ->(resource) { edit_admin_order_url(resource) }, - 'nav-link' - ). + tab = + Tab.new( + 'truck.svg', + :shipments, + ->(resource) { edit_admin_order_url(resource) }, + 'nav-link' + ). with_active_check. with_update_availability_check @@ -83,12 +87,13 @@ def add_shipments_tab(root) end def add_adjustments_tab(root) - tab = Tab.new( - 'adjust.svg', - :adjustments, - ->(resource) { admin_order_adjustments_url(resource) }, - 'nav-link' - ). + tab = + Tab.new( + 'adjust.svg', + :adjustments, + ->(resource) { admin_order_adjustments_url(resource) }, + 'nav-link' + ). with_active_check. with_index_availability_check(Spree::Adjustment) @@ -96,12 +101,13 @@ def add_adjustments_tab(root) end def add_payments_tab(root) - tab = Tab.new( - 'credit-card.svg', - :payments, - ->(resource) { admin_order_payments_url(resource) }, - 'nav-link' - ). + tab = + Tab.new( + 'credit-card.svg', + :payments, + ->(resource) { admin_order_payments_url(resource) }, + 'nav-link' + ). with_active_check. with_index_availability_check(Spree::Payment) @@ -109,12 +115,13 @@ def add_payments_tab(root) end def add_return_authorizations_tab(root) - tab = Tab.new( - 'enter.svg', - :return_authorizations, - ->(resource) { admin_order_return_authorizations_url(resource) }, - 'nav-link' - ). + tab = + Tab.new( + 'enter.svg', + :return_authorizations, + ->(resource) { admin_order_return_authorizations_url(resource) }, + 'nav-link' + ). with_active_check. with_index_availability_check(Spree::ReturnAuthorization) @@ -122,12 +129,13 @@ def add_return_authorizations_tab(root) end def add_customer_returns_tab(root) - tab = Tab.new( - 'returns.svg', - :customer_returns, - ->(resource) { admin_order_customer_returns_url(resource) }, - 'nav-link' - ). + tab = + Tab.new( + 'returns.svg', + :customer_returns, + ->(resource) { admin_order_customer_returns_url(resource) }, + 'nav-link' + ). with_active_check. with_index_availability_check(Spree::CustomerReturn) @@ -135,12 +143,13 @@ def add_customer_returns_tab(root) end def add_state_changes_tab(root) - tab = Tab.new( - 'calendar3.svg', - :state_changes, - ->(resource) { admin_order_state_changes_url(resource) }, - 'nav-link' - ). + tab = + Tab.new( + 'calendar3.svg', + :state_changes, + ->(resource) { admin_order_state_changes_url(resource) }, + 'nav-link' + ). with_active_check. with_update_availability_check From 4833d378b07080ce8fd9b699e73257bd49f1f090 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Thu, 28 Sep 2023 09:05:13 +0200 Subject: [PATCH 33/98] Build admin order_tabs upon initialization --- app/helpers/spree/admin/navigation_helper.rb | 4 ++++ lib/spree/backend/engine.rb | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/helpers/spree/admin/navigation_helper.rb b/app/helpers/spree/admin/navigation_helper.rb index 7da7eba5a9..fa35ef01dd 100644 --- a/app/helpers/spree/admin/navigation_helper.rb +++ b/app/helpers/spree/admin/navigation_helper.rb @@ -308,6 +308,10 @@ def page_header_back_button(url) def main_menu Rails.application.config.spree_backend.main_menu end + + def order_tabs + Rails.application.config.spree_backend.order_tabs + end end end end diff --git a/lib/spree/backend/engine.rb b/lib/spree/backend/engine.rb index 9b4aa681f4..5b5076349f 100644 --- a/lib/spree/backend/engine.rb +++ b/lib/spree/backend/engine.rb @@ -3,7 +3,7 @@ module Spree module Backend class Engine < ::Rails::Engine - Environment = Struct.new(:main_menu) + Environment = Struct.new(:main_menu, :order_tabs) config.middleware.use 'Spree::Backend::Middleware::SeoAssist' @@ -27,6 +27,7 @@ class Engine < ::Rails::Engine config.after_initialize do Rails.application.reload_routes! Rails.application.config.spree_backend.main_menu = Spree::Admin::MainMenu::DefaultConfigurationBuilder.new.build + Rails.application.config.spree_backend.order_tabs = Spree::Admin::Resources::OrderDefaultTabsBuilder.new.build end end end From 2293024bc4a65d81b16a7f8f0e46be3885852464 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Thu, 28 Sep 2023 10:51:33 +0200 Subject: [PATCH 34/98] Use order_tabs defined on boot --- .../spree/admin/shared/_order_tabs.html.erb | 89 ++----------------- 1 file changed, 8 insertions(+), 81 deletions(-) diff --git a/app/views/spree/admin/shared/_order_tabs.html.erb b/app/views/spree/admin/shared/_order_tabs.html.erb index cddac38107..d4fc6698b5 100644 --- a/app/views/spree/admin/shared/_order_tabs.html.erb +++ b/app/views/spree/admin/shared/_order_tabs.html.erb @@ -4,88 +4,15 @@ <% end %> <% content_for :page_tabs do %> - <% if ((can? :update, @order) && (@order.shipments.size.zero? || @order.shipments.shipped.size.zero?)) %> - - <% end %> - - <% if can? :update, @order %> - - <% end %> - - <% if can?(:update, @order) && @order.checkout_steps.include?("address") %> - - <% end %> - - <% if can? :update, @order %> - - <% end %> - - <% if can? :index, Spree::Adjustment %> - - <% end %> - - <% if can?(:index, Spree::Payment) %> - - <% end %> - - <% if can? :index, Spree::ReturnAuthorization %> - <% if @order.completed? %> - - <% end %> - <% end %> - - <% if can? :index, Spree::CustomerReturn %> - <% if @order.completed? %> + <% order_tabs.items.each do |tab| %> + <% next unless tab.available?(current_ability, @order) %> - <% end %> - <% end %> - - <% if can? :update, @order %> - <% end %> <% end %> From 4ac6845a596852eb164125ef2a0f25f4e494386c Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Thu, 28 Sep 2023 13:08:10 +0200 Subject: [PATCH 35/98] Fix - pass correct argument --- app/views/spree/admin/shared/_order_tabs.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/spree/admin/shared/_order_tabs.html.erb b/app/views/spree/admin/shared/_order_tabs.html.erb index d4fc6698b5..5b0460022b 100644 --- a/app/views/spree/admin/shared/_order_tabs.html.erb +++ b/app/views/spree/admin/shared/_order_tabs.html.erb @@ -10,7 +10,7 @@ <%= link_to_with_icon( tab.icon_name, Spree.t(tab.text), - tab.url(@resource), + tab.url(@order), class: tab.active?(current) ? tab.classes.concat(' active') : tab.classes ) %> From 08018a9372c4595a7cb612749c46a3b62109c7f8 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Thu, 28 Sep 2023 15:58:30 +0200 Subject: [PATCH 36/98] Rename text to name --- app/models/spree/admin/resources/root.rb | 6 +++--- app/models/spree/admin/resources/tab.rb | 8 ++++---- app/views/spree/admin/shared/_order_tabs.html.erb | 2 +- .../resources/order_default_tabs_builder_spec.rb | 2 +- spec/models/spree/admin/resources/root_spec.rb | 2 +- spec/models/spree/admin/resources/tab_spec.rb | 14 +++++++------- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/app/models/spree/admin/resources/root.rb b/app/models/spree/admin/resources/root.rb index 8507b89416..6ff0e96dcb 100644 --- a/app/models/spree/admin/resources/root.rb +++ b/app/models/spree/admin/resources/root.rb @@ -9,15 +9,15 @@ def initialize end def add(item) - raise KeyError, "Item with key #{item.text} already exists" if index_for_text(item.text) + raise KeyError, "Item with key #{item.name} already exists" if index_for_name(item.name) @items << item end private - def index_for_text(text) - @items.index { |e| e.text == text } + def index_for_name(name) + @items.index { |e| e.name == name } end end end diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb index ff80869e0f..63bb32d63c 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/resources/tab.rb @@ -5,11 +5,11 @@ class Tab include ConditionalChecker include AvailabilityBuilderMethods - attr_reader :icon_name, :text, :classes + attr_reader :icon_name, :name, :classes - def initialize(icon_name, text, url, classes) + def initialize(icon_name, name, url, classes) @icon_name = icon_name - @text = text + @name = name @url = url @classes = classes end @@ -25,7 +25,7 @@ def url(resource = nil) end def active?(current_tab) - @active_check.call(current_tab, text) + @active_check.call(current_tab, name) end def complete?(resource) diff --git a/app/views/spree/admin/shared/_order_tabs.html.erb b/app/views/spree/admin/shared/_order_tabs.html.erb index 5b0460022b..1b1bcb8f89 100644 --- a/app/views/spree/admin/shared/_order_tabs.html.erb +++ b/app/views/spree/admin/shared/_order_tabs.html.erb @@ -9,7 +9,7 @@ <% end %> From 6bb3ddf19fde9b285d8e0d7fcbbc70784d47f783 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Fri, 29 Sep 2023 12:20:40 +0200 Subject: [PATCH 40/98] Migrate user tabs from view to separate class --- app/helpers/spree/admin/navigation_helper.rb | 4 + .../resources/user_default_tabs_builder.rb | 91 +++++++++++++++++++ app/views/spree/admin/users/_tabs.html.erb | 41 +++------ lib/spree/backend/engine.rb | 3 +- .../user_default_tabs_builder_spec.rb | 26 ++++++ 5 files changed, 134 insertions(+), 31 deletions(-) create mode 100644 app/models/spree/admin/resources/user_default_tabs_builder.rb create mode 100644 spec/models/spree/admin/resources/user_default_tabs_builder_spec.rb diff --git a/app/helpers/spree/admin/navigation_helper.rb b/app/helpers/spree/admin/navigation_helper.rb index fa35ef01dd..5d4b34b96c 100644 --- a/app/helpers/spree/admin/navigation_helper.rb +++ b/app/helpers/spree/admin/navigation_helper.rb @@ -312,6 +312,10 @@ def main_menu def order_tabs Rails.application.config.spree_backend.order_tabs end + + def user_tabs + Rails.application.config.spree_backend.user_tabs + end end end end diff --git a/app/models/spree/admin/resources/user_default_tabs_builder.rb b/app/models/spree/admin/resources/user_default_tabs_builder.rb new file mode 100644 index 0000000000..d4735e0709 --- /dev/null +++ b/app/models/spree/admin/resources/user_default_tabs_builder.rb @@ -0,0 +1,91 @@ +module Spree + module Admin + module Resources + class UserDefaultTabsBuilder + include Spree::Core::Engine.routes.url_helpers + + def build + root = Root.new + add_account_tab(root) + add_addresses_tab(root) + add_orders_tab(root) + add_items_tab(root) + add_store_credits_tab(root) + root + end + + private + + def add_account_tab(root) + tab = + Tab.new( + 'person.svg', + :"admin.user.account", + ->(resource) { edit_admin_user_path(resource) }, + 'nav-link' + ). + with_active_check. + with_default_translator + + root.add(tab) + end + + def add_addresses_tab(root) + tab = + Tab.new( + 'pin-map.svg', + :"admin.user.addresses", + ->(resource) { addresses_admin_user_path(resource) }, + 'nav-link' + ). + with_active_check. + with_default_translator + + root.add(tab) + end + + def add_orders_tab(root) + tab = + Tab.new( + 'inbox.svg', + :"admin.user.orders", + ->(resource) { orders_admin_user_path(resource) }, + 'nav-link' + ). + with_active_check. + with_default_translator + + root.add(tab) + end + + def add_items_tab(root) + tab = + Tab.new( + 'tag.svg', + :"admin.user.items", + ->(resource) { items_admin_user_path(resource) }, + 'nav-link' + ). + with_active_check. + with_default_translator + + root.add(tab) + end + + def add_store_credits_tab(root) + tab = + Tab.new( + 'gift.svg', + :"admin.user.store_credits", + ->(resource) { admin_user_store_credits_path(resource) }, + 'nav-link' + ). + with_active_check. + with_default_translator + + root.add(tab) + end + end + end + end +end diff --git a/app/views/spree/admin/users/_tabs.html.erb b/app/views/spree/admin/users/_tabs.html.erb index ce2832dc36..19b961e879 100644 --- a/app/views/spree/admin/users/_tabs.html.erb +++ b/app/views/spree/admin/users/_tabs.html.erb @@ -4,34 +4,15 @@ <% end %> <% content_for :page_tabs do %> - - - - - + <% user_tabs.items.each do |tab| %> + <% next unless tab.available?(current_ability, @user) %> + + <% end %> <% end %> diff --git a/lib/spree/backend/engine.rb b/lib/spree/backend/engine.rb index 5b5076349f..8c7d0cd74a 100644 --- a/lib/spree/backend/engine.rb +++ b/lib/spree/backend/engine.rb @@ -3,7 +3,7 @@ module Spree module Backend class Engine < ::Rails::Engine - Environment = Struct.new(:main_menu, :order_tabs) + Environment = Struct.new(:main_menu, :order_tabs, :user_tabs) config.middleware.use 'Spree::Backend::Middleware::SeoAssist' @@ -28,6 +28,7 @@ class Engine < ::Rails::Engine Rails.application.reload_routes! Rails.application.config.spree_backend.main_menu = Spree::Admin::MainMenu::DefaultConfigurationBuilder.new.build Rails.application.config.spree_backend.order_tabs = Spree::Admin::Resources::OrderDefaultTabsBuilder.new.build + Rails.application.config.spree_backend.user_tabs = Spree::Admin::Resources::UserDefaultTabsBuilder.new.build end end end diff --git a/spec/models/spree/admin/resources/user_default_tabs_builder_spec.rb b/spec/models/spree/admin/resources/user_default_tabs_builder_spec.rb new file mode 100644 index 0000000000..2317a4d1df --- /dev/null +++ b/spec/models/spree/admin/resources/user_default_tabs_builder_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +module Spree + module Admin + describe Resources::UserDefaultTabsBuilder, type: :model do + let(:builder) { described_class.new } + let(:default_tabs) do + [:"admin.user.account", + :"admin.user.addresses", + :"admin.user.orders", + :"admin.user.items", + :"admin.user.store_credits"] + end + + describe '#build' do + subject { builder.build } + + it 'builds default tabs' do + # this means that tab items will need to respond to 'text' message, + # just as section items respond to 'key' message + expect(subject.items.map(&:name)).to match(default_tabs) + end + end + end + end +end From e65ea17e5f5513ef61577609226d393390cd1fd6 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Fri, 29 Sep 2023 14:20:58 +0200 Subject: [PATCH 41/98] Migrate product tabs from view to separate class --- app/helpers/spree/admin/navigation_helper.rb | 4 + .../resources/availability_builder_methods.rb | 5 + .../resources/product_default_tabs_builder.rb | 172 ++++++++++++++++++ .../spree/admin/shared/_product_tabs.html.erb | 76 ++------ lib/spree/backend/engine.rb | 3 +- .../product_default_tabs_builder_spec.rb | 29 +++ 6 files changed, 224 insertions(+), 65 deletions(-) create mode 100644 app/models/spree/admin/resources/product_default_tabs_builder.rb create mode 100644 spec/models/spree/admin/resources/product_default_tabs_builder_spec.rb diff --git a/app/helpers/spree/admin/navigation_helper.rb b/app/helpers/spree/admin/navigation_helper.rb index 5d4b34b96c..e9cfb0eba0 100644 --- a/app/helpers/spree/admin/navigation_helper.rb +++ b/app/helpers/spree/admin/navigation_helper.rb @@ -316,6 +316,10 @@ def order_tabs def user_tabs Rails.application.config.spree_backend.user_tabs end + + def product_tabs + Rails.application.config.spree_backend.product_tabs + end end end end diff --git a/app/models/spree/admin/resources/availability_builder_methods.rb b/app/models/spree/admin/resources/availability_builder_methods.rb index af47f99340..79002f5dd9 100644 --- a/app/models/spree/admin/resources/availability_builder_methods.rb +++ b/app/models/spree/admin/resources/availability_builder_methods.rb @@ -16,6 +16,11 @@ def with_index_availability_check(klass) @availability_check = ->(ability, _resource) { ability.can?(:index, klass) } self end + + def with_admin_availability_check(klass) + @availability_check = ->(ability, _resource) { ability.can?(:admin, klass) } + self + end end end end diff --git a/app/models/spree/admin/resources/product_default_tabs_builder.rb b/app/models/spree/admin/resources/product_default_tabs_builder.rb new file mode 100644 index 0000000000..41304e0f19 --- /dev/null +++ b/app/models/spree/admin/resources/product_default_tabs_builder.rb @@ -0,0 +1,172 @@ +module Spree + module Admin + module Resources + class ProductDefaultTabsBuilder + include Spree::Core::Engine.routes.url_helpers + + def build + root = Root.new + add_details_tab(root) + add_images_tab(root) + add_variants_tab(root) + add_properties_tab(root) + add_stock_tab(root) + add_prices_tab(root) + add_digitals_tab(root) + add_translations_tab(root) + root + end + + private + + def add_details_tab(root) + tab = + Tab.new( + 'edit.svg', + :details, + ->(resource) { edit_admin_product_path(resource) }, + 'nav-link' + ). + with_active_check. + with_default_translator. + with_admin_availability_check(Spree::Product) + + root.add(tab) + end + + def add_images_tab(root) + tab = + Tab.new( + 'images.svg', + :images, + ->(resource) { admin_product_images_path(resource) }, + 'nav-link' + ). + with_active_check. + with_default_translator. + with_availability_check( + lambda do |ability, resource| + ability.can?(:admin, Spree::Image) && !resource.deleted? + end + ) + + root.add(tab) + end + + def add_variants_tab(root) + tab = + Tab.new( + 'adjust.svg', + :variants, + ->(resource) { admin_product_variants_path(resource) }, + 'nav-link' + ). + with_active_check. + with_default_translator. + with_availability_check( + lambda do |ability, resource| + ability.can?(:admin, Spree::Variant) && !resource.deleted? + end + ) + + root.add(tab) + end + + def add_properties_tab(root) + tab = + Tab.new( + 'list.svg', + :properties, + ->(resource) { admin_product_product_properties_path(resource) }, + 'nav-link' + ). + with_active_check. + with_default_translator. + with_availability_check( + lambda do |ability, resource| + ability.can?(:admin, Spree::ProductProperty) && !resource.deleted? + end + ) + + root.add(tab) + end + + def add_stock_tab(root) + tab = + Tab.new( + 'box-seam.svg', + :stock, + ->(resource) { stock_admin_product_path(resource) }, + 'nav-link' + ). + with_active_check. + with_default_translator. + with_availability_check( + lambda do |ability, resource| + ability.can?(:admin, Spree::StockItem) && !resource.deleted? + end + ) + + root.add(tab) + end + + def add_prices_tab(root) + tab = + Tab.new( + 'currency-exchange.svg', + :prices, + ->(resource) { admin_product_prices_path(resource) }, + 'nav-link' + ). + with_active_check. + with_default_translator. + with_availability_check( + lambda do |ability, resource| + ability.can?(:admin, Spree::Price) && !resource.deleted? + end + ) + + root.add(tab) + end + + def add_digitals_tab(root) + tab = + Tab.new( + 'download.svg', + 'admin.digitals.digital_assets', + ->(resource) { admin_product_digitals_path(resource) }, + 'nav-link' + ). + with_active_check. + with_default_translator. + with_availability_check( + lambda do |ability, resource| + ability.can?(:admin, Spree::Digital) && !resource.deleted? + end + ) + + root.add(tab) + end + + def add_translations_tab(root) + tab = + Tab.new( + 'translate.svg', + :translations, + ->(resource) { translations_admin_product_path(resource) }, + 'nav-link' + ). + with_active_check. + with_default_translator. + with_availability_check( + lambda do |ability, resource| + ability.can?(:admin, Spree::Product) && !resource.deleted? + end + ) + + root.add(tab) + end + end + end + end +end diff --git a/app/views/spree/admin/shared/_product_tabs.html.erb b/app/views/spree/admin/shared/_product_tabs.html.erb index df9baaa7dc..812fd4f123 100644 --- a/app/views/spree/admin/shared/_product_tabs.html.erb +++ b/app/views/spree/admin/shared/_product_tabs.html.erb @@ -3,68 +3,16 @@ <%= @product.name %> <% end %> -<% content_for(:page_tabs) do %> - <%= content_tag :li, class: 'nav-item' do %> - <%= link_to_with_icon 'edit.svg', - Spree.t(:details), - edit_admin_product_url(@product), - class: "nav-link #{'active' if current == :details}" %> - - <% end if can?(:admin, Spree::Product) %> - - <%= content_tag :li, class: 'nav-item' do %> - <%= link_to_with_icon 'images.svg', - Spree.t(:images), - spree.admin_product_images_url(@product), - class: "nav-link #{'active' if current == :images}" %> - - <% end if can?(:admin, Spree::Image) && !@product.deleted? %> - - <%= content_tag :li, class: 'nav-item' do %> - <%= link_to_with_icon 'adjust.svg', - Spree.t(:variants), - spree.admin_product_variants_url(@product), - class: "nav-link #{'active' if current == :variants}" %> - - <% end if can?(:admin, Spree::Variant) && !@product.deleted? %> - - <%= content_tag :li, class: 'nav-item' do %> - <%= link_to_with_icon 'list.svg', - Spree.t(:properties), - spree.admin_product_product_properties_url(@product), - class: "nav-link #{'active' if current == :properties}" %> - - <% end if can?(:admin, Spree::ProductProperty) && !@product.deleted? %> - - <%= content_tag :li, class: 'nav-item' do %> - <%= link_to_with_icon 'box-seam.svg', - Spree.t(:stock), - stock_admin_product_url(@product), - class: "nav-link #{'active' if current == :stock}" %> - - <% end if can?(:admin, Spree::StockItem) && !@product.deleted? %> - - <%= content_tag :li, class: 'nav-item' do %> - <%= link_to_with_icon 'currency-exchange.svg', - Spree.t(:prices), - admin_product_prices_path(@product), - class: "nav-link #{'active' if current == :prices}" %> - - <% end if can?(:admin, Spree::Price) && !@product.deleted? %> - - <%= content_tag :li, class: 'nav-item' do %> - <%= link_to_with_icon 'download.svg', - Spree.t('admin.digitals.digital_assets'), - admin_product_digitals_path(@product), - class: "nav-link #{'active' if current == :digitals} #{current}" %> - - <% end if can?(:admin, Spree::Digital) && !@product.deleted? %> - - <%= content_tag :li, class: 'nav-item' do %> - <%= link_to_with_icon 'translate.svg', - Spree.t(:translations), - translations_admin_product_path(@product), - class: "nav-link #{'active' if current == :translations}" %> - - <% end if can?(:admin, Spree::Product) && !@product.deleted? %> +<% content_for :page_tabs do %> + <% product_tabs.items.each do |tab| %> + <% next unless tab.available?(current_ability, @product) %> + + <% end %> <% end %> diff --git a/lib/spree/backend/engine.rb b/lib/spree/backend/engine.rb index 8c7d0cd74a..5dab344fbf 100644 --- a/lib/spree/backend/engine.rb +++ b/lib/spree/backend/engine.rb @@ -3,7 +3,7 @@ module Spree module Backend class Engine < ::Rails::Engine - Environment = Struct.new(:main_menu, :order_tabs, :user_tabs) + Environment = Struct.new(:main_menu, :order_tabs, :user_tabs, :product_tabs) config.middleware.use 'Spree::Backend::Middleware::SeoAssist' @@ -29,6 +29,7 @@ class Engine < ::Rails::Engine Rails.application.config.spree_backend.main_menu = Spree::Admin::MainMenu::DefaultConfigurationBuilder.new.build Rails.application.config.spree_backend.order_tabs = Spree::Admin::Resources::OrderDefaultTabsBuilder.new.build Rails.application.config.spree_backend.user_tabs = Spree::Admin::Resources::UserDefaultTabsBuilder.new.build + Rails.application.config.spree_backend.product_tabs = Spree::Admin::Resources::ProductDefaultTabsBuilder.new.build end end end diff --git a/spec/models/spree/admin/resources/product_default_tabs_builder_spec.rb b/spec/models/spree/admin/resources/product_default_tabs_builder_spec.rb new file mode 100644 index 0000000000..fa8ee94666 --- /dev/null +++ b/spec/models/spree/admin/resources/product_default_tabs_builder_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +module Spree + module Admin + describe Resources::ProductDefaultTabsBuilder, type: :model do + let(:builder) { described_class.new } + let(:default_tabs) do + [:details, + :images, + :variants, + :properties, + :stock, + :prices, + 'admin.digitals.digital_assets', + :translations] + end + + describe '#build' do + subject { builder.build } + + it 'builds default tabs' do + # this means that tab items will need to respond to 'text' message, + # just as section items respond to 'key' message + expect(subject.items.map(&:name)).to match(default_tabs) + end + end + end + end +end From 5cc8bc32a5a31501200bf4dd18565ffde8499bb3 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Fri, 29 Sep 2023 20:33:01 +0200 Subject: [PATCH 42/98] Disable variables set up in the engine --- lib/spree/backend/engine.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/spree/backend/engine.rb b/lib/spree/backend/engine.rb index 5dab344fbf..fd34ff90c8 100644 --- a/lib/spree/backend/engine.rb +++ b/lib/spree/backend/engine.rb @@ -27,9 +27,9 @@ class Engine < ::Rails::Engine config.after_initialize do Rails.application.reload_routes! Rails.application.config.spree_backend.main_menu = Spree::Admin::MainMenu::DefaultConfigurationBuilder.new.build - Rails.application.config.spree_backend.order_tabs = Spree::Admin::Resources::OrderDefaultTabsBuilder.new.build - Rails.application.config.spree_backend.user_tabs = Spree::Admin::Resources::UserDefaultTabsBuilder.new.build - Rails.application.config.spree_backend.product_tabs = Spree::Admin::Resources::ProductDefaultTabsBuilder.new.build + # Rails.application.config.spree_backend.order_tabs = Spree::Admin::Resources::OrderDefaultTabsBuilder.new.build + # Rails.application.config.spree_backend.user_tabs = Spree::Admin::Resources::UserDefaultTabsBuilder.new.build + # Rails.application.config.spree_backend.product_tabs = Spree::Admin::Resources::ProductDefaultTabsBuilder.new.build end end end From 57af58a40c333d05d5a13ec0945360ef9132508d Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Fri, 29 Sep 2023 20:40:19 +0200 Subject: [PATCH 43/98] Move attributes to config. Add partial_name. --- app/models/spree/admin/resources/tab.rb | 15 +++++++------- spec/models/spree/admin/resources/tab_spec.rb | 20 ++++++++++++++++++- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb index f7e350cf47..dc513ca587 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/resources/tab.rb @@ -6,13 +6,14 @@ class Tab include AvailabilityBuilderMethods include Translator - attr_reader :icon_name, :name, :classes - - def initialize(icon_name, name, url, classes) - @icon_name = icon_name - @name = name - @url = url - @classes = classes + attr_reader :icon_name, :name, :classes, :partial_name + + def initialize(config) + @icon_name = config[:icon_name] + @name = config[:name] + @url = config[:url] + @classes = config[:classes] + @partial_name = config[:partial_name] end def available?(current_ability, resource) diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/resources/tab_spec.rb index ba67f0db46..58d68f9ee4 100644 --- a/spec/models/spree/admin/resources/tab_spec.rb +++ b/spec/models/spree/admin/resources/tab_spec.rb @@ -3,11 +3,21 @@ module Spree module Admin describe Resources::Tab, type: :model do - let(:tab) { described_class.new(icon_name, name, url, classes) } + let(:tab) { described_class.new(config) } + let(:config) do + { + icon_name: icon_name, + name: name, + url: url, + classes: classes, + partial_name: partial_name + } + end let(:icon_name) { 'cart-check.svg' } let(:name) { 'Cart' } let(:url) { '/cart' } let(:classes) { 'nav-link' } + let(:partial_name) { :cart } describe '#icon_name' do subject { tab.icon_name } @@ -40,6 +50,14 @@ module Admin expect(subject).to eq(classes) end end + + describe '#partial_name' do + subject { tab.partial_name } + + it 'returns partial_name' do + expect(subject).to eq(partial_name) + end + end describe '#available?' do subject { tab.available?(ability, resource) } From a60a413800160bbaba6351c8b4aaa508be92d784 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Fri, 29 Sep 2023 21:09:07 +0200 Subject: [PATCH 44/98] Adjust default tab builders to use config --- .../resources/order_default_tabs_builder.rb | 90 +++++++++++-------- .../resources/product_default_tabs_builder.rb | 80 ++++++++++------- .../resources/user_default_tabs_builder.rb | 50 ++++++----- .../user_default_tabs_builder_spec.rb | 10 +-- 4 files changed, 137 insertions(+), 93 deletions(-) diff --git a/app/models/spree/admin/resources/order_default_tabs_builder.rb b/app/models/spree/admin/resources/order_default_tabs_builder.rb index b03616b4c4..e271e1456f 100644 --- a/app/models/spree/admin/resources/order_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/order_default_tabs_builder.rb @@ -23,10 +23,12 @@ def build def add_cart_tab(root) tab = Tab.new( - 'cart-check.svg', - :cart, - ->(resource) { cart_admin_order_path(resource) }, - 'nav-link' + { + icon_name: 'cart-check.svg', + name: :cart, + url: ->(resource) { cart_admin_order_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator. @@ -44,10 +46,12 @@ def add_cart_tab(root) def add_channel_tab(root) tab = Tab.new( - 'funnel.svg', - :channel, - ->(resource) { channel_admin_order_path(resource) }, - 'nav-link' + { + icon_name: 'funnel.svg', + name: :channel, + url: ->(resource) { channel_admin_order_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator. @@ -59,10 +63,12 @@ def add_channel_tab(root) def add_customer_tab(root) tab = Tab.new( - 'person-lines-fill.svg', - :customer, - ->(resource) { admin_order_customer_path(resource) }, - 'nav-link' + { + icon_name: 'person-lines-fill.svg', + name: :customer, + url: ->(resource) { admin_order_customer_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator. @@ -78,10 +84,12 @@ def add_customer_tab(root) def add_shipments_tab(root) tab = Tab.new( - 'truck.svg', - :shipments, - ->(resource) { edit_admin_order_path(resource) }, - 'nav-link' + { + icon_name: 'truck.svg', + name: :shipments, + url: ->(resource) { edit_admin_order_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator. @@ -93,10 +101,12 @@ def add_shipments_tab(root) def add_adjustments_tab(root) tab = Tab.new( - 'adjust.svg', - :adjustments, - ->(resource) { admin_order_adjustments_path(resource) }, - 'nav-link' + { + icon_name: 'adjust.svg', + name: :adjustments, + url: ->(resource) { admin_order_adjustments_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator. @@ -108,10 +118,12 @@ def add_adjustments_tab(root) def add_payments_tab(root) tab = Tab.new( - 'credit-card.svg', - :payments, - ->(resource) { admin_order_payments_path(resource) }, - 'nav-link' + { + icon_name: 'credit-card.svg', + name: :payments, + url: ->(resource) { admin_order_payments_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator. @@ -123,10 +135,12 @@ def add_payments_tab(root) def add_return_authorizations_tab(root) tab = Tab.new( - 'enter.svg', - :return_authorizations, - ->(resource) { admin_order_return_authorizations_path(resource) }, - 'nav-link' + { + icon_name: 'enter.svg', + name: :return_authorizations, + url: ->(resource) { admin_order_return_authorizations_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator. @@ -138,10 +152,12 @@ def add_return_authorizations_tab(root) def add_customer_returns_tab(root) tab = Tab.new( - 'returns.svg', - :customer_returns, - ->(resource) { admin_order_customer_returns_path(resource) }, - 'nav-link' + { + icon_name: 'returns.svg', + name: :customer_returns, + url: ->(resource) { admin_order_customer_returns_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator. @@ -153,10 +169,12 @@ def add_customer_returns_tab(root) def add_state_changes_tab(root) tab = Tab.new( - 'calendar3.svg', - :state_changes, - ->(resource) { admin_order_state_changes_path(resource) }, - 'nav-link' + { + icon_name: 'calendar3.svg', + name: :state_changes, + url: ->(resource) { admin_order_state_changes_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_custom_translator(::Spree::StateChange, :human_attribute_name). diff --git a/app/models/spree/admin/resources/product_default_tabs_builder.rb b/app/models/spree/admin/resources/product_default_tabs_builder.rb index 41304e0f19..beed8ce6f6 100644 --- a/app/models/spree/admin/resources/product_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/product_default_tabs_builder.rb @@ -22,10 +22,12 @@ def build def add_details_tab(root) tab = Tab.new( - 'edit.svg', - :details, - ->(resource) { edit_admin_product_path(resource) }, - 'nav-link' + { + icon_name: 'edit.svg', + name: :details, + url: ->(resource) { edit_admin_product_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator. @@ -37,10 +39,12 @@ def add_details_tab(root) def add_images_tab(root) tab = Tab.new( - 'images.svg', - :images, - ->(resource) { admin_product_images_path(resource) }, - 'nav-link' + { + icon_name: 'images.svg', + name: :images, + url: ->(resource) { admin_product_images_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator. @@ -56,10 +60,12 @@ def add_images_tab(root) def add_variants_tab(root) tab = Tab.new( - 'adjust.svg', - :variants, - ->(resource) { admin_product_variants_path(resource) }, - 'nav-link' + { + icon_name: 'adjust.svg', + name: :variants, + url: ->(resource) { admin_product_variants_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator. @@ -75,10 +81,12 @@ def add_variants_tab(root) def add_properties_tab(root) tab = Tab.new( - 'list.svg', - :properties, - ->(resource) { admin_product_product_properties_path(resource) }, - 'nav-link' + { + icon_name: 'list.svg', + name: :properties, + url: ->(resource) { admin_product_product_properties_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator. @@ -94,10 +102,12 @@ def add_properties_tab(root) def add_stock_tab(root) tab = Tab.new( - 'box-seam.svg', - :stock, - ->(resource) { stock_admin_product_path(resource) }, - 'nav-link' + { + icon_name: 'box-seam.svg', + name: :stock, + url: ->(resource) { stock_admin_product_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator. @@ -113,10 +123,12 @@ def add_stock_tab(root) def add_prices_tab(root) tab = Tab.new( - 'currency-exchange.svg', - :prices, - ->(resource) { admin_product_prices_path(resource) }, - 'nav-link' + { + icon_name: 'currency-exchange.svg', + name: :prices, + url: ->(resource) { admin_product_prices_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator. @@ -132,10 +144,12 @@ def add_prices_tab(root) def add_digitals_tab(root) tab = Tab.new( - 'download.svg', - 'admin.digitals.digital_assets', - ->(resource) { admin_product_digitals_path(resource) }, - 'nav-link' + { + icon_name: 'download.svg', + name: 'admin.digitals.digital_assets', + url: ->(resource) { admin_product_digitals_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator. @@ -151,10 +165,12 @@ def add_digitals_tab(root) def add_translations_tab(root) tab = Tab.new( - 'translate.svg', - :translations, - ->(resource) { translations_admin_product_path(resource) }, - 'nav-link' + { + icon_name: 'translate.svg', + name: :translations, + url: ->(resource) { translations_admin_product_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator. diff --git a/app/models/spree/admin/resources/user_default_tabs_builder.rb b/app/models/spree/admin/resources/user_default_tabs_builder.rb index d4735e0709..47cb67103f 100644 --- a/app/models/spree/admin/resources/user_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/user_default_tabs_builder.rb @@ -19,10 +19,12 @@ def build def add_account_tab(root) tab = Tab.new( - 'person.svg', - :"admin.user.account", - ->(resource) { edit_admin_user_path(resource) }, - 'nav-link' + { + icon_name: 'person.svg', + name: "admin.user.account", + url: ->(resource) { edit_admin_user_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator @@ -33,10 +35,12 @@ def add_account_tab(root) def add_addresses_tab(root) tab = Tab.new( - 'pin-map.svg', - :"admin.user.addresses", - ->(resource) { addresses_admin_user_path(resource) }, - 'nav-link' + { + icon_name: 'pin-map.svg', + name: "admin.user.addresses", + url: ->(resource) { addresses_admin_user_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator @@ -47,10 +51,12 @@ def add_addresses_tab(root) def add_orders_tab(root) tab = Tab.new( - 'inbox.svg', - :"admin.user.orders", - ->(resource) { orders_admin_user_path(resource) }, - 'nav-link' + { + icon_name: 'inbox.svg', + name: 'admin.user.orders', + url: ->(resource) { orders_admin_user_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator @@ -61,10 +67,12 @@ def add_orders_tab(root) def add_items_tab(root) tab = Tab.new( - 'tag.svg', - :"admin.user.items", - ->(resource) { items_admin_user_path(resource) }, - 'nav-link' + { + icon_name: 'tag.svg', + name: 'admin.user.items', + url: ->(resource) { items_admin_user_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator @@ -75,10 +83,12 @@ def add_items_tab(root) def add_store_credits_tab(root) tab = Tab.new( - 'gift.svg', - :"admin.user.store_credits", - ->(resource) { admin_user_store_credits_path(resource) }, - 'nav-link' + { + icon_name: 'gift.svg', + name: 'admin.user.store_credits', + url: ->(resource) { admin_user_store_credits_path(resource) }, + classes: 'nav-link' + } ). with_active_check. with_default_translator diff --git a/spec/models/spree/admin/resources/user_default_tabs_builder_spec.rb b/spec/models/spree/admin/resources/user_default_tabs_builder_spec.rb index 2317a4d1df..716d648636 100644 --- a/spec/models/spree/admin/resources/user_default_tabs_builder_spec.rb +++ b/spec/models/spree/admin/resources/user_default_tabs_builder_spec.rb @@ -5,11 +5,11 @@ module Admin describe Resources::UserDefaultTabsBuilder, type: :model do let(:builder) { described_class.new } let(:default_tabs) do - [:"admin.user.account", - :"admin.user.addresses", - :"admin.user.orders", - :"admin.user.items", - :"admin.user.store_credits"] + ['admin.user.account', + 'admin.user.addresses', + 'admin.user.orders', + 'admin.user.items', + 'admin.user.store_credits'] end describe '#build' do From e44c65d8bdb18ce06037bd4d0b1355c2e592be75 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Fri, 29 Sep 2023 21:19:43 +0200 Subject: [PATCH 45/98] Change back partial current attribute --- app/views/spree/admin/orders/customer_details/edit.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/spree/admin/orders/customer_details/edit.html.erb b/app/views/spree/admin/orders/customer_details/edit.html.erb index 24fbdb240f..adea338af2 100644 --- a/app/views/spree/admin/orders/customer_details/edit.html.erb +++ b/app/views/spree/admin/orders/customer_details/edit.html.erb @@ -1,4 +1,4 @@ -<%= render 'spree/admin/shared/order_tabs', current: :customer %> +<%= render 'spree/admin/shared/order_tabs', current: :customer_details %> <% if can? :edit, @order.user %>
From 5b001f18a525346e461654bdd9fa5f1387d273b8 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Fri, 29 Sep 2023 21:26:05 +0200 Subject: [PATCH 46/98] Supply configs in builders with partial_name --- .../resources/order_default_tabs_builder.rb | 27 ++++++++++++------- .../resources/product_default_tabs_builder.rb | 24 +++++++++++------ .../resources/user_default_tabs_builder.rb | 15 +++++++---- 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/app/models/spree/admin/resources/order_default_tabs_builder.rb b/app/models/spree/admin/resources/order_default_tabs_builder.rb index e271e1456f..bd7faed0f0 100644 --- a/app/models/spree/admin/resources/order_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/order_default_tabs_builder.rb @@ -27,7 +27,8 @@ def add_cart_tab(root) icon_name: 'cart-check.svg', name: :cart, url: ->(resource) { cart_admin_order_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :cart } ). with_active_check. @@ -50,7 +51,8 @@ def add_channel_tab(root) icon_name: 'funnel.svg', name: :channel, url: ->(resource) { channel_admin_order_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :channel } ). with_active_check. @@ -67,7 +69,8 @@ def add_customer_tab(root) icon_name: 'person-lines-fill.svg', name: :customer, url: ->(resource) { admin_order_customer_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :customer_details } ). with_active_check. @@ -88,7 +91,8 @@ def add_shipments_tab(root) icon_name: 'truck.svg', name: :shipments, url: ->(resource) { edit_admin_order_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :shipments } ). with_active_check. @@ -105,7 +109,8 @@ def add_adjustments_tab(root) icon_name: 'adjust.svg', name: :adjustments, url: ->(resource) { admin_order_adjustments_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :adjustments } ). with_active_check. @@ -122,7 +127,8 @@ def add_payments_tab(root) icon_name: 'credit-card.svg', name: :payments, url: ->(resource) { admin_order_payments_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :payments } ). with_active_check. @@ -139,7 +145,8 @@ def add_return_authorizations_tab(root) icon_name: 'enter.svg', name: :return_authorizations, url: ->(resource) { admin_order_return_authorizations_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :return_authorizations } ). with_active_check. @@ -156,7 +163,8 @@ def add_customer_returns_tab(root) icon_name: 'returns.svg', name: :customer_returns, url: ->(resource) { admin_order_customer_returns_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :customer_returns } ). with_active_check. @@ -173,7 +181,8 @@ def add_state_changes_tab(root) icon_name: 'calendar3.svg', name: :state_changes, url: ->(resource) { admin_order_state_changes_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :state_changes } ). with_active_check. diff --git a/app/models/spree/admin/resources/product_default_tabs_builder.rb b/app/models/spree/admin/resources/product_default_tabs_builder.rb index beed8ce6f6..b20b46821e 100644 --- a/app/models/spree/admin/resources/product_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/product_default_tabs_builder.rb @@ -26,7 +26,8 @@ def add_details_tab(root) icon_name: 'edit.svg', name: :details, url: ->(resource) { edit_admin_product_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :details } ). with_active_check. @@ -43,7 +44,8 @@ def add_images_tab(root) icon_name: 'images.svg', name: :images, url: ->(resource) { admin_product_images_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :images } ). with_active_check. @@ -64,7 +66,8 @@ def add_variants_tab(root) icon_name: 'adjust.svg', name: :variants, url: ->(resource) { admin_product_variants_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :variants } ). with_active_check. @@ -85,7 +88,8 @@ def add_properties_tab(root) icon_name: 'list.svg', name: :properties, url: ->(resource) { admin_product_product_properties_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :properties } ). with_active_check. @@ -106,7 +110,8 @@ def add_stock_tab(root) icon_name: 'box-seam.svg', name: :stock, url: ->(resource) { stock_admin_product_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :stock } ). with_active_check. @@ -127,7 +132,8 @@ def add_prices_tab(root) icon_name: 'currency-exchange.svg', name: :prices, url: ->(resource) { admin_product_prices_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :prices } ). with_active_check. @@ -148,7 +154,8 @@ def add_digitals_tab(root) icon_name: 'download.svg', name: 'admin.digitals.digital_assets', url: ->(resource) { admin_product_digitals_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :digitals } ). with_active_check. @@ -169,7 +176,8 @@ def add_translations_tab(root) icon_name: 'translate.svg', name: :translations, url: ->(resource) { translations_admin_product_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :translations } ). with_active_check. diff --git a/app/models/spree/admin/resources/user_default_tabs_builder.rb b/app/models/spree/admin/resources/user_default_tabs_builder.rb index 47cb67103f..2c76269d09 100644 --- a/app/models/spree/admin/resources/user_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/user_default_tabs_builder.rb @@ -23,7 +23,8 @@ def add_account_tab(root) icon_name: 'person.svg', name: "admin.user.account", url: ->(resource) { edit_admin_user_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :account } ). with_active_check. @@ -39,7 +40,8 @@ def add_addresses_tab(root) icon_name: 'pin-map.svg', name: "admin.user.addresses", url: ->(resource) { addresses_admin_user_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :address } ). with_active_check. @@ -55,7 +57,8 @@ def add_orders_tab(root) icon_name: 'inbox.svg', name: 'admin.user.orders', url: ->(resource) { orders_admin_user_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :orders } ). with_active_check. @@ -71,7 +74,8 @@ def add_items_tab(root) icon_name: 'tag.svg', name: 'admin.user.items', url: ->(resource) { items_admin_user_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :items } ). with_active_check. @@ -87,7 +91,8 @@ def add_store_credits_tab(root) icon_name: 'gift.svg', name: 'admin.user.store_credits', url: ->(resource) { admin_user_store_credits_path(resource) }, - classes: 'nav-link' + classes: 'nav-link', + partial_name: :store_credits } ). with_active_check. From f845daf952b2b7261256ea21948ff15090b00454 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Fri, 29 Sep 2023 21:34:10 +0200 Subject: [PATCH 47/98] Fix active check to use partial_name. Fix tab spec to use config. --- app/models/spree/admin/resources/tab.rb | 2 +- spec/models/spree/admin/resources/tab_spec.rb | 27 ++++++++----------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb index dc513ca587..ceae6da51e 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/resources/tab.rb @@ -27,7 +27,7 @@ def url(resource = nil) end def active?(current_tab) - @active_check.call(current_tab, name) + @active_check.call(current_tab, partial_name) end def complete?(resource) diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/resources/tab_spec.rb index 58d68f9ee4..4fa5eda0a3 100644 --- a/spec/models/spree/admin/resources/tab_spec.rb +++ b/spec/models/spree/admin/resources/tab_spec.rb @@ -6,24 +6,19 @@ module Admin let(:tab) { described_class.new(config) } let(:config) do { - icon_name: icon_name, - name: name, - url: url, - classes: classes, - partial_name: partial_name + icon_name: 'cart-check.svg', + name: 'Cart', + url: '/cart', + classes: 'nav-link', + partial_name: :cart } end - let(:icon_name) { 'cart-check.svg' } - let(:name) { 'Cart' } - let(:url) { '/cart' } - let(:classes) { 'nav-link' } - let(:partial_name) { :cart } describe '#icon_name' do subject { tab.icon_name } it 'returns icon_name' do - expect(subject).to eq(icon_name) + expect(subject).to eq(config[:icon_name]) end end @@ -31,7 +26,7 @@ module Admin subject { tab.name } it 'returns name' do - expect(subject).to eq(name) + expect(subject).to eq(config[:name]) end end @@ -39,7 +34,7 @@ module Admin subject { tab.url } it 'returns url' do - expect(subject).to eq(url) + expect(subject).to eq(config[:url]) end end @@ -47,7 +42,7 @@ module Admin subject { tab.classes } it 'returns classes' do - expect(subject).to eq(classes) + expect(subject).to eq(config[:classes]) end end @@ -55,7 +50,7 @@ module Admin subject { tab.partial_name } it 'returns partial_name' do - expect(subject).to eq(partial_name) + expect(subject).to eq(config[:partial_name]) end end @@ -98,7 +93,7 @@ module Admin before { tab.with_active_check } context 'when tab matches the current tab' do - let(:current_tab) { name } + let(:current_tab) { config[:partial_name] } it "returns true" do expect(subject).to be(true) From 235fb5659dddc6c8c9245722714969b254f4155a Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Fri, 29 Sep 2023 21:35:24 +0200 Subject: [PATCH 48/98] Enable variables set up in the engine --- lib/spree/backend/engine.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/spree/backend/engine.rb b/lib/spree/backend/engine.rb index fd34ff90c8..5dab344fbf 100644 --- a/lib/spree/backend/engine.rb +++ b/lib/spree/backend/engine.rb @@ -27,9 +27,9 @@ class Engine < ::Rails::Engine config.after_initialize do Rails.application.reload_routes! Rails.application.config.spree_backend.main_menu = Spree::Admin::MainMenu::DefaultConfigurationBuilder.new.build - # Rails.application.config.spree_backend.order_tabs = Spree::Admin::Resources::OrderDefaultTabsBuilder.new.build - # Rails.application.config.spree_backend.user_tabs = Spree::Admin::Resources::UserDefaultTabsBuilder.new.build - # Rails.application.config.spree_backend.product_tabs = Spree::Admin::Resources::ProductDefaultTabsBuilder.new.build + Rails.application.config.spree_backend.order_tabs = Spree::Admin::Resources::OrderDefaultTabsBuilder.new.build + Rails.application.config.spree_backend.user_tabs = Spree::Admin::Resources::UserDefaultTabsBuilder.new.build + Rails.application.config.spree_backend.product_tabs = Spree::Admin::Resources::ProductDefaultTabsBuilder.new.build end end end From 2e48d2e8022bc14bf648cc1037a85cd4383972bf Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Fri, 29 Sep 2023 21:48:21 +0200 Subject: [PATCH 49/98] Lint fix --- app/models/spree/admin/resources/user_default_tabs_builder.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/spree/admin/resources/user_default_tabs_builder.rb b/app/models/spree/admin/resources/user_default_tabs_builder.rb index 2c76269d09..e1b239b947 100644 --- a/app/models/spree/admin/resources/user_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/user_default_tabs_builder.rb @@ -21,7 +21,7 @@ def add_account_tab(root) Tab.new( { icon_name: 'person.svg', - name: "admin.user.account", + name: 'admin.user.account', url: ->(resource) { edit_admin_user_path(resource) }, classes: 'nav-link', partial_name: :account @@ -38,7 +38,7 @@ def add_addresses_tab(root) Tab.new( { icon_name: 'pin-map.svg', - name: "admin.user.addresses", + name: 'admin.user.addresses', url: ->(resource) { addresses_admin_user_path(resource) }, classes: 'nav-link', partial_name: :address From 7c8c1826ae4900420bf755e734097ded767604f2 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Fri, 29 Sep 2023 22:06:09 +0200 Subject: [PATCH 50/98] Lint fixes --- .../resources/order_default_tabs_builder_spec.rb | 16 ++++++++-------- spec/models/spree/admin/resources/root_spec.rb | 5 ++--- spec/models/spree/admin/resources/tab_spec.rb | 6 +++--- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/spec/models/spree/admin/resources/order_default_tabs_builder_spec.rb b/spec/models/spree/admin/resources/order_default_tabs_builder_spec.rb index b73d065fa3..844a251cec 100644 --- a/spec/models/spree/admin/resources/order_default_tabs_builder_spec.rb +++ b/spec/models/spree/admin/resources/order_default_tabs_builder_spec.rb @@ -6,14 +6,14 @@ module Admin let(:builder) { described_class.new } let(:default_tabs) do %i(cart - channel - customer - shipments - adjustments - payments - return_authorizations - customer_returns - state_changes) + channel + customer + shipments + adjustments + payments + return_authorizations + customer_returns + state_changes) end describe '#build' do diff --git a/spec/models/spree/admin/resources/root_spec.rb b/spec/models/spree/admin/resources/root_spec.rb index 8cc46a6bae..a158aa1b73 100644 --- a/spec/models/spree/admin/resources/root_spec.rb +++ b/spec/models/spree/admin/resources/root_spec.rb @@ -9,15 +9,14 @@ module Admin let(:item) { double(name: 'test') } context "when there's no item with a particular key" do - it 'appends an item' do root.add(item) expect(root.items).to include(item) end end - context "when there is an item with a particular key" do - before { root.add(item) } + context 'when there is an item with a particular key' do + before { root.add(item) } it 'raises an error' do expect { root.add(item) }.to raise_error(KeyError) diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/resources/tab_spec.rb index 4fa5eda0a3..d7ce2ab022 100644 --- a/spec/models/spree/admin/resources/tab_spec.rb +++ b/spec/models/spree/admin/resources/tab_spec.rb @@ -53,7 +53,7 @@ module Admin expect(subject).to eq(config[:partial_name]) end end - + describe '#available?' do subject { tab.available?(ability, resource) } @@ -95,7 +95,7 @@ module Admin context 'when tab matches the current tab' do let(:current_tab) { config[:partial_name] } - it "returns true" do + it 'returns true' do expect(subject).to be(true) end end @@ -103,7 +103,7 @@ module Admin context 'when tab does not match the current tab' do let(:current_tab) { 'non-matching' } - it "returns false" do + it 'returns false' do expect(subject).to be(false) end end From 50f449ef0b20c8eeb5e119ec5c8aadacfdceeda0 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Fri, 29 Sep 2023 22:33:48 +0200 Subject: [PATCH 51/98] Use global scope --- .../resources/order_default_tabs_builder.rb | 8 ++++---- .../resources/product_default_tabs_builder.rb | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/app/models/spree/admin/resources/order_default_tabs_builder.rb b/app/models/spree/admin/resources/order_default_tabs_builder.rb index bd7faed0f0..8f2d80def9 100644 --- a/app/models/spree/admin/resources/order_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/order_default_tabs_builder.rb @@ -115,7 +115,7 @@ def add_adjustments_tab(root) ). with_active_check. with_default_translator. - with_index_availability_check(Spree::Adjustment) + with_index_availability_check(::Spree::Adjustment) root.add(tab) end @@ -133,7 +133,7 @@ def add_payments_tab(root) ). with_active_check. with_default_translator. - with_index_availability_check(Spree::Payment) + with_index_availability_check(::Spree::Payment) root.add(tab) end @@ -151,7 +151,7 @@ def add_return_authorizations_tab(root) ). with_active_check. with_default_translator. - with_index_availability_check(Spree::ReturnAuthorization) + with_index_availability_check(::Spree::ReturnAuthorization) root.add(tab) end @@ -169,7 +169,7 @@ def add_customer_returns_tab(root) ). with_active_check. with_default_translator. - with_index_availability_check(Spree::CustomerReturn) + with_index_availability_check(::Spree::CustomerReturn) root.add(tab) end diff --git a/app/models/spree/admin/resources/product_default_tabs_builder.rb b/app/models/spree/admin/resources/product_default_tabs_builder.rb index b20b46821e..eb7ba74914 100644 --- a/app/models/spree/admin/resources/product_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/product_default_tabs_builder.rb @@ -32,7 +32,7 @@ def add_details_tab(root) ). with_active_check. with_default_translator. - with_admin_availability_check(Spree::Product) + with_admin_availability_check(::Spree::Product) root.add(tab) end @@ -52,7 +52,7 @@ def add_images_tab(root) with_default_translator. with_availability_check( lambda do |ability, resource| - ability.can?(:admin, Spree::Image) && !resource.deleted? + ability.can?(:admin, ::Spree::Image) && !resource.deleted? end ) @@ -74,7 +74,7 @@ def add_variants_tab(root) with_default_translator. with_availability_check( lambda do |ability, resource| - ability.can?(:admin, Spree::Variant) && !resource.deleted? + ability.can?(:admin, ::Spree::Variant) && !resource.deleted? end ) @@ -96,7 +96,7 @@ def add_properties_tab(root) with_default_translator. with_availability_check( lambda do |ability, resource| - ability.can?(:admin, Spree::ProductProperty) && !resource.deleted? + ability.can?(:admin, ::Spree::ProductProperty) && !resource.deleted? end ) @@ -118,7 +118,7 @@ def add_stock_tab(root) with_default_translator. with_availability_check( lambda do |ability, resource| - ability.can?(:admin, Spree::StockItem) && !resource.deleted? + ability.can?(:admin, ::Spree::StockItem) && !resource.deleted? end ) @@ -140,7 +140,7 @@ def add_prices_tab(root) with_default_translator. with_availability_check( lambda do |ability, resource| - ability.can?(:admin, Spree::Price) && !resource.deleted? + ability.can?(:admin, ::Spree::Price) && !resource.deleted? end ) @@ -162,7 +162,7 @@ def add_digitals_tab(root) with_default_translator. with_availability_check( lambda do |ability, resource| - ability.can?(:admin, Spree::Digital) && !resource.deleted? + ability.can?(:admin, ::Spree::Digital) && !resource.deleted? end ) @@ -184,7 +184,7 @@ def add_translations_tab(root) with_default_translator. with_availability_check( lambda do |ability, resource| - ability.can?(:admin, Spree::Product) && !resource.deleted? + ability.can?(:admin, ::Spree::Product) && !resource.deleted? end ) From 9ad49ddfde50961a18cb9beda5163e8c33d5ec12 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Mon, 2 Oct 2023 15:02:21 +0200 Subject: [PATCH 52/98] Refactor order_tabs_builder --- .../resources/order_default_tabs_builder.rb | 180 ++++++++++-------- 1 file changed, 99 insertions(+), 81 deletions(-) diff --git a/app/models/spree/admin/resources/order_default_tabs_builder.rb b/app/models/spree/admin/resources/order_default_tabs_builder.rb index 8f2d80def9..0140bd9c50 100644 --- a/app/models/spree/admin/resources/order_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/order_default_tabs_builder.rb @@ -22,15 +22,7 @@ def build def add_cart_tab(root) tab = - Tab.new( - { - icon_name: 'cart-check.svg', - name: :cart, - url: ->(resource) { cart_admin_order_path(resource) }, - classes: 'nav-link', - partial_name: :cart - } - ). + Tab.new(cart_tab_config). with_active_check. with_default_translator. with_availability_check( @@ -44,17 +36,19 @@ def add_cart_tab(root) root.add(tab) end + def cart_tab_config + { + icon_name: 'cart-check.svg', + name: :cart, + url: ->(resource) { cart_admin_order_path(resource) }, + classes: 'nav-link', + partial_name: :cart + } + end + def add_channel_tab(root) tab = - Tab.new( - { - icon_name: 'funnel.svg', - name: :channel, - url: ->(resource) { channel_admin_order_path(resource) }, - classes: 'nav-link', - partial_name: :channel - } - ). + Tab.new(channel_tab_config). with_active_check. with_default_translator. with_update_availability_check @@ -62,17 +56,19 @@ def add_channel_tab(root) root.add(tab) end + def channel_tab_config + { + icon_name: 'funnel.svg', + name: :channel, + url: ->(resource) { channel_admin_order_path(resource) }, + classes: 'nav-link', + partial_name: :channel + } + end + def add_customer_tab(root) tab = - Tab.new( - { - icon_name: 'person-lines-fill.svg', - name: :customer, - url: ->(resource) { admin_order_customer_path(resource) }, - classes: 'nav-link', - partial_name: :customer_details - } - ). + Tab.new(customer_tab_config). with_active_check. with_default_translator. with_availability_check( @@ -84,17 +80,19 @@ def add_customer_tab(root) root.add(tab) end + def customer_tab_config + { + icon_name: 'person-lines-fill.svg', + name: :customer, + url: ->(resource) { admin_order_customer_path(resource) }, + classes: 'nav-link', + partial_name: :customer_details + } + end + def add_shipments_tab(root) tab = - Tab.new( - { - icon_name: 'truck.svg', - name: :shipments, - url: ->(resource) { edit_admin_order_path(resource) }, - classes: 'nav-link', - partial_name: :shipments - } - ). + Tab.new(shipments_tab_config). with_active_check. with_default_translator. with_update_availability_check @@ -102,17 +100,19 @@ def add_shipments_tab(root) root.add(tab) end + def shipments_tab_config + { + icon_name: 'truck.svg', + name: :shipments, + url: ->(resource) { edit_admin_order_path(resource) }, + classes: 'nav-link', + partial_name: :shipments + } + end + def add_adjustments_tab(root) tab = - Tab.new( - { - icon_name: 'adjust.svg', - name: :adjustments, - url: ->(resource) { admin_order_adjustments_path(resource) }, - classes: 'nav-link', - partial_name: :adjustments - } - ). + Tab.new(adjustments_tab_config). with_active_check. with_default_translator. with_index_availability_check(::Spree::Adjustment) @@ -120,17 +120,19 @@ def add_adjustments_tab(root) root.add(tab) end + def adjustments_tab_config + { + icon_name: 'adjust.svg', + name: :adjustments, + url: ->(resource) { admin_order_adjustments_path(resource) }, + classes: 'nav-link', + partial_name: :adjustments + } + end + def add_payments_tab(root) tab = - Tab.new( - { - icon_name: 'credit-card.svg', - name: :payments, - url: ->(resource) { admin_order_payments_path(resource) }, - classes: 'nav-link', - partial_name: :payments - } - ). + Tab.new(payments_tab_config). with_active_check. with_default_translator. with_index_availability_check(::Spree::Payment) @@ -138,17 +140,19 @@ def add_payments_tab(root) root.add(tab) end + def payments_tab_config + { + icon_name: 'credit-card.svg', + name: :payments, + url: ->(resource) { admin_order_payments_path(resource) }, + classes: 'nav-link', + partial_name: :payments + } + end + def add_return_authorizations_tab(root) tab = - Tab.new( - { - icon_name: 'enter.svg', - name: :return_authorizations, - url: ->(resource) { admin_order_return_authorizations_path(resource) }, - classes: 'nav-link', - partial_name: :return_authorizations - } - ). + Tab.new(return_authorizations_tab_config). with_active_check. with_default_translator. with_index_availability_check(::Spree::ReturnAuthorization) @@ -156,17 +160,19 @@ def add_return_authorizations_tab(root) root.add(tab) end + def return_authorizations_tab_config + { + icon_name: 'enter.svg', + name: :return_authorizations, + url: ->(resource) { admin_order_return_authorizations_path(resource) }, + classes: 'nav-link', + partial_name: :return_authorizations + } + end + def add_customer_returns_tab(root) tab = - Tab.new( - { - icon_name: 'returns.svg', - name: :customer_returns, - url: ->(resource) { admin_order_customer_returns_path(resource) }, - classes: 'nav-link', - partial_name: :customer_returns - } - ). + Tab.new(customer_returns_tab_config). with_active_check. with_default_translator. with_index_availability_check(::Spree::CustomerReturn) @@ -174,23 +180,35 @@ def add_customer_returns_tab(root) root.add(tab) end + def customer_returns_tab_config + { + icon_name: 'returns.svg', + name: :customer_returns, + url: ->(resource) { admin_order_customer_returns_path(resource) }, + classes: 'nav-link', + partial_name: :customer_returns + } + end + def add_state_changes_tab(root) tab = - Tab.new( - { - icon_name: 'calendar3.svg', - name: :state_changes, - url: ->(resource) { admin_order_state_changes_path(resource) }, - classes: 'nav-link', - partial_name: :state_changes - } - ). + Tab.new(state_changes_tab_config). with_active_check. with_custom_translator(::Spree::StateChange, :human_attribute_name). with_update_availability_check root.add(tab) end + + def state_changes_tab_config + { + icon_name: 'calendar3.svg', + name: :state_changes, + url: ->(resource) { admin_order_state_changes_path(resource) }, + classes: 'nav-link', + partial_name: :state_changes + } + end end end end From 109faeead32d0369fe10b1f19a0ef54d094006b9 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Mon, 2 Oct 2023 15:29:34 +0200 Subject: [PATCH 53/98] Refactor product_tabs_builder. Refactor user_tabs_builder. --- .../resources/product_default_tabs_builder.rb | 160 ++++++++++-------- .../resources/user_default_tabs_builder.rb | 100 ++++++----- 2 files changed, 143 insertions(+), 117 deletions(-) diff --git a/app/models/spree/admin/resources/product_default_tabs_builder.rb b/app/models/spree/admin/resources/product_default_tabs_builder.rb index eb7ba74914..6cd1ab5f9d 100644 --- a/app/models/spree/admin/resources/product_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/product_default_tabs_builder.rb @@ -21,15 +21,7 @@ def build def add_details_tab(root) tab = - Tab.new( - { - icon_name: 'edit.svg', - name: :details, - url: ->(resource) { edit_admin_product_path(resource) }, - classes: 'nav-link', - partial_name: :details - } - ). + Tab.new(details_config). with_active_check. with_default_translator. with_admin_availability_check(::Spree::Product) @@ -37,17 +29,19 @@ def add_details_tab(root) root.add(tab) end + def details_config + { + icon_name: 'edit.svg', + name: :details, + url: ->(resource) { edit_admin_product_path(resource) }, + classes: 'nav-link', + partial_name: :details + } + end + def add_images_tab(root) tab = - Tab.new( - { - icon_name: 'images.svg', - name: :images, - url: ->(resource) { admin_product_images_path(resource) }, - classes: 'nav-link', - partial_name: :images - } - ). + Tab.new(images_config). with_active_check. with_default_translator. with_availability_check( @@ -59,17 +53,19 @@ def add_images_tab(root) root.add(tab) end + def images_config + { + icon_name: 'images.svg', + name: :images, + url: ->(resource) { admin_product_images_path(resource) }, + classes: 'nav-link', + partial_name: :images + } + end + def add_variants_tab(root) tab = - Tab.new( - { - icon_name: 'adjust.svg', - name: :variants, - url: ->(resource) { admin_product_variants_path(resource) }, - classes: 'nav-link', - partial_name: :variants - } - ). + Tab.new(variants_config). with_active_check. with_default_translator. with_availability_check( @@ -81,17 +77,19 @@ def add_variants_tab(root) root.add(tab) end + def variants_config + { + icon_name: 'adjust.svg', + name: :variants, + url: ->(resource) { admin_product_variants_path(resource) }, + classes: 'nav-link', + partial_name: :variants + } + end + def add_properties_tab(root) tab = - Tab.new( - { - icon_name: 'list.svg', - name: :properties, - url: ->(resource) { admin_product_product_properties_path(resource) }, - classes: 'nav-link', - partial_name: :properties - } - ). + Tab.new(properties_config). with_active_check. with_default_translator. with_availability_check( @@ -103,17 +101,19 @@ def add_properties_tab(root) root.add(tab) end + def properties_config + { + icon_name: 'list.svg', + name: :properties, + url: ->(resource) { admin_product_product_properties_path(resource) }, + classes: 'nav-link', + partial_name: :properties + } + end + def add_stock_tab(root) tab = - Tab.new( - { - icon_name: 'box-seam.svg', - name: :stock, - url: ->(resource) { stock_admin_product_path(resource) }, - classes: 'nav-link', - partial_name: :stock - } - ). + Tab.new(stock_config). with_active_check. with_default_translator. with_availability_check( @@ -125,17 +125,19 @@ def add_stock_tab(root) root.add(tab) end + def stock_config + { + icon_name: 'box-seam.svg', + name: :stock, + url: ->(resource) { stock_admin_product_path(resource) }, + classes: 'nav-link', + partial_name: :stock + } + end + def add_prices_tab(root) tab = - Tab.new( - { - icon_name: 'currency-exchange.svg', - name: :prices, - url: ->(resource) { admin_product_prices_path(resource) }, - classes: 'nav-link', - partial_name: :prices - } - ). + Tab.new(prices_config). with_active_check. with_default_translator. with_availability_check( @@ -147,17 +149,19 @@ def add_prices_tab(root) root.add(tab) end + def prices_config + { + icon_name: 'currency-exchange.svg', + name: :prices, + url: ->(resource) { admin_product_prices_path(resource) }, + classes: 'nav-link', + partial_name: :prices + } + end + def add_digitals_tab(root) tab = - Tab.new( - { - icon_name: 'download.svg', - name: 'admin.digitals.digital_assets', - url: ->(resource) { admin_product_digitals_path(resource) }, - classes: 'nav-link', - partial_name: :digitals - } - ). + Tab.new(digitals_config). with_active_check. with_default_translator. with_availability_check( @@ -169,17 +173,19 @@ def add_digitals_tab(root) root.add(tab) end + def digitals_config + { + icon_name: 'download.svg', + name: 'admin.digitals.digital_assets', + url: ->(resource) { admin_product_digitals_path(resource) }, + classes: 'nav-link', + partial_name: :digitals + } + end + def add_translations_tab(root) tab = - Tab.new( - { - icon_name: 'translate.svg', - name: :translations, - url: ->(resource) { translations_admin_product_path(resource) }, - classes: 'nav-link', - partial_name: :translations - } - ). + Tab.new(translations_config). with_active_check. with_default_translator. with_availability_check( @@ -190,6 +196,16 @@ def add_translations_tab(root) root.add(tab) end + + def translations_config + { + icon_name: 'translate.svg', + name: :translations, + url: ->(resource) { translations_admin_product_path(resource) }, + classes: 'nav-link', + partial_name: :translations + } + end end end end diff --git a/app/models/spree/admin/resources/user_default_tabs_builder.rb b/app/models/spree/admin/resources/user_default_tabs_builder.rb index e1b239b947..e1d1fc0a17 100644 --- a/app/models/spree/admin/resources/user_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/user_default_tabs_builder.rb @@ -18,88 +18,98 @@ def build def add_account_tab(root) tab = - Tab.new( - { - icon_name: 'person.svg', - name: 'admin.user.account', - url: ->(resource) { edit_admin_user_path(resource) }, - classes: 'nav-link', - partial_name: :account - } - ). + Tab.new(account_config). with_active_check. with_default_translator root.add(tab) end + def account_config + { + icon_name: 'person.svg', + name: 'admin.user.account', + url: ->(resource) { edit_admin_user_path(resource) }, + classes: 'nav-link', + partial_name: :account + } + end + def add_addresses_tab(root) tab = - Tab.new( - { - icon_name: 'pin-map.svg', - name: 'admin.user.addresses', - url: ->(resource) { addresses_admin_user_path(resource) }, - classes: 'nav-link', - partial_name: :address - } - ). + Tab.new(addresses_config). with_active_check. with_default_translator root.add(tab) end + def addresses_config + { + icon_name: 'pin-map.svg', + name: 'admin.user.addresses', + url: ->(resource) { addresses_admin_user_path(resource) }, + classes: 'nav-link', + partial_name: :address + } + end + def add_orders_tab(root) tab = - Tab.new( - { - icon_name: 'inbox.svg', - name: 'admin.user.orders', - url: ->(resource) { orders_admin_user_path(resource) }, - classes: 'nav-link', - partial_name: :orders - } - ). + Tab.new(orders_config). with_active_check. with_default_translator root.add(tab) end + def orders_config + { + icon_name: 'inbox.svg', + name: 'admin.user.orders', + url: ->(resource) { orders_admin_user_path(resource) }, + classes: 'nav-link', + partial_name: :orders + } + end + def add_items_tab(root) tab = - Tab.new( - { - icon_name: 'tag.svg', - name: 'admin.user.items', - url: ->(resource) { items_admin_user_path(resource) }, - classes: 'nav-link', - partial_name: :items - } - ). + Tab.new(items_config). with_active_check. with_default_translator root.add(tab) end + def items_config + { + icon_name: 'tag.svg', + name: 'admin.user.items', + url: ->(resource) { items_admin_user_path(resource) }, + classes: 'nav-link', + partial_name: :items + } + end + def add_store_credits_tab(root) tab = - Tab.new( - { - icon_name: 'gift.svg', - name: 'admin.user.store_credits', - url: ->(resource) { admin_user_store_credits_path(resource) }, - classes: 'nav-link', - partial_name: :store_credits - } - ). + Tab.new(store_credits_config). with_active_check. with_default_translator root.add(tab) end + + def store_credits_config + { + icon_name: 'gift.svg', + name: 'admin.user.store_credits', + url: ->(resource) { admin_user_store_credits_path(resource) }, + classes: 'nav-link', + partial_name: :store_credits + } + end end end end From 3a6ad94b2f810f2e7414e29ab6d0e56849fbb832 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Mon, 2 Oct 2023 15:35:23 +0200 Subject: [PATCH 54/98] Disable class length cop --- app/models/spree/admin/resources/order_default_tabs_builder.rb | 2 ++ .../spree/admin/resources/product_default_tabs_builder.rb | 2 ++ 2 files changed, 4 insertions(+) diff --git a/app/models/spree/admin/resources/order_default_tabs_builder.rb b/app/models/spree/admin/resources/order_default_tabs_builder.rb index 0140bd9c50..4bedaf1420 100644 --- a/app/models/spree/admin/resources/order_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/order_default_tabs_builder.rb @@ -1,6 +1,7 @@ module Spree module Admin module Resources + # rubocop:disable Metrics/ClassLength class OrderDefaultTabsBuilder include Spree::Core::Engine.routes.url_helpers @@ -210,6 +211,7 @@ def state_changes_tab_config } end end + # rubocop:enable Metrics/ClassLength end end end diff --git a/app/models/spree/admin/resources/product_default_tabs_builder.rb b/app/models/spree/admin/resources/product_default_tabs_builder.rb index 6cd1ab5f9d..6d79e8512f 100644 --- a/app/models/spree/admin/resources/product_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/product_default_tabs_builder.rb @@ -1,6 +1,7 @@ module Spree module Admin module Resources + # rubocop:disable Metrics/ClassLength class ProductDefaultTabsBuilder include Spree::Core::Engine.routes.url_helpers @@ -207,6 +208,7 @@ def translations_config } end end + # rubocop:enable Metrics/ClassLength end end end From 3fb2c430c34273fb382f33d30c40e450a4ea53d5 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 3 Oct 2023 18:38:53 +0200 Subject: [PATCH 55/98] Add missing data_hook attribute --- app/models/spree/admin/resources/data_hook.rb | 12 ++++++++++ .../resources/order_default_tabs_builder.rb | 24 ++++++++++++------- app/models/spree/admin/resources/tab.rb | 5 ++++ .../spree/admin/shared/_order_tabs.html.erb | 4 ++-- 4 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 app/models/spree/admin/resources/data_hook.rb diff --git a/app/models/spree/admin/resources/data_hook.rb b/app/models/spree/admin/resources/data_hook.rb new file mode 100644 index 0000000000..9b6efaa5ad --- /dev/null +++ b/app/models/spree/admin/resources/data_hook.rb @@ -0,0 +1,12 @@ +module Spree + module Admin + module Resources + module DataHook + def with_data_hook(data_hook) + @data_hook = data_hook + self + end + end + end + end +end diff --git a/app/models/spree/admin/resources/order_default_tabs_builder.rb b/app/models/spree/admin/resources/order_default_tabs_builder.rb index 4bedaf1420..a9366d10b9 100644 --- a/app/models/spree/admin/resources/order_default_tabs_builder.rb +++ b/app/models/spree/admin/resources/order_default_tabs_builder.rb @@ -32,7 +32,8 @@ def add_cart_tab(root) lambda do |ability, resource| ability.can?(:update, resource) && (resource.shipments.empty? || resource.shipments.shipped.empty?) end - ) + ). + with_data_hook('admin_order_tabs_cart_details') root.add(tab) end @@ -52,7 +53,8 @@ def add_channel_tab(root) Tab.new(channel_tab_config). with_active_check. with_default_translator. - with_update_availability_check + with_update_availability_check. + with_data_hook('admin_order_tabs_channel_details') root.add(tab) end @@ -76,7 +78,8 @@ def add_customer_tab(root) lambda do |ability, resource| ability.can?(:update, resource) && resource.checkout_steps.include?('address') end - ) + ). + with_data_hook('admin_order_tabs_customer_details') root.add(tab) end @@ -96,7 +99,8 @@ def add_shipments_tab(root) Tab.new(shipments_tab_config). with_active_check. with_default_translator. - with_update_availability_check + with_update_availability_check. + with_data_hook('admin_order_tabs_shipment_details') root.add(tab) end @@ -116,7 +120,8 @@ def add_adjustments_tab(root) Tab.new(adjustments_tab_config). with_active_check. with_default_translator. - with_index_availability_check(::Spree::Adjustment) + with_index_availability_check(::Spree::Adjustment). + with_data_hook('admin_order_tabs_adjustments') root.add(tab) end @@ -136,7 +141,8 @@ def add_payments_tab(root) Tab.new(payments_tab_config). with_active_check. with_default_translator. - with_index_availability_check(::Spree::Payment) + with_index_availability_check(::Spree::Payment). + with_data_hook('admin_order_tabs_payments') root.add(tab) end @@ -156,7 +162,8 @@ def add_return_authorizations_tab(root) Tab.new(return_authorizations_tab_config). with_active_check. with_default_translator. - with_index_availability_check(::Spree::ReturnAuthorization) + with_index_availability_check(::Spree::ReturnAuthorization). + with_data_hook('admin_order_tabs_return_authorizations') root.add(tab) end @@ -196,7 +203,8 @@ def add_state_changes_tab(root) Tab.new(state_changes_tab_config). with_active_check. with_custom_translator(::Spree::StateChange, :human_attribute_name). - with_update_availability_check + with_update_availability_check. + with_data_hook('admin_order_tabs_state_changes') root.add(tab) end diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/resources/tab.rb index ceae6da51e..08d2922fc1 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/resources/tab.rb @@ -5,6 +5,7 @@ class Tab include ConditionalChecker include AvailabilityBuilderMethods include Translator + include DataHook attr_reader :icon_name, :name, :classes, :partial_name @@ -41,6 +42,10 @@ def text @translate.call(name) end + + def data_hook + @data_hook.presence + end end end end diff --git a/app/views/spree/admin/shared/_order_tabs.html.erb b/app/views/spree/admin/shared/_order_tabs.html.erb index 66755519ea..7e94dee4b4 100644 --- a/app/views/spree/admin/shared/_order_tabs.html.erb +++ b/app/views/spree/admin/shared/_order_tabs.html.erb @@ -6,13 +6,13 @@ <% content_for :page_tabs do %> <% order_tabs.items.each do |tab| %> <% next unless tab.available?(current_ability, @order) %> - + <% end %> <% end %> <% end %> From 759fce631f2efeec802cbf2491c7915afc3a7401 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 3 Oct 2023 18:54:10 +0200 Subject: [PATCH 56/98] Use more descriptive namespace --- .../{resources => tabs}/availability_builder_methods.rb | 2 +- .../spree/admin/{resources => tabs}/conditional_checker.rb | 2 +- app/models/spree/admin/{resources => tabs}/data_hook.rb | 2 +- .../admin/{resources => tabs}/order_default_tabs_builder.rb | 2 +- .../{resources => tabs}/product_default_tabs_builder.rb | 2 +- app/models/spree/admin/{resources => tabs}/root.rb | 2 +- app/models/spree/admin/{resources => tabs}/tab.rb | 2 +- app/models/spree/admin/{resources => tabs}/translator.rb | 2 +- .../admin/{resources => tabs}/user_default_tabs_builder.rb | 2 +- lib/spree/backend/engine.rb | 6 +++--- .../{resources => tabs}/order_default_tabs_builder_spec.rb | 2 +- .../product_default_tabs_builder_spec.rb | 2 +- spec/models/spree/admin/{resources => tabs}/root_spec.rb | 2 +- spec/models/spree/admin/{resources => tabs}/tab_spec.rb | 2 +- .../{resources => tabs}/user_default_tabs_builder_spec.rb | 2 +- 15 files changed, 17 insertions(+), 17 deletions(-) rename app/models/spree/admin/{resources => tabs}/availability_builder_methods.rb (97%) rename app/models/spree/admin/{resources => tabs}/conditional_checker.rb (94%) rename app/models/spree/admin/{resources => tabs}/data_hook.rb (89%) rename app/models/spree/admin/{resources => tabs}/order_default_tabs_builder.rb (99%) rename app/models/spree/admin/{resources => tabs}/product_default_tabs_builder.rb (99%) rename app/models/spree/admin/{resources => tabs}/root.rb (95%) rename app/models/spree/admin/{resources => tabs}/tab.rb (98%) rename app/models/spree/admin/{resources => tabs}/translator.rb (94%) rename app/models/spree/admin/{resources => tabs}/user_default_tabs_builder.rb (99%) rename spec/models/spree/admin/{resources => tabs}/order_default_tabs_builder_spec.rb (91%) rename spec/models/spree/admin/{resources => tabs}/product_default_tabs_builder_spec.rb (90%) rename spec/models/spree/admin/{resources => tabs}/root_spec.rb (93%) rename spec/models/spree/admin/{resources => tabs}/tab_spec.rb (98%) rename spec/models/spree/admin/{resources => tabs}/user_default_tabs_builder_spec.rb (90%) diff --git a/app/models/spree/admin/resources/availability_builder_methods.rb b/app/models/spree/admin/tabs/availability_builder_methods.rb similarity index 97% rename from app/models/spree/admin/resources/availability_builder_methods.rb rename to app/models/spree/admin/tabs/availability_builder_methods.rb index 79002f5dd9..57dd343043 100644 --- a/app/models/spree/admin/resources/availability_builder_methods.rb +++ b/app/models/spree/admin/tabs/availability_builder_methods.rb @@ -1,6 +1,6 @@ module Spree module Admin - module Resources + module Tabs module AvailabilityBuilderMethods def with_availability_check(availability_check) @availability_check = availability_check diff --git a/app/models/spree/admin/resources/conditional_checker.rb b/app/models/spree/admin/tabs/conditional_checker.rb similarity index 94% rename from app/models/spree/admin/resources/conditional_checker.rb rename to app/models/spree/admin/tabs/conditional_checker.rb index e5d5bbd09f..e17b65d362 100644 --- a/app/models/spree/admin/resources/conditional_checker.rb +++ b/app/models/spree/admin/tabs/conditional_checker.rb @@ -1,6 +1,6 @@ module Spree module Admin - module Resources + module Tabs module ConditionalChecker def with_active_check @active_check = ->(current_tab, text) { current_tab == text } diff --git a/app/models/spree/admin/resources/data_hook.rb b/app/models/spree/admin/tabs/data_hook.rb similarity index 89% rename from app/models/spree/admin/resources/data_hook.rb rename to app/models/spree/admin/tabs/data_hook.rb index 9b6efaa5ad..16bcd67c83 100644 --- a/app/models/spree/admin/resources/data_hook.rb +++ b/app/models/spree/admin/tabs/data_hook.rb @@ -1,6 +1,6 @@ module Spree module Admin - module Resources + module Tabs module DataHook def with_data_hook(data_hook) @data_hook = data_hook diff --git a/app/models/spree/admin/resources/order_default_tabs_builder.rb b/app/models/spree/admin/tabs/order_default_tabs_builder.rb similarity index 99% rename from app/models/spree/admin/resources/order_default_tabs_builder.rb rename to app/models/spree/admin/tabs/order_default_tabs_builder.rb index a9366d10b9..cb65d67c1b 100644 --- a/app/models/spree/admin/resources/order_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/order_default_tabs_builder.rb @@ -1,6 +1,6 @@ module Spree module Admin - module Resources + module Tabs # rubocop:disable Metrics/ClassLength class OrderDefaultTabsBuilder include Spree::Core::Engine.routes.url_helpers diff --git a/app/models/spree/admin/resources/product_default_tabs_builder.rb b/app/models/spree/admin/tabs/product_default_tabs_builder.rb similarity index 99% rename from app/models/spree/admin/resources/product_default_tabs_builder.rb rename to app/models/spree/admin/tabs/product_default_tabs_builder.rb index 6d79e8512f..5d906ba843 100644 --- a/app/models/spree/admin/resources/product_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/product_default_tabs_builder.rb @@ -1,6 +1,6 @@ module Spree module Admin - module Resources + module Tabs # rubocop:disable Metrics/ClassLength class ProductDefaultTabsBuilder include Spree::Core::Engine.routes.url_helpers diff --git a/app/models/spree/admin/resources/root.rb b/app/models/spree/admin/tabs/root.rb similarity index 95% rename from app/models/spree/admin/resources/root.rb rename to app/models/spree/admin/tabs/root.rb index 6ff0e96dcb..bb8781a250 100644 --- a/app/models/spree/admin/resources/root.rb +++ b/app/models/spree/admin/tabs/root.rb @@ -1,6 +1,6 @@ module Spree module Admin - module Resources + module Tabs class Root attr_reader :items diff --git a/app/models/spree/admin/resources/tab.rb b/app/models/spree/admin/tabs/tab.rb similarity index 98% rename from app/models/spree/admin/resources/tab.rb rename to app/models/spree/admin/tabs/tab.rb index 08d2922fc1..e5554f90ca 100644 --- a/app/models/spree/admin/resources/tab.rb +++ b/app/models/spree/admin/tabs/tab.rb @@ -1,6 +1,6 @@ module Spree module Admin - module Resources + module Tabs class Tab include ConditionalChecker include AvailabilityBuilderMethods diff --git a/app/models/spree/admin/resources/translator.rb b/app/models/spree/admin/tabs/translator.rb similarity index 94% rename from app/models/spree/admin/resources/translator.rb rename to app/models/spree/admin/tabs/translator.rb index 90973e1ff1..1cc0ff3675 100644 --- a/app/models/spree/admin/resources/translator.rb +++ b/app/models/spree/admin/tabs/translator.rb @@ -1,6 +1,6 @@ module Spree module Admin - module Resources + module Tabs module Translator def with_default_translator @translate = ->(name) { ::Spree.t(name) } diff --git a/app/models/spree/admin/resources/user_default_tabs_builder.rb b/app/models/spree/admin/tabs/user_default_tabs_builder.rb similarity index 99% rename from app/models/spree/admin/resources/user_default_tabs_builder.rb rename to app/models/spree/admin/tabs/user_default_tabs_builder.rb index e1d1fc0a17..c705930702 100644 --- a/app/models/spree/admin/resources/user_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/user_default_tabs_builder.rb @@ -1,6 +1,6 @@ module Spree module Admin - module Resources + module Tabs class UserDefaultTabsBuilder include Spree::Core::Engine.routes.url_helpers diff --git a/lib/spree/backend/engine.rb b/lib/spree/backend/engine.rb index 5dab344fbf..85eee2fee8 100644 --- a/lib/spree/backend/engine.rb +++ b/lib/spree/backend/engine.rb @@ -27,9 +27,9 @@ class Engine < ::Rails::Engine config.after_initialize do Rails.application.reload_routes! Rails.application.config.spree_backend.main_menu = Spree::Admin::MainMenu::DefaultConfigurationBuilder.new.build - Rails.application.config.spree_backend.order_tabs = Spree::Admin::Resources::OrderDefaultTabsBuilder.new.build - Rails.application.config.spree_backend.user_tabs = Spree::Admin::Resources::UserDefaultTabsBuilder.new.build - Rails.application.config.spree_backend.product_tabs = Spree::Admin::Resources::ProductDefaultTabsBuilder.new.build + Rails.application.config.spree_backend.order_tabs = Spree::Admin::Tabs::OrderDefaultTabsBuilder.new.build + Rails.application.config.spree_backend.user_tabs = Spree::Admin::Tabs::UserDefaultTabsBuilder.new.build + Rails.application.config.spree_backend.product_tabs = Spree::Admin::Tabs::ProductDefaultTabsBuilder.new.build end end end diff --git a/spec/models/spree/admin/resources/order_default_tabs_builder_spec.rb b/spec/models/spree/admin/tabs/order_default_tabs_builder_spec.rb similarity index 91% rename from spec/models/spree/admin/resources/order_default_tabs_builder_spec.rb rename to spec/models/spree/admin/tabs/order_default_tabs_builder_spec.rb index 844a251cec..296497ad42 100644 --- a/spec/models/spree/admin/resources/order_default_tabs_builder_spec.rb +++ b/spec/models/spree/admin/tabs/order_default_tabs_builder_spec.rb @@ -2,7 +2,7 @@ module Spree module Admin - describe Resources::OrderDefaultTabsBuilder, type: :model do + describe Tabs::OrderDefaultTabsBuilder, type: :model do let(:builder) { described_class.new } let(:default_tabs) do %i(cart diff --git a/spec/models/spree/admin/resources/product_default_tabs_builder_spec.rb b/spec/models/spree/admin/tabs/product_default_tabs_builder_spec.rb similarity index 90% rename from spec/models/spree/admin/resources/product_default_tabs_builder_spec.rb rename to spec/models/spree/admin/tabs/product_default_tabs_builder_spec.rb index fa8ee94666..15515371c7 100644 --- a/spec/models/spree/admin/resources/product_default_tabs_builder_spec.rb +++ b/spec/models/spree/admin/tabs/product_default_tabs_builder_spec.rb @@ -2,7 +2,7 @@ module Spree module Admin - describe Resources::ProductDefaultTabsBuilder, type: :model do + describe Tabs::ProductDefaultTabsBuilder, type: :model do let(:builder) { described_class.new } let(:default_tabs) do [:details, diff --git a/spec/models/spree/admin/resources/root_spec.rb b/spec/models/spree/admin/tabs/root_spec.rb similarity index 93% rename from spec/models/spree/admin/resources/root_spec.rb rename to spec/models/spree/admin/tabs/root_spec.rb index a158aa1b73..84b6617e34 100644 --- a/spec/models/spree/admin/resources/root_spec.rb +++ b/spec/models/spree/admin/tabs/root_spec.rb @@ -2,7 +2,7 @@ module Spree module Admin - describe Resources::Root, type: :model do + describe Tabs::Root, type: :model do let(:root) { described_class.new } describe '#add(item)' do diff --git a/spec/models/spree/admin/resources/tab_spec.rb b/spec/models/spree/admin/tabs/tab_spec.rb similarity index 98% rename from spec/models/spree/admin/resources/tab_spec.rb rename to spec/models/spree/admin/tabs/tab_spec.rb index d7ce2ab022..c894d1d440 100644 --- a/spec/models/spree/admin/resources/tab_spec.rb +++ b/spec/models/spree/admin/tabs/tab_spec.rb @@ -2,7 +2,7 @@ module Spree module Admin - describe Resources::Tab, type: :model do + describe Tabs::Tab, type: :model do let(:tab) { described_class.new(config) } let(:config) do { diff --git a/spec/models/spree/admin/resources/user_default_tabs_builder_spec.rb b/spec/models/spree/admin/tabs/user_default_tabs_builder_spec.rb similarity index 90% rename from spec/models/spree/admin/resources/user_default_tabs_builder_spec.rb rename to spec/models/spree/admin/tabs/user_default_tabs_builder_spec.rb index 716d648636..2d26376c45 100644 --- a/spec/models/spree/admin/resources/user_default_tabs_builder_spec.rb +++ b/spec/models/spree/admin/tabs/user_default_tabs_builder_spec.rb @@ -2,7 +2,7 @@ module Spree module Admin - describe Resources::UserDefaultTabsBuilder, type: :model do + describe Tabs::UserDefaultTabsBuilder, type: :model do let(:builder) { described_class.new } let(:default_tabs) do ['admin.user.account', From 4ca91066ae776a5dafc10bee4d84e9745228600d Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 3 Oct 2023 19:29:53 +0200 Subject: [PATCH 57/98] Group tabs together in the app config --- app/helpers/spree/admin/navigation_helper.rb | 6 +++--- lib/spree/backend/engine.rb | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/helpers/spree/admin/navigation_helper.rb b/app/helpers/spree/admin/navigation_helper.rb index e9cfb0eba0..0060742ff6 100644 --- a/app/helpers/spree/admin/navigation_helper.rb +++ b/app/helpers/spree/admin/navigation_helper.rb @@ -310,15 +310,15 @@ def main_menu end def order_tabs - Rails.application.config.spree_backend.order_tabs + Rails.application.config.spree_backend.tabs[:order] end def user_tabs - Rails.application.config.spree_backend.user_tabs + Rails.application.config.spree_backend.tabs[:user] end def product_tabs - Rails.application.config.spree_backend.product_tabs + Rails.application.config.spree_backend.tabs[:product] end end end diff --git a/lib/spree/backend/engine.rb b/lib/spree/backend/engine.rb index 85eee2fee8..3b59f3e2e1 100644 --- a/lib/spree/backend/engine.rb +++ b/lib/spree/backend/engine.rb @@ -3,7 +3,7 @@ module Spree module Backend class Engine < ::Rails::Engine - Environment = Struct.new(:main_menu, :order_tabs, :user_tabs, :product_tabs) + Environment = Struct.new(:main_menu, :tabs) config.middleware.use 'Spree::Backend::Middleware::SeoAssist' @@ -27,9 +27,10 @@ class Engine < ::Rails::Engine config.after_initialize do Rails.application.reload_routes! Rails.application.config.spree_backend.main_menu = Spree::Admin::MainMenu::DefaultConfigurationBuilder.new.build - Rails.application.config.spree_backend.order_tabs = Spree::Admin::Tabs::OrderDefaultTabsBuilder.new.build - Rails.application.config.spree_backend.user_tabs = Spree::Admin::Tabs::UserDefaultTabsBuilder.new.build - Rails.application.config.spree_backend.product_tabs = Spree::Admin::Tabs::ProductDefaultTabsBuilder.new.build + Rails.application.config.spree_backend.tabs = {} + Rails.application.config.spree_backend.tabs[:order] = Spree::Admin::Tabs::OrderDefaultTabsBuilder.new.build + Rails.application.config.spree_backend.tabs[:user] = Spree::Admin::Tabs::UserDefaultTabsBuilder.new.build + Rails.application.config.spree_backend.tabs[:product] = Spree::Admin::Tabs::ProductDefaultTabsBuilder.new.build end end end From 1569d11c1baa428eb79dd177aaba9b2364950a3a Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 3 Oct 2023 19:48:14 +0200 Subject: [PATCH 58/98] Embed default translation mechanism in tab --- .../admin/tabs/order_default_tabs_builder.rb | 8 -------- .../admin/tabs/product_default_tabs_builder.rb | 8 -------- app/models/spree/admin/tabs/tab.rb | 2 +- app/models/spree/admin/tabs/translator.rb | 5 ----- .../spree/admin/tabs/user_default_tabs_builder.rb | 15 +++++---------- 5 files changed, 6 insertions(+), 32 deletions(-) diff --git a/app/models/spree/admin/tabs/order_default_tabs_builder.rb b/app/models/spree/admin/tabs/order_default_tabs_builder.rb index cb65d67c1b..589914a79c 100644 --- a/app/models/spree/admin/tabs/order_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/order_default_tabs_builder.rb @@ -25,7 +25,6 @@ def add_cart_tab(root) tab = Tab.new(cart_tab_config). with_active_check. - with_default_translator. with_availability_check( # An abstract module should not be aware of resource's internal structure. # If these checks are elaborate, it's better to have this complexity declared explicitly here. @@ -52,7 +51,6 @@ def add_channel_tab(root) tab = Tab.new(channel_tab_config). with_active_check. - with_default_translator. with_update_availability_check. with_data_hook('admin_order_tabs_channel_details') @@ -73,7 +71,6 @@ def add_customer_tab(root) tab = Tab.new(customer_tab_config). with_active_check. - with_default_translator. with_availability_check( lambda do |ability, resource| ability.can?(:update, resource) && resource.checkout_steps.include?('address') @@ -98,7 +95,6 @@ def add_shipments_tab(root) tab = Tab.new(shipments_tab_config). with_active_check. - with_default_translator. with_update_availability_check. with_data_hook('admin_order_tabs_shipment_details') @@ -119,7 +115,6 @@ def add_adjustments_tab(root) tab = Tab.new(adjustments_tab_config). with_active_check. - with_default_translator. with_index_availability_check(::Spree::Adjustment). with_data_hook('admin_order_tabs_adjustments') @@ -140,7 +135,6 @@ def add_payments_tab(root) tab = Tab.new(payments_tab_config). with_active_check. - with_default_translator. with_index_availability_check(::Spree::Payment). with_data_hook('admin_order_tabs_payments') @@ -161,7 +155,6 @@ def add_return_authorizations_tab(root) tab = Tab.new(return_authorizations_tab_config). with_active_check. - with_default_translator. with_index_availability_check(::Spree::ReturnAuthorization). with_data_hook('admin_order_tabs_return_authorizations') @@ -182,7 +175,6 @@ def add_customer_returns_tab(root) tab = Tab.new(customer_returns_tab_config). with_active_check. - with_default_translator. with_index_availability_check(::Spree::CustomerReturn) root.add(tab) diff --git a/app/models/spree/admin/tabs/product_default_tabs_builder.rb b/app/models/spree/admin/tabs/product_default_tabs_builder.rb index 5d906ba843..b278ff0d41 100644 --- a/app/models/spree/admin/tabs/product_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/product_default_tabs_builder.rb @@ -24,7 +24,6 @@ def add_details_tab(root) tab = Tab.new(details_config). with_active_check. - with_default_translator. with_admin_availability_check(::Spree::Product) root.add(tab) @@ -44,7 +43,6 @@ def add_images_tab(root) tab = Tab.new(images_config). with_active_check. - with_default_translator. with_availability_check( lambda do |ability, resource| ability.can?(:admin, ::Spree::Image) && !resource.deleted? @@ -68,7 +66,6 @@ def add_variants_tab(root) tab = Tab.new(variants_config). with_active_check. - with_default_translator. with_availability_check( lambda do |ability, resource| ability.can?(:admin, ::Spree::Variant) && !resource.deleted? @@ -92,7 +89,6 @@ def add_properties_tab(root) tab = Tab.new(properties_config). with_active_check. - with_default_translator. with_availability_check( lambda do |ability, resource| ability.can?(:admin, ::Spree::ProductProperty) && !resource.deleted? @@ -116,7 +112,6 @@ def add_stock_tab(root) tab = Tab.new(stock_config). with_active_check. - with_default_translator. with_availability_check( lambda do |ability, resource| ability.can?(:admin, ::Spree::StockItem) && !resource.deleted? @@ -140,7 +135,6 @@ def add_prices_tab(root) tab = Tab.new(prices_config). with_active_check. - with_default_translator. with_availability_check( lambda do |ability, resource| ability.can?(:admin, ::Spree::Price) && !resource.deleted? @@ -164,7 +158,6 @@ def add_digitals_tab(root) tab = Tab.new(digitals_config). with_active_check. - with_default_translator. with_availability_check( lambda do |ability, resource| ability.can?(:admin, ::Spree::Digital) && !resource.deleted? @@ -188,7 +181,6 @@ def add_translations_tab(root) tab = Tab.new(translations_config). with_active_check. - with_default_translator. with_availability_check( lambda do |ability, resource| ability.can?(:admin, ::Spree::Product) && !resource.deleted? diff --git a/app/models/spree/admin/tabs/tab.rb b/app/models/spree/admin/tabs/tab.rb index e5554f90ca..4c8fd13133 100644 --- a/app/models/spree/admin/tabs/tab.rb +++ b/app/models/spree/admin/tabs/tab.rb @@ -38,7 +38,7 @@ def complete?(resource) end def text - return true unless @translate.present? + return ::Spree.t(name) unless @translate.present? @translate.call(name) end diff --git a/app/models/spree/admin/tabs/translator.rb b/app/models/spree/admin/tabs/translator.rb index 1cc0ff3675..9042dda943 100644 --- a/app/models/spree/admin/tabs/translator.rb +++ b/app/models/spree/admin/tabs/translator.rb @@ -2,11 +2,6 @@ module Spree module Admin module Tabs module Translator - def with_default_translator - @translate = ->(name) { ::Spree.t(name) } - self - end - def with_custom_translator(klass, method) @translate = lambda do |name| klass.send(method, name) diff --git a/app/models/spree/admin/tabs/user_default_tabs_builder.rb b/app/models/spree/admin/tabs/user_default_tabs_builder.rb index c705930702..73adf57e87 100644 --- a/app/models/spree/admin/tabs/user_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/user_default_tabs_builder.rb @@ -19,8 +19,7 @@ def build def add_account_tab(root) tab = Tab.new(account_config). - with_active_check. - with_default_translator + with_active_check root.add(tab) end @@ -38,8 +37,7 @@ def account_config def add_addresses_tab(root) tab = Tab.new(addresses_config). - with_active_check. - with_default_translator + with_active_check root.add(tab) end @@ -57,8 +55,7 @@ def addresses_config def add_orders_tab(root) tab = Tab.new(orders_config). - with_active_check. - with_default_translator + with_active_check root.add(tab) end @@ -76,8 +73,7 @@ def orders_config def add_items_tab(root) tab = Tab.new(items_config). - with_active_check. - with_default_translator + with_active_check root.add(tab) end @@ -95,8 +91,7 @@ def items_config def add_store_credits_tab(root) tab = Tab.new(store_credits_config). - with_active_check. - with_default_translator + with_active_check root.add(tab) end From 5e923d8cd5da428652ea16fe5de0e358d3f63122 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 4 Oct 2023 08:13:24 +0200 Subject: [PATCH 59/98] Remove custom translations --- .../spree/admin/tabs/order_default_tabs_builder.rb | 1 - app/models/spree/admin/tabs/tab.rb | 5 +---- app/models/spree/admin/tabs/translator.rb | 14 -------------- 3 files changed, 1 insertion(+), 19 deletions(-) delete mode 100644 app/models/spree/admin/tabs/translator.rb diff --git a/app/models/spree/admin/tabs/order_default_tabs_builder.rb b/app/models/spree/admin/tabs/order_default_tabs_builder.rb index 589914a79c..88d4e114fe 100644 --- a/app/models/spree/admin/tabs/order_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/order_default_tabs_builder.rb @@ -194,7 +194,6 @@ def add_state_changes_tab(root) tab = Tab.new(state_changes_tab_config). with_active_check. - with_custom_translator(::Spree::StateChange, :human_attribute_name). with_update_availability_check. with_data_hook('admin_order_tabs_state_changes') diff --git a/app/models/spree/admin/tabs/tab.rb b/app/models/spree/admin/tabs/tab.rb index 4c8fd13133..ea7a50667a 100644 --- a/app/models/spree/admin/tabs/tab.rb +++ b/app/models/spree/admin/tabs/tab.rb @@ -4,7 +4,6 @@ module Tabs class Tab include ConditionalChecker include AvailabilityBuilderMethods - include Translator include DataHook attr_reader :icon_name, :name, :classes, :partial_name @@ -38,9 +37,7 @@ def complete?(resource) end def text - return ::Spree.t(name) unless @translate.present? - - @translate.call(name) + ::Spree.t(name) end def data_hook diff --git a/app/models/spree/admin/tabs/translator.rb b/app/models/spree/admin/tabs/translator.rb deleted file mode 100644 index 9042dda943..0000000000 --- a/app/models/spree/admin/tabs/translator.rb +++ /dev/null @@ -1,14 +0,0 @@ -module Spree - module Admin - module Tabs - module Translator - def with_custom_translator(klass, method) - @translate = lambda do |name| - klass.send(method, name) - end - self - end - end - end - end -end From 31d41c3c5cf4022d78ec5f207776e410cce6a797 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 4 Oct 2023 15:05:34 +0200 Subject: [PATCH 60/98] Add missing .complete? checks --- .../admin/tabs/order_default_tabs_builder.rb | 2 ++ .../spree/admin/shared/_order_tabs.html.erb | 16 +++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app/models/spree/admin/tabs/order_default_tabs_builder.rb b/app/models/spree/admin/tabs/order_default_tabs_builder.rb index 88d4e114fe..3fc8f919c5 100644 --- a/app/models/spree/admin/tabs/order_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/order_default_tabs_builder.rb @@ -155,6 +155,7 @@ def add_return_authorizations_tab(root) tab = Tab.new(return_authorizations_tab_config). with_active_check. + with_completed_check. with_index_availability_check(::Spree::ReturnAuthorization). with_data_hook('admin_order_tabs_return_authorizations') @@ -175,6 +176,7 @@ def add_customer_returns_tab(root) tab = Tab.new(customer_returns_tab_config). with_active_check. + with_completed_check. with_index_availability_check(::Spree::CustomerReturn) root.add(tab) diff --git a/app/views/spree/admin/shared/_order_tabs.html.erb b/app/views/spree/admin/shared/_order_tabs.html.erb index 7e94dee4b4..76afce781c 100644 --- a/app/views/spree/admin/shared/_order_tabs.html.erb +++ b/app/views/spree/admin/shared/_order_tabs.html.erb @@ -6,13 +6,15 @@ <% content_for :page_tabs do %> <% order_tabs.items.each do |tab| %> <% next unless tab.available?(current_ability, @order) %> - <%= content_tag :li, class: 'nav-item', data: { hook: tab.data_hook } do %> - <%= link_to_with_icon( - tab.icon_name, - tab.text, - tab.url(@order), - class: tab.active?(current) ? "active #{tab.classes}" : tab.classes - ) %> + <% next unless tab.complete?(@order) %> + <%= content_tag :li, class: 'nav-item', data: { hook: tab.data_hook } do %> + <%= link_to_with_icon( + tab.icon_name, + tab.text, + tab.url(@order), + class: tab.active?(current) ? "active #{tab.classes}" : tab.classes + ) %> <% end %> + <% end %> <% end %> <% end %> From 934e845586cc4b5b3d06017c457cf29c5648517e Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 4 Oct 2023 17:07:51 +0200 Subject: [PATCH 61/98] Introduce TabBuilder. Split responsibilities between Tab and TabBuilder. --- .../admin/tabs/order_default_tabs_builder.rb | 45 +++++++++------- .../tabs/product_default_tabs_builder.rb | 40 +++++++++------ app/models/spree/admin/tabs/tab.rb | 31 +++++------ app/models/spree/admin/tabs/tab_builder.rb | 51 +++++++++++++++++++ .../admin/tabs/user_default_tabs_builder.rb | 25 +++++---- 5 files changed, 129 insertions(+), 63 deletions(-) create mode 100644 app/models/spree/admin/tabs/tab_builder.rb diff --git a/app/models/spree/admin/tabs/order_default_tabs_builder.rb b/app/models/spree/admin/tabs/order_default_tabs_builder.rb index 3fc8f919c5..fed23e5f43 100644 --- a/app/models/spree/admin/tabs/order_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/order_default_tabs_builder.rb @@ -23,7 +23,7 @@ def build def add_cart_tab(root) tab = - Tab.new(cart_tab_config). + TabBuilder.new(cart_tab_config). with_active_check. with_availability_check( # An abstract module should not be aware of resource's internal structure. @@ -32,7 +32,8 @@ def add_cart_tab(root) ability.can?(:update, resource) && (resource.shipments.empty? || resource.shipments.shipped.empty?) end ). - with_data_hook('admin_order_tabs_cart_details') + with_data_hook('admin_order_tabs_cart_details'). + build root.add(tab) end @@ -49,10 +50,11 @@ def cart_tab_config def add_channel_tab(root) tab = - Tab.new(channel_tab_config). + TabBuilder.new(channel_tab_config). with_active_check. with_update_availability_check. - with_data_hook('admin_order_tabs_channel_details') + with_data_hook('admin_order_tabs_channel_details'). + build root.add(tab) end @@ -69,14 +71,15 @@ def channel_tab_config def add_customer_tab(root) tab = - Tab.new(customer_tab_config). + TabBuilder.new(customer_tab_config). with_active_check. with_availability_check( lambda do |ability, resource| ability.can?(:update, resource) && resource.checkout_steps.include?('address') end ). - with_data_hook('admin_order_tabs_customer_details') + with_data_hook('admin_order_tabs_customer_details'). + build root.add(tab) end @@ -93,10 +96,11 @@ def customer_tab_config def add_shipments_tab(root) tab = - Tab.new(shipments_tab_config). + TabBuilder.new(shipments_tab_config). with_active_check. with_update_availability_check. - with_data_hook('admin_order_tabs_shipment_details') + with_data_hook('admin_order_tabs_shipment_details'). + build root.add(tab) end @@ -113,10 +117,11 @@ def shipments_tab_config def add_adjustments_tab(root) tab = - Tab.new(adjustments_tab_config). + TabBuilder.new(adjustments_tab_config). with_active_check. with_index_availability_check(::Spree::Adjustment). - with_data_hook('admin_order_tabs_adjustments') + with_data_hook('admin_order_tabs_adjustments'). + build root.add(tab) end @@ -133,10 +138,11 @@ def adjustments_tab_config def add_payments_tab(root) tab = - Tab.new(payments_tab_config). + TabBuilder.new(payments_tab_config). with_active_check. with_index_availability_check(::Spree::Payment). - with_data_hook('admin_order_tabs_payments') + with_data_hook('admin_order_tabs_payments'). + build root.add(tab) end @@ -153,11 +159,12 @@ def payments_tab_config def add_return_authorizations_tab(root) tab = - Tab.new(return_authorizations_tab_config). + TabBuilder.new(return_authorizations_tab_config). with_active_check. with_completed_check. with_index_availability_check(::Spree::ReturnAuthorization). - with_data_hook('admin_order_tabs_return_authorizations') + with_data_hook('admin_order_tabs_return_authorizations'). + build root.add(tab) end @@ -174,10 +181,11 @@ def return_authorizations_tab_config def add_customer_returns_tab(root) tab = - Tab.new(customer_returns_tab_config). + TabBuilder.new(customer_returns_tab_config). with_active_check. with_completed_check. - with_index_availability_check(::Spree::CustomerReturn) + with_index_availability_check(::Spree::CustomerReturn). + build root.add(tab) end @@ -194,10 +202,11 @@ def customer_returns_tab_config def add_state_changes_tab(root) tab = - Tab.new(state_changes_tab_config). + TabBuilder.new(state_changes_tab_config). with_active_check. with_update_availability_check. - with_data_hook('admin_order_tabs_state_changes') + with_data_hook('admin_order_tabs_state_changes'). + build root.add(tab) end diff --git a/app/models/spree/admin/tabs/product_default_tabs_builder.rb b/app/models/spree/admin/tabs/product_default_tabs_builder.rb index b278ff0d41..5645d3d8df 100644 --- a/app/models/spree/admin/tabs/product_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/product_default_tabs_builder.rb @@ -22,9 +22,10 @@ def build def add_details_tab(root) tab = - Tab.new(details_config). + TabBuilder.new(details_config). with_active_check. - with_admin_availability_check(::Spree::Product) + with_admin_availability_check(::Spree::Product). + build root.add(tab) end @@ -41,13 +42,14 @@ def details_config def add_images_tab(root) tab = - Tab.new(images_config). + TabBuilder.new(images_config). with_active_check. with_availability_check( lambda do |ability, resource| ability.can?(:admin, ::Spree::Image) && !resource.deleted? end - ) + ). + build root.add(tab) end @@ -64,13 +66,14 @@ def images_config def add_variants_tab(root) tab = - Tab.new(variants_config). + TabBuilder.new(variants_config). with_active_check. with_availability_check( lambda do |ability, resource| ability.can?(:admin, ::Spree::Variant) && !resource.deleted? end - ) + ). + build root.add(tab) end @@ -87,13 +90,14 @@ def variants_config def add_properties_tab(root) tab = - Tab.new(properties_config). + TabBuilder.new(properties_config). with_active_check. with_availability_check( lambda do |ability, resource| ability.can?(:admin, ::Spree::ProductProperty) && !resource.deleted? end - ) + ). + build root.add(tab) end @@ -110,13 +114,14 @@ def properties_config def add_stock_tab(root) tab = - Tab.new(stock_config). + TabBuilder.new(stock_config). with_active_check. with_availability_check( lambda do |ability, resource| ability.can?(:admin, ::Spree::StockItem) && !resource.deleted? end - ) + ). + build root.add(tab) end @@ -133,13 +138,14 @@ def stock_config def add_prices_tab(root) tab = - Tab.new(prices_config). + TabBuilder.new(prices_config). with_active_check. with_availability_check( lambda do |ability, resource| ability.can?(:admin, ::Spree::Price) && !resource.deleted? end - ) + ). + build root.add(tab) end @@ -156,13 +162,14 @@ def prices_config def add_digitals_tab(root) tab = - Tab.new(digitals_config). + TabBuilder.new(digitals_config). with_active_check. with_availability_check( lambda do |ability, resource| ability.can?(:admin, ::Spree::Digital) && !resource.deleted? end - ) + ). + build root.add(tab) end @@ -179,13 +186,14 @@ def digitals_config def add_translations_tab(root) tab = - Tab.new(translations_config). + TabBuilder.new(translations_config). with_active_check. with_availability_check( lambda do |ability, resource| ability.can?(:admin, ::Spree::Product) && !resource.deleted? end - ) + ). + build root.add(tab) end diff --git a/app/models/spree/admin/tabs/tab.rb b/app/models/spree/admin/tabs/tab.rb index ea7a50667a..a67bccaf44 100644 --- a/app/models/spree/admin/tabs/tab.rb +++ b/app/models/spree/admin/tabs/tab.rb @@ -2,18 +2,19 @@ module Spree module Admin module Tabs class Tab - include ConditionalChecker - include AvailabilityBuilderMethods - include DataHook - - attr_reader :icon_name, :name, :classes, :partial_name + attr_reader :icon_name, :name, :classes, :text, :data_hook def initialize(config) - @icon_name = config[:icon_name] - @name = config[:name] - @url = config[:url] - @classes = config[:classes] - @partial_name = config[:partial_name] + @icon_name = config[:icon_name] + @name = config[:name] + @url = config[:url] + @classes = config[:classes] + @partial_name = config[:partial_name] + @availability_check = config[:availability_check] + @active_check = config[:active_check] + @completed_check = config[:completed_check] + @text = config[:text] + @data_hook = config[:data_hook] end def available?(current_ability, resource) @@ -27,7 +28,7 @@ def url(resource = nil) end def active?(current_tab) - @active_check.call(current_tab, partial_name) + @active_check.call(current_tab, @partial_name) end def complete?(resource) @@ -35,14 +36,6 @@ def complete?(resource) @completed_check.call(resource) end - - def text - ::Spree.t(name) - end - - def data_hook - @data_hook.presence - end end end end diff --git a/app/models/spree/admin/tabs/tab_builder.rb b/app/models/spree/admin/tabs/tab_builder.rb new file mode 100644 index 0000000000..cd468cdf2b --- /dev/null +++ b/app/models/spree/admin/tabs/tab_builder.rb @@ -0,0 +1,51 @@ +module Spree + module Admin + module Tabs + class TabBuilder + include ConditionalChecker + include AvailabilityBuilderMethods + include DataHook + + def initialize(config) + @icon_name = config[:icon_name] + @name = config[:name] + @url = config[:url] + @classes = config[:classes] + @partial_name = config[:partial_name] + @availability_check = nil + @active_check = nil + @completed_check = nil + end + + def build + Tab.new(build_config) + end + + private + + def build_config + { + icon_name: @icon_name, + name: @name, + url: @url, + classes: @classes, + partial_name: @partial_name, + availability_check: @availability_check, + active_check: @active_check, + completed_check: @completed_check, + text: text, + data_hook: data_hook + } + end + + def text + ::Spree.t(@name) + end + + def data_hook + @data_hook.presence + end + end + end + end +end diff --git a/app/models/spree/admin/tabs/user_default_tabs_builder.rb b/app/models/spree/admin/tabs/user_default_tabs_builder.rb index 73adf57e87..0339f8555f 100644 --- a/app/models/spree/admin/tabs/user_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/user_default_tabs_builder.rb @@ -18,8 +18,9 @@ def build def add_account_tab(root) tab = - Tab.new(account_config). - with_active_check + TabBuilder.new(account_config). + with_active_check. + build root.add(tab) end @@ -36,8 +37,9 @@ def account_config def add_addresses_tab(root) tab = - Tab.new(addresses_config). - with_active_check + TabBuilder.new(addresses_config). + with_active_check. + build root.add(tab) end @@ -54,8 +56,9 @@ def addresses_config def add_orders_tab(root) tab = - Tab.new(orders_config). - with_active_check + TabBuilder.new(orders_config). + with_active_check. + build root.add(tab) end @@ -72,8 +75,9 @@ def orders_config def add_items_tab(root) tab = - Tab.new(items_config). - with_active_check + TabBuilder.new(items_config). + with_active_check. + build root.add(tab) end @@ -90,8 +94,9 @@ def items_config def add_store_credits_tab(root) tab = - Tab.new(store_credits_config). - with_active_check + TabBuilder.new(store_credits_config). + with_active_check. + build root.add(tab) end From 3b36fb9450eb6107eacaec42ea9c7d10adc84027 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 4 Oct 2023 17:19:31 +0200 Subject: [PATCH 62/98] Fix .complete? check --- .../spree/admin/shared/_order_tabs.html.erb | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/app/views/spree/admin/shared/_order_tabs.html.erb b/app/views/spree/admin/shared/_order_tabs.html.erb index 76afce781c..36871b7678 100644 --- a/app/views/spree/admin/shared/_order_tabs.html.erb +++ b/app/views/spree/admin/shared/_order_tabs.html.erb @@ -5,16 +5,14 @@ <% content_for :page_tabs do %> <% order_tabs.items.each do |tab| %> - <% next unless tab.available?(current_ability, @order) %> - <% next unless tab.complete?(@order) %> - <%= content_tag :li, class: 'nav-item', data: { hook: tab.data_hook } do %> - <%= link_to_with_icon( - tab.icon_name, - tab.text, - tab.url(@order), - class: tab.active?(current) ? "active #{tab.classes}" : tab.classes - ) %> + <% next unless (tab.available?(current_ability, @order) && tab.complete?(@order)) %> + <%= content_tag :li, class: 'nav-item', data: { hook: tab.data_hook } do %> + <%= link_to_with_icon( + tab.icon_name, + tab.text, + tab.url(@order), + class: tab.active?(current) ? "active #{tab.classes}" : tab.classes + ) %> <% end %> - <% end %> <% end %> <% end %> From 57e054ffd7cf4b42eef9f58ae381102e85d8a05b Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 4 Oct 2023 17:23:03 +0200 Subject: [PATCH 63/98] Lint fix --- app/models/spree/admin/tabs/tab.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/spree/admin/tabs/tab.rb b/app/models/spree/admin/tabs/tab.rb index a67bccaf44..a7de0dc054 100644 --- a/app/models/spree/admin/tabs/tab.rb +++ b/app/models/spree/admin/tabs/tab.rb @@ -12,7 +12,7 @@ def initialize(config) @partial_name = config[:partial_name] @availability_check = config[:availability_check] @active_check = config[:active_check] - @completed_check = config[:completed_check] + @completed_check = config[:completed_check] @text = config[:text] @data_hook = config[:data_hook] end From e489e1afc3d34f083369219e81782a97b1f81a45 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 4 Oct 2023 17:26:20 +0200 Subject: [PATCH 64/98] Improve formatting --- app/models/spree/admin/tabs/tab_builder.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/models/spree/admin/tabs/tab_builder.rb b/app/models/spree/admin/tabs/tab_builder.rb index cd468cdf2b..aa71068ccd 100644 --- a/app/models/spree/admin/tabs/tab_builder.rb +++ b/app/models/spree/admin/tabs/tab_builder.rb @@ -7,14 +7,14 @@ class TabBuilder include DataHook def initialize(config) - @icon_name = config[:icon_name] - @name = config[:name] - @url = config[:url] - @classes = config[:classes] - @partial_name = config[:partial_name] + @icon_name = config[:icon_name] + @name = config[:name] + @url = config[:url] + @classes = config[:classes] + @partial_name = config[:partial_name] @availability_check = nil - @active_check = nil - @completed_check = nil + @active_check = nil + @completed_check = nil end def build From 6c2f96c78fa4c420e548369ea12651442a49ccb4 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 4 Oct 2023 17:43:23 +0200 Subject: [PATCH 65/98] Remove unnecessary test --- spec/models/spree/admin/tabs/tab_spec.rb | 8 -------- 1 file changed, 8 deletions(-) diff --git a/spec/models/spree/admin/tabs/tab_spec.rb b/spec/models/spree/admin/tabs/tab_spec.rb index c894d1d440..c4ec4de0e9 100644 --- a/spec/models/spree/admin/tabs/tab_spec.rb +++ b/spec/models/spree/admin/tabs/tab_spec.rb @@ -46,14 +46,6 @@ module Admin end end - describe '#partial_name' do - subject { tab.partial_name } - - it 'returns partial_name' do - expect(subject).to eq(config[:partial_name]) - end - end - describe '#available?' do subject { tab.available?(ability, resource) } From 6d34140af023d3318f12f9ab2548bc0a5d7091d4 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 4 Oct 2023 18:35:47 +0200 Subject: [PATCH 66/98] Cleanup tab test --- spec/models/spree/admin/tabs/tab_spec.rb | 56 +++++++++++------------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/spec/models/spree/admin/tabs/tab_spec.rb b/spec/models/spree/admin/tabs/tab_spec.rb index c4ec4de0e9..4331121e60 100644 --- a/spec/models/spree/admin/tabs/tab_spec.rb +++ b/spec/models/spree/admin/tabs/tab_spec.rb @@ -4,7 +4,7 @@ module Spree module Admin describe Tabs::Tab, type: :model do let(:tab) { described_class.new(config) } - let(:config) do + let(:config_base) do { icon_name: 'cart-check.svg', name: 'Cart', @@ -13,6 +13,10 @@ module Admin partial_name: :cart } end + let(:additional_config) { {} } + let(:config) do + config_base.merge(additional_config) + end describe '#icon_name' do subject { tab.icon_name } @@ -58,19 +62,11 @@ module Admin end end - context 'when availability check returns true' do - before do - tab.with_availability_check(->(_ability, _resource) { true }) - end - - it 'returns true' do - expect(subject).to be(true) - end - end - context 'when availability check returns false' do - before do - tab.with_availability_check(->(_ability, _resource) { false }) + let(:additional_config) do + { + availability_check: ->(_ability, _resource) { false } + } end it 'returns false' do @@ -82,9 +78,12 @@ module Admin describe '#active?' do subject { tab.active?(current_tab) } - before { tab.with_active_check } - - context 'when tab matches the current tab' do + context 'when active check returns true' do + let(:additional_config) do + { + active_check: ->(_current_tab, _text) { true } + } + end let(:current_tab) { config[:partial_name] } it 'returns true' do @@ -92,7 +91,12 @@ module Admin end end - context 'when tab does not match the current tab' do + context 'when active check returns false' do + let(:additional_config) do + { + active_check: ->(_current_tab, _text) { false } + } + end let(:current_tab) { 'non-matching' } it 'returns false' do @@ -106,26 +110,16 @@ module Admin let(:resource) { double } context 'when complete check is not set' do - it 'is returns true' do - expect(subject).to be(true) - end - end - - context 'when complete check returns true' do - before do - tab.with_completed_check - allow(resource).to receive(:completed?).and_return(true) - end - it 'returns true' do expect(subject).to be(true) end end context 'when complete check returns false' do - before do - tab.with_completed_check - allow(resource).to receive(:completed?).and_return(false) + let(:additional_config) do + { + completed_check: ->(_resource) { false } + } end it 'returns false' do From 94b4157f15c38ad8d48e958822749cbc58c0030a Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 4 Oct 2023 18:45:52 +0200 Subject: [PATCH 67/98] Refactor test to meet rubocop expectations --- spec/models/spree/admin/tabs/tab_spec.rb | 36 +++++++----------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/spec/models/spree/admin/tabs/tab_spec.rb b/spec/models/spree/admin/tabs/tab_spec.rb index 4331121e60..95a8d6d166 100644 --- a/spec/models/spree/admin/tabs/tab_spec.rb +++ b/spec/models/spree/admin/tabs/tab_spec.rb @@ -4,19 +4,19 @@ module Spree module Admin describe Tabs::Tab, type: :model do let(:tab) { described_class.new(config) } - let(:config_base) do + let(:config) do { icon_name: 'cart-check.svg', name: 'Cart', url: '/cart', classes: 'nav-link', - partial_name: :cart + partial_name: :cart, + availability_check: check, + active_check: check, + completed_check: check } end - let(:additional_config) { {} } - let(:config) do - config_base.merge(additional_config) - end + let(:check) { nil } describe '#icon_name' do subject { tab.icon_name } @@ -63,11 +63,7 @@ module Admin end context 'when availability check returns false' do - let(:additional_config) do - { - availability_check: ->(_ability, _resource) { false } - } - end + let(:check) { ->(_ability, _resource) { false } } it 'returns false' do expect(subject).to be(false) @@ -79,11 +75,7 @@ module Admin subject { tab.active?(current_tab) } context 'when active check returns true' do - let(:additional_config) do - { - active_check: ->(_current_tab, _text) { true } - } - end + let(:check) { ->(_current_tab, _text) { true } } let(:current_tab) { config[:partial_name] } it 'returns true' do @@ -92,11 +84,7 @@ module Admin end context 'when active check returns false' do - let(:additional_config) do - { - active_check: ->(_current_tab, _text) { false } - } - end + let(:check) { ->(_current_tab, _text) { false } } let(:current_tab) { 'non-matching' } it 'returns false' do @@ -116,11 +104,7 @@ module Admin end context 'when complete check returns false' do - let(:additional_config) do - { - completed_check: ->(_resource) { false } - } - end + let(:check) { ->(_resource) { false } } it 'returns false' do expect(subject).to be(false) From 68026ae49d3c4885c8e7fc5cc4e1b44fee918058 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Thu, 5 Oct 2023 07:36:10 +0200 Subject: [PATCH 68/98] Spell fix --- spec/features/admin/products/edit/products_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/features/admin/products/edit/products_spec.rb b/spec/features/admin/products/edit/products_spec.rb index 33b015387c..dc1a7f5306 100644 --- a/spec/features/admin/products/edit/products_spec.rb +++ b/spec/features/admin/products/edit/products_spec.rb @@ -19,7 +19,7 @@ end end - context 'editing a product with WYSIWYG editer enabled' do + context 'editing a product with WYSIWYG editor enabled' do before do Spree::Backend::Config.product_wysiwyg_editor_enabled = true visit spree.admin_product_path(product) From b7903a3c2160c90672e11dd915514978013e96a1 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Thu, 12 Oct 2023 12:36:17 +0200 Subject: [PATCH 69/98] Add missing tests --- spec/models/spree/admin/tabs/tab_spec.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/spec/models/spree/admin/tabs/tab_spec.rb b/spec/models/spree/admin/tabs/tab_spec.rb index 95a8d6d166..fc2bb69ddd 100644 --- a/spec/models/spree/admin/tabs/tab_spec.rb +++ b/spec/models/spree/admin/tabs/tab_spec.rb @@ -13,7 +13,9 @@ module Admin partial_name: :cart, availability_check: check, active_check: check, - completed_check: check + completed_check: check, + text: 'Cart', + data_hook: 'data_hook' } end let(:check) { nil } @@ -50,6 +52,22 @@ module Admin end end + describe '#text' do + subject { tab.text } + + it 'returns text' do + expect(subject).to eq(config[:text]) + end + end + + describe '#data_hook' do + subject { tab.data_hook } + + it 'returns classes' do + expect(subject).to eq(config[:data_hook]) + end + end + describe '#available?' do subject { tab.available?(ability, resource) } From 2075114fd5929a9b10dc96e4acd9fd1286028b2b Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 17 Oct 2023 08:37:57 +0200 Subject: [PATCH 70/98] Adhere to naming convention in tests --- spec/models/spree/admin/tabs/root_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/spree/admin/tabs/root_spec.rb b/spec/models/spree/admin/tabs/root_spec.rb index 84b6617e34..b749162ff2 100644 --- a/spec/models/spree/admin/tabs/root_spec.rb +++ b/spec/models/spree/admin/tabs/root_spec.rb @@ -5,7 +5,7 @@ module Admin describe Tabs::Root, type: :model do let(:root) { described_class.new } - describe '#add(item)' do + describe '#add' do let(:item) { double(name: 'test') } context "when there's no item with a particular key" do From 63e847e2317e84803965aa0cea51e774fbd8c576 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 17 Oct 2023 09:22:59 +0200 Subject: [PATCH 71/98] Cleanup - remove superfluous method --- app/models/spree/admin/main_menu/root.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/models/spree/admin/main_menu/root.rb b/app/models/spree/admin/main_menu/root.rb index 86af3528e1..fd2823aa50 100644 --- a/app/models/spree/admin/main_menu/root.rb +++ b/app/models/spree/admin/main_menu/root.rb @@ -7,10 +7,6 @@ class Root < Section def initialize super('root', 'root', nil, nil, []) end - - def add_to_section(section_key, item) - @items.find { |e| e.key == section_key }.add_item(item) - end end end end From dbccb049decb3000a6c83fa5b2d035d6110f7279 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 17 Oct 2023 12:23:52 +0200 Subject: [PATCH 72/98] Rename an attribute --- .../admin/tabs/order_default_tabs_builder.rb | 18 +++++++++--------- .../admin/tabs/product_default_tabs_builder.rb | 16 ++++++++-------- app/models/spree/admin/tabs/root.rb | 6 +++--- app/models/spree/admin/tabs/tab.rb | 4 ++-- app/models/spree/admin/tabs/tab_builder.rb | 6 +++--- .../admin/tabs/user_default_tabs_builder.rb | 10 +++++----- 6 files changed, 30 insertions(+), 30 deletions(-) diff --git a/app/models/spree/admin/tabs/order_default_tabs_builder.rb b/app/models/spree/admin/tabs/order_default_tabs_builder.rb index fed23e5f43..c3748f9138 100644 --- a/app/models/spree/admin/tabs/order_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/order_default_tabs_builder.rb @@ -41,7 +41,7 @@ def add_cart_tab(root) def cart_tab_config { icon_name: 'cart-check.svg', - name: :cart, + key: :cart, url: ->(resource) { cart_admin_order_path(resource) }, classes: 'nav-link', partial_name: :cart @@ -62,7 +62,7 @@ def add_channel_tab(root) def channel_tab_config { icon_name: 'funnel.svg', - name: :channel, + key: :channel, url: ->(resource) { channel_admin_order_path(resource) }, classes: 'nav-link', partial_name: :channel @@ -87,7 +87,7 @@ def add_customer_tab(root) def customer_tab_config { icon_name: 'person-lines-fill.svg', - name: :customer, + key: :customer, url: ->(resource) { admin_order_customer_path(resource) }, classes: 'nav-link', partial_name: :customer_details @@ -108,7 +108,7 @@ def add_shipments_tab(root) def shipments_tab_config { icon_name: 'truck.svg', - name: :shipments, + key: :shipments, url: ->(resource) { edit_admin_order_path(resource) }, classes: 'nav-link', partial_name: :shipments @@ -129,7 +129,7 @@ def add_adjustments_tab(root) def adjustments_tab_config { icon_name: 'adjust.svg', - name: :adjustments, + key: :adjustments, url: ->(resource) { admin_order_adjustments_path(resource) }, classes: 'nav-link', partial_name: :adjustments @@ -150,7 +150,7 @@ def add_payments_tab(root) def payments_tab_config { icon_name: 'credit-card.svg', - name: :payments, + key: :payments, url: ->(resource) { admin_order_payments_path(resource) }, classes: 'nav-link', partial_name: :payments @@ -172,7 +172,7 @@ def add_return_authorizations_tab(root) def return_authorizations_tab_config { icon_name: 'enter.svg', - name: :return_authorizations, + key: :return_authorizations, url: ->(resource) { admin_order_return_authorizations_path(resource) }, classes: 'nav-link', partial_name: :return_authorizations @@ -193,7 +193,7 @@ def add_customer_returns_tab(root) def customer_returns_tab_config { icon_name: 'returns.svg', - name: :customer_returns, + key: :customer_returns, url: ->(resource) { admin_order_customer_returns_path(resource) }, classes: 'nav-link', partial_name: :customer_returns @@ -214,7 +214,7 @@ def add_state_changes_tab(root) def state_changes_tab_config { icon_name: 'calendar3.svg', - name: :state_changes, + key: :state_changes, url: ->(resource) { admin_order_state_changes_path(resource) }, classes: 'nav-link', partial_name: :state_changes diff --git a/app/models/spree/admin/tabs/product_default_tabs_builder.rb b/app/models/spree/admin/tabs/product_default_tabs_builder.rb index 5645d3d8df..506283e87b 100644 --- a/app/models/spree/admin/tabs/product_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/product_default_tabs_builder.rb @@ -33,7 +33,7 @@ def add_details_tab(root) def details_config { icon_name: 'edit.svg', - name: :details, + key: :details, url: ->(resource) { edit_admin_product_path(resource) }, classes: 'nav-link', partial_name: :details @@ -57,7 +57,7 @@ def add_images_tab(root) def images_config { icon_name: 'images.svg', - name: :images, + key: :images, url: ->(resource) { admin_product_images_path(resource) }, classes: 'nav-link', partial_name: :images @@ -81,7 +81,7 @@ def add_variants_tab(root) def variants_config { icon_name: 'adjust.svg', - name: :variants, + key: :variants, url: ->(resource) { admin_product_variants_path(resource) }, classes: 'nav-link', partial_name: :variants @@ -105,7 +105,7 @@ def add_properties_tab(root) def properties_config { icon_name: 'list.svg', - name: :properties, + key: :properties, url: ->(resource) { admin_product_product_properties_path(resource) }, classes: 'nav-link', partial_name: :properties @@ -129,7 +129,7 @@ def add_stock_tab(root) def stock_config { icon_name: 'box-seam.svg', - name: :stock, + key: :stock, url: ->(resource) { stock_admin_product_path(resource) }, classes: 'nav-link', partial_name: :stock @@ -153,7 +153,7 @@ def add_prices_tab(root) def prices_config { icon_name: 'currency-exchange.svg', - name: :prices, + key: :prices, url: ->(resource) { admin_product_prices_path(resource) }, classes: 'nav-link', partial_name: :prices @@ -177,7 +177,7 @@ def add_digitals_tab(root) def digitals_config { icon_name: 'download.svg', - name: 'admin.digitals.digital_assets', + key: 'admin.digitals.digital_assets', url: ->(resource) { admin_product_digitals_path(resource) }, classes: 'nav-link', partial_name: :digitals @@ -201,7 +201,7 @@ def add_translations_tab(root) def translations_config { icon_name: 'translate.svg', - name: :translations, + key: :translations, url: ->(resource) { translations_admin_product_path(resource) }, classes: 'nav-link', partial_name: :translations diff --git a/app/models/spree/admin/tabs/root.rb b/app/models/spree/admin/tabs/root.rb index bb8781a250..ac31dc815d 100644 --- a/app/models/spree/admin/tabs/root.rb +++ b/app/models/spree/admin/tabs/root.rb @@ -9,15 +9,15 @@ def initialize end def add(item) - raise KeyError, "Item with key #{item.name} already exists" if index_for_name(item.name) + raise KeyError, "Item with key #{item.key} already exists" if index_for_key(item.key) @items << item end private - def index_for_name(name) - @items.index { |e| e.name == name } + def index_for_key(key) + @items.index { |e| e.key == key } end end end diff --git a/app/models/spree/admin/tabs/tab.rb b/app/models/spree/admin/tabs/tab.rb index a7de0dc054..4abf220670 100644 --- a/app/models/spree/admin/tabs/tab.rb +++ b/app/models/spree/admin/tabs/tab.rb @@ -2,11 +2,11 @@ module Spree module Admin module Tabs class Tab - attr_reader :icon_name, :name, :classes, :text, :data_hook + attr_reader :icon_name, :key, :classes, :text, :data_hook def initialize(config) @icon_name = config[:icon_name] - @name = config[:name] + @key = config[:key] @url = config[:url] @classes = config[:classes] @partial_name = config[:partial_name] diff --git a/app/models/spree/admin/tabs/tab_builder.rb b/app/models/spree/admin/tabs/tab_builder.rb index aa71068ccd..69d0f62ecc 100644 --- a/app/models/spree/admin/tabs/tab_builder.rb +++ b/app/models/spree/admin/tabs/tab_builder.rb @@ -8,7 +8,7 @@ class TabBuilder def initialize(config) @icon_name = config[:icon_name] - @name = config[:name] + @key = config[:key] @url = config[:url] @classes = config[:classes] @partial_name = config[:partial_name] @@ -26,7 +26,7 @@ def build def build_config { icon_name: @icon_name, - name: @name, + key: @key, url: @url, classes: @classes, partial_name: @partial_name, @@ -39,7 +39,7 @@ def build_config end def text - ::Spree.t(@name) + ::Spree.t(@key) end def data_hook diff --git a/app/models/spree/admin/tabs/user_default_tabs_builder.rb b/app/models/spree/admin/tabs/user_default_tabs_builder.rb index 0339f8555f..b7ba74f225 100644 --- a/app/models/spree/admin/tabs/user_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/user_default_tabs_builder.rb @@ -28,7 +28,7 @@ def add_account_tab(root) def account_config { icon_name: 'person.svg', - name: 'admin.user.account', + key: 'admin.user.account', url: ->(resource) { edit_admin_user_path(resource) }, classes: 'nav-link', partial_name: :account @@ -47,7 +47,7 @@ def add_addresses_tab(root) def addresses_config { icon_name: 'pin-map.svg', - name: 'admin.user.addresses', + key: 'admin.user.addresses', url: ->(resource) { addresses_admin_user_path(resource) }, classes: 'nav-link', partial_name: :address @@ -66,7 +66,7 @@ def add_orders_tab(root) def orders_config { icon_name: 'inbox.svg', - name: 'admin.user.orders', + key: 'admin.user.orders', url: ->(resource) { orders_admin_user_path(resource) }, classes: 'nav-link', partial_name: :orders @@ -85,7 +85,7 @@ def add_items_tab(root) def items_config { icon_name: 'tag.svg', - name: 'admin.user.items', + key: 'admin.user.items', url: ->(resource) { items_admin_user_path(resource) }, classes: 'nav-link', partial_name: :items @@ -104,7 +104,7 @@ def add_store_credits_tab(root) def store_credits_config { icon_name: 'gift.svg', - name: 'admin.user.store_credits', + key: 'admin.user.store_credits', url: ->(resource) { admin_user_store_credits_path(resource) }, classes: 'nav-link', partial_name: :store_credits From 474be4cbbe6dbe67b0e63ab520a6bff0b2459b67 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 17 Oct 2023 12:44:48 +0200 Subject: [PATCH 73/98] Fix spec after renaming an attribute --- spec/models/spree/admin/tabs/root_spec.rb | 2 +- spec/models/spree/admin/tabs/tab_spec.rb | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/models/spree/admin/tabs/root_spec.rb b/spec/models/spree/admin/tabs/root_spec.rb index b749162ff2..a3e0f37b63 100644 --- a/spec/models/spree/admin/tabs/root_spec.rb +++ b/spec/models/spree/admin/tabs/root_spec.rb @@ -6,7 +6,7 @@ module Admin let(:root) { described_class.new } describe '#add' do - let(:item) { double(name: 'test') } + let(:item) { double(key: 'test') } context "when there's no item with a particular key" do it 'appends an item' do diff --git a/spec/models/spree/admin/tabs/tab_spec.rb b/spec/models/spree/admin/tabs/tab_spec.rb index fc2bb69ddd..7d1c4170f1 100644 --- a/spec/models/spree/admin/tabs/tab_spec.rb +++ b/spec/models/spree/admin/tabs/tab_spec.rb @@ -7,7 +7,7 @@ module Admin let(:config) do { icon_name: 'cart-check.svg', - name: 'Cart', + key: 'Cart', url: '/cart', classes: 'nav-link', partial_name: :cart, @@ -28,11 +28,11 @@ module Admin end end - describe '#name' do - subject { tab.name } + describe '#key' do + subject { tab.key } - it 'returns name' do - expect(subject).to eq(config[:name]) + it 'returns key' do + expect(subject).to eq(config[:key]) end end From 927d42f1fd24ce062e4e786e35a92c8eb5a1aaef Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 17 Oct 2023 14:49:41 +0200 Subject: [PATCH 74/98] Create common abstraction for main_menu and tab items. --- app/models/spree/admin/item_manager.rb | 17 +++++++++++++++++ app/models/spree/admin/main_menu/root.rb | 2 ++ app/models/spree/admin/tabs/root.rb | 14 ++------------ 3 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 app/models/spree/admin/item_manager.rb diff --git a/app/models/spree/admin/item_manager.rb b/app/models/spree/admin/item_manager.rb new file mode 100644 index 0000000000..5a01f499f0 --- /dev/null +++ b/app/models/spree/admin/item_manager.rb @@ -0,0 +1,17 @@ +module Spree + module Admin + module ItemManager + def add(item) + raise KeyError, "Item with key #{item.key} already exists" if index_for_key(item.key) + + @items << item + end + + private + + def index_for_key(key) + @items.index { |e| e.key == key } + end + end + end +end diff --git a/app/models/spree/admin/main_menu/root.rb b/app/models/spree/admin/main_menu/root.rb index fd2823aa50..25b9ddb930 100644 --- a/app/models/spree/admin/main_menu/root.rb +++ b/app/models/spree/admin/main_menu/root.rb @@ -2,6 +2,8 @@ module Spree module Admin module MainMenu class Root < Section + include ::Spree::Admin::ItemManager + attr_reader :items def initialize diff --git a/app/models/spree/admin/tabs/root.rb b/app/models/spree/admin/tabs/root.rb index ac31dc815d..969ca69887 100644 --- a/app/models/spree/admin/tabs/root.rb +++ b/app/models/spree/admin/tabs/root.rb @@ -2,23 +2,13 @@ module Spree module Admin module Tabs class Root + include ::Spree::Admin::ItemManager + attr_reader :items def initialize @items = [] end - - def add(item) - raise KeyError, "Item with key #{item.key} already exists" if index_for_key(item.key) - - @items << item - end - - private - - def index_for_key(key) - @items.index { |e| e.key == key } - end end end end From c47e0ec8d8862c9720d4d988283d725b8076a3b4 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 17 Oct 2023 15:17:54 +0200 Subject: [PATCH 75/98] Add missing test --- .../models/spree/admin/main_menu/root_spec.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/spec/models/spree/admin/main_menu/root_spec.rb b/spec/models/spree/admin/main_menu/root_spec.rb index bb40cf7b36..020cddcca9 100644 --- a/spec/models/spree/admin/main_menu/root_spec.rb +++ b/spec/models/spree/admin/main_menu/root_spec.rb @@ -5,6 +5,25 @@ module Admin describe MainMenu::Root, type: :model do let(:root) { described_class.new } + describe '#add' do + let(:item) { double(key: 'test') } + + context "when there's no item with a particular key" do + it 'appends an item' do + root.add(item) + expect(root.items).to include(item) + end + end + + context 'when there is an item with a particular key' do + before { root.add(item) } + + it 'raises an error' do + expect { root.add(item) }.to raise_error(KeyError) + end + end + end + describe '#key' do subject { root.key } From 60db052d84e7efa86290b9b48f684a7fff9c4e67 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 17 Oct 2023 16:14:55 +0200 Subject: [PATCH 76/98] Remove superfluous methods --- app/models/spree/admin/main_menu/section.rb | 10 ------ .../spree/admin/main_menu/section_spec.rb | 32 ------------------- 2 files changed, 42 deletions(-) diff --git a/app/models/spree/admin/main_menu/section.rb b/app/models/spree/admin/main_menu/section.rb index 30b812d833..3e5e07c306 100644 --- a/app/models/spree/admin/main_menu/section.rb +++ b/app/models/spree/admin/main_menu/section.rb @@ -12,12 +12,6 @@ def initialize(key, label_translation_key, icon_key, availability_check, items) @items = items end - def add(item) - raise KeyError, "Item with key #{key} already exists" if index_for_key(item.key) - - @items << item - end - def child_with_key?(key) index_for_key(key).present? end @@ -56,10 +50,6 @@ def children? private - def index_for_key(key) - @items.index { |e| e.key == key } - end - def index_for_key!(key) item_index = index_for_key(key) raise KeyError, "Item not found for key #{key}" unless item_index diff --git a/spec/models/spree/admin/main_menu/section_spec.rb b/spec/models/spree/admin/main_menu/section_spec.rb index b555d262cf..345b21c687 100644 --- a/spec/models/spree/admin/main_menu/section_spec.rb +++ b/spec/models/spree/admin/main_menu/section_spec.rb @@ -83,38 +83,6 @@ module Admin end end - describe '#add' do - subject { section.add(item) } - let(:item) { double(key: 'test') } - - context 'when there are no child items' do - let(:items) { [] } - - it 'adds the item to the list' do - subject - expect(section.items.count).to eq(1) - end - end - - context 'when there already exists an item with the same key' do - let(:items) { [double(key: 'test')] } - - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - - context 'when there are other items with different keys' do - let(:items) { [double(key: 'other-key'), double(key: 'different-key') ]} - end - - it 'adds the item to the list' do - subject - expect(section.items.count).to eq(1) - expect(section.items.last.key).to eq('test') - end - end - describe '#remove' do subject { section.remove(key) } let(:key) { 'test-key' } From 4902cffde65f61c4fb9c4f39ccbd2c452ee907c5 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 17 Oct 2023 16:59:33 +0200 Subject: [PATCH 77/98] Abstract away #child_with_key? method --- app/models/spree/admin/item_manager.rb | 4 +++ app/models/spree/admin/main_menu/section.rb | 4 --- .../models/spree/admin/main_menu/root_spec.rb | 29 +++++++++++++++++++ .../spree/admin/main_menu/section_spec.rb | 21 -------------- spec/models/spree/admin/tabs/root_spec.rb | 29 +++++++++++++++++++ 5 files changed, 62 insertions(+), 25 deletions(-) diff --git a/app/models/spree/admin/item_manager.rb b/app/models/spree/admin/item_manager.rb index 5a01f499f0..dae044f264 100644 --- a/app/models/spree/admin/item_manager.rb +++ b/app/models/spree/admin/item_manager.rb @@ -7,6 +7,10 @@ def add(item) @items << item end + def child_with_key?(key) + index_for_key(key).present? + end + private def index_for_key(key) diff --git a/app/models/spree/admin/main_menu/section.rb b/app/models/spree/admin/main_menu/section.rb index 3e5e07c306..3e29a2705f 100644 --- a/app/models/spree/admin/main_menu/section.rb +++ b/app/models/spree/admin/main_menu/section.rb @@ -12,10 +12,6 @@ def initialize(key, label_translation_key, icon_key, availability_check, items) @items = items end - def child_with_key?(key) - index_for_key(key).present? - end - def remove(item_key) item_index = index_for_key!(item_key) diff --git a/spec/models/spree/admin/main_menu/root_spec.rb b/spec/models/spree/admin/main_menu/root_spec.rb index 020cddcca9..7f0cd80bf1 100644 --- a/spec/models/spree/admin/main_menu/root_spec.rb +++ b/spec/models/spree/admin/main_menu/root_spec.rb @@ -24,6 +24,35 @@ module Admin end end + describe '#child_with_key?' do + subject { root.child_with_key?(key) } + let(:key) { 'key' } + + context 'when an item with given key exists' do + let(:items) { [double(key: key), double(key: 'other-key')] } + + before do + items.each { |i| root.add(i) } + end + + it 'returns true' do + expect(subject).to be(true) + end + end + + context 'when an item with given key does not exist' do + let(:items) { [double(key: 'other-key')] } + + before do + items.each { |i| root.add(i) } + end + + it 'returns false' do + expect(subject).to be(false) + end + end + end + describe '#key' do subject { root.key } diff --git a/spec/models/spree/admin/main_menu/section_spec.rb b/spec/models/spree/admin/main_menu/section_spec.rb index 345b21c687..be0f5f940c 100644 --- a/spec/models/spree/admin/main_menu/section_spec.rb +++ b/spec/models/spree/admin/main_menu/section_spec.rb @@ -107,27 +107,6 @@ module Admin end end - describe '#child_with_key?' do - subject { section.child_with_key?(key) } - let(:key) { 'key' } - - context 'when an item with given key exists' do - let(:items) { [double(key: key), double(key: 'other-key')] } - - it 'returns true' do - expect(subject).to be(true) - end - end - - context 'when an item with given key does not exist' do - let(:items) { [double(key: 'other-key')] } - - it 'returns false' do - expect(subject).to be(false) - end - end - end - describe '#item_for_key' do subject { section.item_for_key(key) } let(:key) { 'key' } diff --git a/spec/models/spree/admin/tabs/root_spec.rb b/spec/models/spree/admin/tabs/root_spec.rb index a3e0f37b63..d0e254d7d1 100644 --- a/spec/models/spree/admin/tabs/root_spec.rb +++ b/spec/models/spree/admin/tabs/root_spec.rb @@ -23,6 +23,35 @@ module Admin end end end + + describe '#child_with_key?' do + subject { root.child_with_key?(key) } + let(:key) { 'key' } + + context 'when an item with given key exists' do + let(:items) { [double(key: key), double(key: 'other-key')] } + + before do + items.each { |i| root.add(i) } + end + + it 'returns true' do + expect(subject).to be(true) + end + end + + context 'when an item with given key does not exist' do + let(:items) { [double(key: 'other-key')] } + + before do + items.each { |i| root.add(i) } + end + + it 'returns false' do + expect(subject).to be(false) + end + end + end end end end From c354d4a092851278c5cd3b14b577c7db9c085dc2 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 17 Oct 2023 17:17:22 +0200 Subject: [PATCH 78/98] Simplify setup --- spec/models/spree/admin/tabs/root_spec.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/spec/models/spree/admin/tabs/root_spec.rb b/spec/models/spree/admin/tabs/root_spec.rb index d0e254d7d1..f5018b5ad1 100644 --- a/spec/models/spree/admin/tabs/root_spec.rb +++ b/spec/models/spree/admin/tabs/root_spec.rb @@ -28,13 +28,13 @@ module Admin subject { root.child_with_key?(key) } let(:key) { 'key' } + before do + items.each { |i| root.add(i) } + end + context 'when an item with given key exists' do let(:items) { [double(key: key), double(key: 'other-key')] } - before do - items.each { |i| root.add(i) } - end - it 'returns true' do expect(subject).to be(true) end @@ -43,10 +43,6 @@ module Admin context 'when an item with given key does not exist' do let(:items) { [double(key: 'other-key')] } - before do - items.each { |i| root.add(i) } - end - it 'returns false' do expect(subject).to be(false) end From 699e8863bdc6eac721a5d34c971d729b13813de4 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 17 Oct 2023 17:27:51 +0200 Subject: [PATCH 79/98] Abstract away #remove method --- app/models/spree/admin/item_manager.rb | 13 +++++++++ app/models/spree/admin/main_menu/section.rb | 6 ---- .../models/spree/admin/main_menu/root_spec.rb | 28 +++++++++++++++++++ .../spree/admin/main_menu/section_spec.rb | 24 ---------------- spec/models/spree/admin/tabs/root_spec.rb | 28 +++++++++++++++++++ 5 files changed, 69 insertions(+), 30 deletions(-) diff --git a/app/models/spree/admin/item_manager.rb b/app/models/spree/admin/item_manager.rb index dae044f264..c126d78758 100644 --- a/app/models/spree/admin/item_manager.rb +++ b/app/models/spree/admin/item_manager.rb @@ -11,11 +11,24 @@ def child_with_key?(key) index_for_key(key).present? end + def remove(item_key) + item_index = index_for_key!(item_key) + + @items.delete_at(item_index) + end + private def index_for_key(key) @items.index { |e| e.key == key } end + + def index_for_key!(key) + item_index = index_for_key(key) + raise KeyError, "Item not found for key #{key}" unless item_index + + item_index + end end end end diff --git a/app/models/spree/admin/main_menu/section.rb b/app/models/spree/admin/main_menu/section.rb index 3e29a2705f..e653dbd2da 100644 --- a/app/models/spree/admin/main_menu/section.rb +++ b/app/models/spree/admin/main_menu/section.rb @@ -12,12 +12,6 @@ def initialize(key, label_translation_key, icon_key, availability_check, items) @items = items end - def remove(item_key) - item_index = index_for_key!(item_key) - - @items.delete_at(item_index) - end - def item_for_key(key) @items.find { |e| e.key == key } end diff --git a/spec/models/spree/admin/main_menu/root_spec.rb b/spec/models/spree/admin/main_menu/root_spec.rb index 7f0cd80bf1..63b269711a 100644 --- a/spec/models/spree/admin/main_menu/root_spec.rb +++ b/spec/models/spree/admin/main_menu/root_spec.rb @@ -53,6 +53,34 @@ module Admin end end + describe '#remove' do + subject { root.remove(key) } + let(:key) { 'key' } + let(:other_key) { 'other-key' } + + before do + items.each { |i| root.add(i) } + end + + context 'when an item with given key exists' do + let(:items) { [double(key: key), double(key: other_key)] } + + it 'removes the item' do + subject + expect(root.items.count).to eq(1) + expect(root.items.first.key).to eq(other_key) + end + end + + context 'when an item with given key does not exist' do + let(:items) { [double(key: 'other-key')] } + + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + end + describe '#key' do subject { root.key } diff --git a/spec/models/spree/admin/main_menu/section_spec.rb b/spec/models/spree/admin/main_menu/section_spec.rb index be0f5f940c..000b19face 100644 --- a/spec/models/spree/admin/main_menu/section_spec.rb +++ b/spec/models/spree/admin/main_menu/section_spec.rb @@ -83,30 +83,6 @@ module Admin end end - describe '#remove' do - subject { section.remove(key) } - let(:key) { 'test-key' } - let(:other_key) { 'other-key' } - - context 'when the element is present in an array' do - let(:items) { [double(key: key), double(key: other_key)] } - - it 'removes the item' do - subject - expect(items.count).to eq(1) - expect(items.first.key).to eq(other_key) - end - end - - context 'when the element is not present in an array' do - let(:items) { [double(key: other_key)] } - - it 'removes the item' do - expect { subject }.to raise_error(KeyError) - end - end - end - describe '#item_for_key' do subject { section.item_for_key(key) } let(:key) { 'key' } diff --git a/spec/models/spree/admin/tabs/root_spec.rb b/spec/models/spree/admin/tabs/root_spec.rb index f5018b5ad1..8998b5226f 100644 --- a/spec/models/spree/admin/tabs/root_spec.rb +++ b/spec/models/spree/admin/tabs/root_spec.rb @@ -48,6 +48,34 @@ module Admin end end end + + describe '#remove' do + subject { root.remove(key) } + let(:key) { 'key' } + let(:other_key) { 'other-key' } + + before do + items.each { |i| root.add(i) } + end + + context 'when an item with given key exists' do + let(:items) { [double(key: key), double(key: other_key)] } + + it 'removes the item' do + subject + expect(root.items.count).to eq(1) + expect(root.items.first.key).to eq(other_key) + end + end + + context 'when an item with given key does not exist' do + let(:items) { [double(key: 'other-key')] } + + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + end end end end From 25f69705d7f9e4ceeebe6b765cd10e24bd7c17b8 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 17 Oct 2023 17:33:59 +0200 Subject: [PATCH 80/98] Abstract away #item_for_key method --- app/models/spree/admin/item_manager.rb | 4 +++ app/models/spree/admin/main_menu/section.rb | 4 --- .../models/spree/admin/main_menu/root_spec.rb | 26 +++++++++++++++++++ .../spree/admin/main_menu/section_spec.rb | 22 ---------------- spec/models/spree/admin/tabs/root_spec.rb | 26 +++++++++++++++++++ 5 files changed, 56 insertions(+), 26 deletions(-) diff --git a/app/models/spree/admin/item_manager.rb b/app/models/spree/admin/item_manager.rb index c126d78758..c760179c8f 100644 --- a/app/models/spree/admin/item_manager.rb +++ b/app/models/spree/admin/item_manager.rb @@ -17,6 +17,10 @@ def remove(item_key) @items.delete_at(item_index) end + def item_for_key(key) + @items.find { |e| e.key == key } + end + private def index_for_key(key) diff --git a/app/models/spree/admin/main_menu/section.rb b/app/models/spree/admin/main_menu/section.rb index e653dbd2da..9e9ce636ac 100644 --- a/app/models/spree/admin/main_menu/section.rb +++ b/app/models/spree/admin/main_menu/section.rb @@ -12,10 +12,6 @@ def initialize(key, label_translation_key, icon_key, availability_check, items) @items = items end - def item_for_key(key) - @items.find { |e| e.key == key } - end - def insert_before(item_key, item_to_add) item_index = index_for_key!(item_key) diff --git a/spec/models/spree/admin/main_menu/root_spec.rb b/spec/models/spree/admin/main_menu/root_spec.rb index 63b269711a..92eb75d2c0 100644 --- a/spec/models/spree/admin/main_menu/root_spec.rb +++ b/spec/models/spree/admin/main_menu/root_spec.rb @@ -81,6 +81,32 @@ module Admin end end + describe '#item_for_key' do + subject { root.item_for_key(key) } + let(:key) { 'key' } + + before do + items.each { |i| root.add(i) } + end + + context 'when an item with given key exists' do + let(:items) { [double(key: 'other-key'), item] } + let(:item) { double(key: key) } + + it 'returns the item' do + expect(subject).to be(item) + end + end + + context 'when an item with given key does not exist' do + let(:items) { [double(key: 'other-key')] } + + it 'returns nil' do + expect(subject).to be(nil) + end + end + end + describe '#key' do subject { root.key } diff --git a/spec/models/spree/admin/main_menu/section_spec.rb b/spec/models/spree/admin/main_menu/section_spec.rb index 000b19face..cfaffa4e99 100644 --- a/spec/models/spree/admin/main_menu/section_spec.rb +++ b/spec/models/spree/admin/main_menu/section_spec.rb @@ -83,28 +83,6 @@ module Admin end end - describe '#item_for_key' do - subject { section.item_for_key(key) } - let(:key) { 'key' } - - context 'when an item with given key exists' do - let(:items) { [double(key: 'other-key'), item] } - let(:item) { double(key: key) } - - it 'returns the item' do - expect(subject).to be(item) - end - end - - context 'when an item with given key does not exist' do - let(:items) { [double(key: 'other-key')] } - - it 'returns nil' do - expect(subject).to be(nil) - end - end - end - describe '#insert_before' do subject { section.insert_before(existing_key, item) } diff --git a/spec/models/spree/admin/tabs/root_spec.rb b/spec/models/spree/admin/tabs/root_spec.rb index 8998b5226f..128275c320 100644 --- a/spec/models/spree/admin/tabs/root_spec.rb +++ b/spec/models/spree/admin/tabs/root_spec.rb @@ -76,6 +76,32 @@ module Admin end end end + + describe '#item_for_key' do + subject { root.item_for_key(key) } + let(:key) { 'key' } + + before do + items.each { |i| root.add(i) } + end + + context 'when an item with given key exists' do + let(:items) { [double(key: 'other-key'), item] } + let(:item) { double(key: key) } + + it 'returns the item' do + expect(subject).to be(item) + end + end + + context 'when an item with given key does not exist' do + let(:items) { [double(key: 'other-key')] } + + it 'returns nil' do + expect(subject).to be(nil) + end + end + end end end end From c6d901a988798c21aa8b436740be5d9dad120960 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 17 Oct 2023 17:40:09 +0200 Subject: [PATCH 81/98] Abstract away #insert_before method --- app/models/spree/admin/item_manager.rb | 6 +++ app/models/spree/admin/main_menu/section.rb | 6 --- .../models/spree/admin/main_menu/root_spec.rb | 37 +++++++++++++++++++ .../spree/admin/main_menu/section_spec.rb | 32 ---------------- spec/models/spree/admin/tabs/root_spec.rb | 37 +++++++++++++++++++ 5 files changed, 80 insertions(+), 38 deletions(-) diff --git a/app/models/spree/admin/item_manager.rb b/app/models/spree/admin/item_manager.rb index c760179c8f..7afa7b6149 100644 --- a/app/models/spree/admin/item_manager.rb +++ b/app/models/spree/admin/item_manager.rb @@ -21,6 +21,12 @@ def item_for_key(key) @items.find { |e| e.key == key } end + def insert_before(item_key, item_to_add) + item_index = index_for_key!(item_key) + + @items.insert(item_index, item_to_add) + end + private def index_for_key(key) diff --git a/app/models/spree/admin/main_menu/section.rb b/app/models/spree/admin/main_menu/section.rb index 9e9ce636ac..475be526ad 100644 --- a/app/models/spree/admin/main_menu/section.rb +++ b/app/models/spree/admin/main_menu/section.rb @@ -12,12 +12,6 @@ def initialize(key, label_translation_key, icon_key, availability_check, items) @items = items end - def insert_before(item_key, item_to_add) - item_index = index_for_key!(item_key) - - @items.insert(item_index, item_to_add) - end - def insert_after(item_key, item_to_add) item_index = index_for_key!(item_key) diff --git a/spec/models/spree/admin/main_menu/root_spec.rb b/spec/models/spree/admin/main_menu/root_spec.rb index 92eb75d2c0..92e0f29bd7 100644 --- a/spec/models/spree/admin/main_menu/root_spec.rb +++ b/spec/models/spree/admin/main_menu/root_spec.rb @@ -107,6 +107,43 @@ module Admin end end + describe '#insert_before' do + subject { root.insert_before(existing_key, item) } + + let(:item) { double(key: inserted_key) } + let(:inserted_key) { 'test-item' } + let(:existing_key) { 'test-old' } + let(:items) { [] } + + before do + items.each { |i| root.add(i) } + end + + context 'when the list is empty' do + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + + context 'when an item with specified key does not exist' do + let(:items) { [double(key: 'other-key'), double(key: 'other-key2')] } + + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + + context 'when an item with specified key exists' do + let(:items) { [double(key: 'other-key'), double(key: existing_key)] } + + it 'inserts the item before the other item' do + subject + expect(root.items.count).to eq(3) + expect(root.items[1].key).to eq(inserted_key) + end + end + end + describe '#key' do subject { root.key } diff --git a/spec/models/spree/admin/main_menu/section_spec.rb b/spec/models/spree/admin/main_menu/section_spec.rb index cfaffa4e99..ba9319a695 100644 --- a/spec/models/spree/admin/main_menu/section_spec.rb +++ b/spec/models/spree/admin/main_menu/section_spec.rb @@ -83,38 +83,6 @@ module Admin end end - describe '#insert_before' do - subject { section.insert_before(existing_key, item) } - - let(:item) { double(key: inserted_key) } - let(:inserted_key) { 'test-item' } - let(:existing_key) { 'test-old' } - - context 'when the list is empty' do - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - - context 'when an item with specified key does not exist' do - let(:items) { [double(key: 'other-key'), double(key: 'other-key2')] } - - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - - context 'when an item with specified key exists' do - let(:items) { [double(key: 'other-key'), double(key: existing_key)] } - - it 'inserts the item before the other item' do - subject - expect(section.items.count).to eq(3) - expect(section.items[1].key).to eq(inserted_key) - end - end - end - describe '#insert_after' do subject { section.insert_after(existing_key, item) } diff --git a/spec/models/spree/admin/tabs/root_spec.rb b/spec/models/spree/admin/tabs/root_spec.rb index 128275c320..5eb8c0e3d7 100644 --- a/spec/models/spree/admin/tabs/root_spec.rb +++ b/spec/models/spree/admin/tabs/root_spec.rb @@ -102,6 +102,43 @@ module Admin end end end + + describe '#insert_before' do + subject { root.insert_before(existing_key, item) } + + let(:item) { double(key: inserted_key) } + let(:inserted_key) { 'test-item' } + let(:existing_key) { 'test-old' } + let(:items) { [] } + + before do + items.each { |i| root.add(i) } + end + + context 'when the list is empty' do + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + + context 'when an item with specified key does not exist' do + let(:items) { [double(key: 'other-key'), double(key: 'other-key2')] } + + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + + context 'when an item with specified key exists' do + let(:items) { [double(key: 'other-key'), double(key: existing_key)] } + + it 'inserts the item before the other item' do + subject + expect(root.items.count).to eq(3) + expect(root.items[1].key).to eq(inserted_key) + end + end + end end end end From af2e2faab518dc507c69bfdb000a7590d2d26b9d Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Tue, 17 Oct 2023 17:42:07 +0200 Subject: [PATCH 82/98] Abstract away #insert_after method --- app/models/spree/admin/item_manager.rb | 6 +++ app/models/spree/admin/main_menu/section.rb | 6 --- .../models/spree/admin/main_menu/root_spec.rb | 37 +++++++++++++++++++ .../spree/admin/main_menu/section_spec.rb | 32 ---------------- spec/models/spree/admin/tabs/root_spec.rb | 37 +++++++++++++++++++ 5 files changed, 80 insertions(+), 38 deletions(-) diff --git a/app/models/spree/admin/item_manager.rb b/app/models/spree/admin/item_manager.rb index 7afa7b6149..49d7c3f147 100644 --- a/app/models/spree/admin/item_manager.rb +++ b/app/models/spree/admin/item_manager.rb @@ -27,6 +27,12 @@ def insert_before(item_key, item_to_add) @items.insert(item_index, item_to_add) end + def insert_after(item_key, item_to_add) + item_index = index_for_key!(item_key) + + @items.insert(item_index + 1, item_to_add) + end + private def index_for_key(key) diff --git a/app/models/spree/admin/main_menu/section.rb b/app/models/spree/admin/main_menu/section.rb index 475be526ad..34b12ca7ac 100644 --- a/app/models/spree/admin/main_menu/section.rb +++ b/app/models/spree/admin/main_menu/section.rb @@ -12,12 +12,6 @@ def initialize(key, label_translation_key, icon_key, availability_check, items) @items = items end - def insert_after(item_key, item_to_add) - item_index = index_for_key!(item_key) - - @items.insert(item_index + 1, item_to_add) - end - def available?(current_ability, current_store) return true unless @availability_check.present? diff --git a/spec/models/spree/admin/main_menu/root_spec.rb b/spec/models/spree/admin/main_menu/root_spec.rb index 92e0f29bd7..e7254ab832 100644 --- a/spec/models/spree/admin/main_menu/root_spec.rb +++ b/spec/models/spree/admin/main_menu/root_spec.rb @@ -144,6 +144,43 @@ module Admin end end + describe '#insert_after' do + subject { root.insert_after(existing_key, item) } + + let(:item) { double(key: inserted_key) } + let(:inserted_key) { 'test-item' } + let(:existing_key) { 'test-old' } + let(:items) { [] } + + before do + items.each { |i| root.add(i) } + end + + context 'when the list is empty' do + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + + context 'when an item with specified key does not exist' do + let(:items) { [double(key: 'other-key'), double(key: 'other-key2')] } + + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + + context 'when an item with specified key exists' do + let(:items) { [double(key: existing_key), double(key: 'other-key')] } + + it 'inserts the item after the other item' do + subject + expect(root.items.count).to eq(3) + expect(root.items[1].key).to eq(inserted_key) + end + end + end + describe '#key' do subject { root.key } diff --git a/spec/models/spree/admin/main_menu/section_spec.rb b/spec/models/spree/admin/main_menu/section_spec.rb index ba9319a695..6619efab1e 100644 --- a/spec/models/spree/admin/main_menu/section_spec.rb +++ b/spec/models/spree/admin/main_menu/section_spec.rb @@ -82,38 +82,6 @@ module Admin end end end - - describe '#insert_after' do - subject { section.insert_after(existing_key, item) } - - let(:item) { double(key: inserted_key) } - let(:inserted_key) { 'test-item' } - let(:existing_key) { 'test-old' } - - context 'when the list is empty' do - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - - context 'when an item with specified key does not exist' do - let(:items) { [double(key: 'other-key'), double(key: 'other-key2')] } - - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - - context 'when an item with specified key exists' do - let(:items) { [double(key: existing_key), double(key: 'other-key')] } - - it 'inserts the item after the other item' do - subject - expect(section.items.count).to eq(3) - expect(section.items[1].key).to eq(inserted_key) - end - end - end end end end diff --git a/spec/models/spree/admin/tabs/root_spec.rb b/spec/models/spree/admin/tabs/root_spec.rb index 5eb8c0e3d7..333f313c5b 100644 --- a/spec/models/spree/admin/tabs/root_spec.rb +++ b/spec/models/spree/admin/tabs/root_spec.rb @@ -139,6 +139,43 @@ module Admin end end end + + describe '#insert_after' do + subject { root.insert_after(existing_key, item) } + + let(:item) { double(key: inserted_key) } + let(:inserted_key) { 'test-item' } + let(:existing_key) { 'test-old' } + let(:items) { [] } + + before do + items.each { |i| root.add(i) } + end + + context 'when the list is empty' do + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + + context 'when an item with specified key does not exist' do + let(:items) { [double(key: 'other-key'), double(key: 'other-key2')] } + + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + + context 'when an item with specified key exists' do + let(:items) { [double(key: existing_key), double(key: 'other-key')] } + + it 'inserts the item after the other item' do + subject + expect(root.items.count).to eq(3) + expect(root.items[1].key).to eq(inserted_key) + end + end + end end end end From 8430877792c0ad52157c51ec069cf7f57a3262f4 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 18 Oct 2023 08:27:54 +0200 Subject: [PATCH 83/98] Remove superfluous method --- app/models/spree/admin/main_menu/section.rb | 9 --------- 1 file changed, 9 deletions(-) diff --git a/app/models/spree/admin/main_menu/section.rb b/app/models/spree/admin/main_menu/section.rb index 34b12ca7ac..f5541c625d 100644 --- a/app/models/spree/admin/main_menu/section.rb +++ b/app/models/spree/admin/main_menu/section.rb @@ -21,15 +21,6 @@ def available?(current_ability, current_store) def children? @items.any? end - - private - - def index_for_key!(key) - item_index = index_for_key(key) - raise KeyError, "Item not found for key #{key}" unless item_index - - item_index - end end end end From b1c9aebcd922b9e4bb2620259fed5d123c21a53f Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 18 Oct 2023 09:47:19 +0200 Subject: [PATCH 84/98] Equalize spec setup --- spec/models/spree/admin/main_menu/root_spec.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/spec/models/spree/admin/main_menu/root_spec.rb b/spec/models/spree/admin/main_menu/root_spec.rb index e7254ab832..8c1490465b 100644 --- a/spec/models/spree/admin/main_menu/root_spec.rb +++ b/spec/models/spree/admin/main_menu/root_spec.rb @@ -28,13 +28,13 @@ module Admin subject { root.child_with_key?(key) } let(:key) { 'key' } + before do + items.each { |i| root.add(i) } + end + context 'when an item with given key exists' do let(:items) { [double(key: key), double(key: 'other-key')] } - before do - items.each { |i| root.add(i) } - end - it 'returns true' do expect(subject).to be(true) end @@ -43,10 +43,6 @@ module Admin context 'when an item with given key does not exist' do let(:items) { [double(key: 'other-key')] } - before do - items.each { |i| root.add(i) } - end - it 'returns false' do expect(subject).to be(false) end From 57d6c652f62c3c3efe512aca6d9aa853ff91a3d8 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 18 Oct 2023 09:49:30 +0200 Subject: [PATCH 85/98] Remove superfluous test --- spec/models/spree/admin/main_menu/root_spec.rb | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/spec/models/spree/admin/main_menu/root_spec.rb b/spec/models/spree/admin/main_menu/root_spec.rb index 8c1490465b..001ebaea35 100644 --- a/spec/models/spree/admin/main_menu/root_spec.rb +++ b/spec/models/spree/admin/main_menu/root_spec.rb @@ -211,24 +211,6 @@ module Admin expect(subject).to be(true) end end - - describe '#children?' do - subject { root.children? } - - context 'when there are child items' do - before { root.add(double(key: 'test')) } - - it 'returns true' do - expect(subject).to be(true) - end - end - - context 'when there are no child items' do - it 'returns false' do - expect(subject).to be(false) - end - end - end end end end From f19f5d9cb1794a2642cc39298edcbd3a92053f96 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 18 Oct 2023 10:06:16 +0200 Subject: [PATCH 86/98] Equalize root specs --- .../models/spree/admin/main_menu/root_spec.rb | 35 ++++++------------- spec/models/spree/admin/tabs/root_spec.rb | 35 ++++++------------- 2 files changed, 20 insertions(+), 50 deletions(-) diff --git a/spec/models/spree/admin/main_menu/root_spec.rb b/spec/models/spree/admin/main_menu/root_spec.rb index 001ebaea35..3885ed20a8 100644 --- a/spec/models/spree/admin/main_menu/root_spec.rb +++ b/spec/models/spree/admin/main_menu/root_spec.rb @@ -4,22 +4,29 @@ module Spree module Admin describe MainMenu::Root, type: :model do let(:root) { described_class.new } + let(:items) { [] } + + before do + items.each { |i| root.add(i) } + end describe '#add' do + subject { root.add(item) } let(:item) { double(key: 'test') } context "when there's no item with a particular key" do + it 'appends an item' do - root.add(item) + subject expect(root.items).to include(item) end end context 'when there is an item with a particular key' do - before { root.add(item) } + let(:items) { [item] } it 'raises an error' do - expect { root.add(item) }.to raise_error(KeyError) + expect { subject }.to raise_error(KeyError) end end end @@ -28,10 +35,6 @@ module Admin subject { root.child_with_key?(key) } let(:key) { 'key' } - before do - items.each { |i| root.add(i) } - end - context 'when an item with given key exists' do let(:items) { [double(key: key), double(key: 'other-key')] } @@ -54,10 +57,6 @@ module Admin let(:key) { 'key' } let(:other_key) { 'other-key' } - before do - items.each { |i| root.add(i) } - end - context 'when an item with given key exists' do let(:items) { [double(key: key), double(key: other_key)] } @@ -81,10 +80,6 @@ module Admin subject { root.item_for_key(key) } let(:key) { 'key' } - before do - items.each { |i| root.add(i) } - end - context 'when an item with given key exists' do let(:items) { [double(key: 'other-key'), item] } let(:item) { double(key: key) } @@ -109,11 +104,6 @@ module Admin let(:item) { double(key: inserted_key) } let(:inserted_key) { 'test-item' } let(:existing_key) { 'test-old' } - let(:items) { [] } - - before do - items.each { |i| root.add(i) } - end context 'when the list is empty' do it 'raises an error' do @@ -146,11 +136,6 @@ module Admin let(:item) { double(key: inserted_key) } let(:inserted_key) { 'test-item' } let(:existing_key) { 'test-old' } - let(:items) { [] } - - before do - items.each { |i| root.add(i) } - end context 'when the list is empty' do it 'raises an error' do diff --git a/spec/models/spree/admin/tabs/root_spec.rb b/spec/models/spree/admin/tabs/root_spec.rb index 333f313c5b..dd49ce4f67 100644 --- a/spec/models/spree/admin/tabs/root_spec.rb +++ b/spec/models/spree/admin/tabs/root_spec.rb @@ -4,22 +4,29 @@ module Spree module Admin describe Tabs::Root, type: :model do let(:root) { described_class.new } + let(:items) { [] } + + before do + items.each { |i| root.add(i) } + end describe '#add' do + subject { root.add(item) } let(:item) { double(key: 'test') } context "when there's no item with a particular key" do + it 'appends an item' do - root.add(item) + subject expect(root.items).to include(item) end end context 'when there is an item with a particular key' do - before { root.add(item) } + let(:items) { [item] } it 'raises an error' do - expect { root.add(item) }.to raise_error(KeyError) + expect { subject }.to raise_error(KeyError) end end end @@ -28,10 +35,6 @@ module Admin subject { root.child_with_key?(key) } let(:key) { 'key' } - before do - items.each { |i| root.add(i) } - end - context 'when an item with given key exists' do let(:items) { [double(key: key), double(key: 'other-key')] } @@ -54,10 +57,6 @@ module Admin let(:key) { 'key' } let(:other_key) { 'other-key' } - before do - items.each { |i| root.add(i) } - end - context 'when an item with given key exists' do let(:items) { [double(key: key), double(key: other_key)] } @@ -81,10 +80,6 @@ module Admin subject { root.item_for_key(key) } let(:key) { 'key' } - before do - items.each { |i| root.add(i) } - end - context 'when an item with given key exists' do let(:items) { [double(key: 'other-key'), item] } let(:item) { double(key: key) } @@ -109,11 +104,6 @@ module Admin let(:item) { double(key: inserted_key) } let(:inserted_key) { 'test-item' } let(:existing_key) { 'test-old' } - let(:items) { [] } - - before do - items.each { |i| root.add(i) } - end context 'when the list is empty' do it 'raises an error' do @@ -146,11 +136,6 @@ module Admin let(:item) { double(key: inserted_key) } let(:inserted_key) { 'test-item' } let(:existing_key) { 'test-old' } - let(:items) { [] } - - before do - items.each { |i| root.add(i) } - end context 'when the list is empty' do it 'raises an error' do From b458feb851bf13dd294d21f5bc45c9aad19552e5 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 18 Oct 2023 11:33:51 +0200 Subject: [PATCH 87/98] Introduce a shared abstraction for ability checks --- .../main_menu/availability_builder_methods.rb | 22 -------------- .../spree/admin/main_menu/item_builder.rb | 2 +- .../spree/admin/main_menu/section_builder.rb | 2 +- app/models/spree/admin/permission_checks.rb | 30 +++++++++++++++++++ .../tabs/availability_builder_methods.rb | 27 ----------------- .../admin/tabs/order_default_tabs_builder.rb | 14 ++++----- .../tabs/product_default_tabs_builder.rb | 2 +- app/models/spree/admin/tabs/tab_builder.rb | 2 +- 8 files changed, 41 insertions(+), 60 deletions(-) delete mode 100644 app/models/spree/admin/main_menu/availability_builder_methods.rb create mode 100644 app/models/spree/admin/permission_checks.rb delete mode 100644 app/models/spree/admin/tabs/availability_builder_methods.rb diff --git a/app/models/spree/admin/main_menu/availability_builder_methods.rb b/app/models/spree/admin/main_menu/availability_builder_methods.rb deleted file mode 100644 index 69a9bd2af7..0000000000 --- a/app/models/spree/admin/main_menu/availability_builder_methods.rb +++ /dev/null @@ -1,22 +0,0 @@ -module Spree - module Admin - module MainMenu - module AvailabilityBuilderMethods - def with_availability_check(availability_check) - @availability_check = availability_check - self - end - - def with_manage_ability_check(*any_of_resources) - @availability_check = ->(ability, _store) { any_of_resources.any? { |r| ability.can?(:manage, r) } } - self - end - - def with_admin_ability_check(*any_of_resources) - @availability_check = ->(ability, _store) { any_of_resources.any? { |r| ability.can?(:admin, r) } } - self - end - end - end - end -end diff --git a/app/models/spree/admin/main_menu/item_builder.rb b/app/models/spree/admin/main_menu/item_builder.rb index 46aeec9c07..d3ed00c0ba 100644 --- a/app/models/spree/admin/main_menu/item_builder.rb +++ b/app/models/spree/admin/main_menu/item_builder.rb @@ -2,7 +2,7 @@ module Spree module Admin module MainMenu class ItemBuilder - include AvailabilityBuilderMethods + include ::Spree::Admin::PermissionChecks def initialize(key, url) @key = key diff --git a/app/models/spree/admin/main_menu/section_builder.rb b/app/models/spree/admin/main_menu/section_builder.rb index e147a5a544..d3864a2eb7 100644 --- a/app/models/spree/admin/main_menu/section_builder.rb +++ b/app/models/spree/admin/main_menu/section_builder.rb @@ -2,7 +2,7 @@ module Spree module Admin module MainMenu class SectionBuilder - include AvailabilityBuilderMethods + include ::Spree::Admin::PermissionChecks def initialize(key, icon_key) @key = key diff --git a/app/models/spree/admin/permission_checks.rb b/app/models/spree/admin/permission_checks.rb new file mode 100644 index 0000000000..475e27096f --- /dev/null +++ b/app/models/spree/admin/permission_checks.rb @@ -0,0 +1,30 @@ +module Spree + module Admin + module PermissionChecks + def with_availability_check(availability_check) + @availability_check = availability_check + self + end + + def with_manage_ability_check(*classes) + @availability_check = ->(ability, _current) { classes.any? { |c| ability.can?(:manage, c) } } + self + end + + def with_admin_ability_check(*classes) + @availability_check = ->(ability, _current) { classes.any? { |c| ability.can?(:admin, c) } } + self + end + + def with_index_ability_check(*classes) + @availability_check = ->(ability, _current) { classes.any? { |c| ability.can?(:index, c) } } + self + end + + def with_update_ability_check + @availability_check = ->(ability, resource) { ability.can?(:update, resource) } + self + end + end + end +end diff --git a/app/models/spree/admin/tabs/availability_builder_methods.rb b/app/models/spree/admin/tabs/availability_builder_methods.rb deleted file mode 100644 index 57dd343043..0000000000 --- a/app/models/spree/admin/tabs/availability_builder_methods.rb +++ /dev/null @@ -1,27 +0,0 @@ -module Spree - module Admin - module Tabs - module AvailabilityBuilderMethods - def with_availability_check(availability_check) - @availability_check = availability_check - self - end - - def with_update_availability_check - @availability_check = ->(ability, resource) { ability.can?(:update, resource) } - self - end - - def with_index_availability_check(klass) - @availability_check = ->(ability, _resource) { ability.can?(:index, klass) } - self - end - - def with_admin_availability_check(klass) - @availability_check = ->(ability, _resource) { ability.can?(:admin, klass) } - self - end - end - end - end -end diff --git a/app/models/spree/admin/tabs/order_default_tabs_builder.rb b/app/models/spree/admin/tabs/order_default_tabs_builder.rb index c3748f9138..d4bf5ad4c7 100644 --- a/app/models/spree/admin/tabs/order_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/order_default_tabs_builder.rb @@ -52,7 +52,7 @@ def add_channel_tab(root) tab = TabBuilder.new(channel_tab_config). with_active_check. - with_update_availability_check. + with_update_ability_check. with_data_hook('admin_order_tabs_channel_details'). build @@ -98,7 +98,7 @@ def add_shipments_tab(root) tab = TabBuilder.new(shipments_tab_config). with_active_check. - with_update_availability_check. + with_update_ability_check. with_data_hook('admin_order_tabs_shipment_details'). build @@ -119,7 +119,7 @@ def add_adjustments_tab(root) tab = TabBuilder.new(adjustments_tab_config). with_active_check. - with_index_availability_check(::Spree::Adjustment). + with_index_ability_check(::Spree::Adjustment). with_data_hook('admin_order_tabs_adjustments'). build @@ -140,7 +140,7 @@ def add_payments_tab(root) tab = TabBuilder.new(payments_tab_config). with_active_check. - with_index_availability_check(::Spree::Payment). + with_index_ability_check(::Spree::Payment). with_data_hook('admin_order_tabs_payments'). build @@ -162,7 +162,7 @@ def add_return_authorizations_tab(root) TabBuilder.new(return_authorizations_tab_config). with_active_check. with_completed_check. - with_index_availability_check(::Spree::ReturnAuthorization). + with_index_ability_check(::Spree::ReturnAuthorization). with_data_hook('admin_order_tabs_return_authorizations'). build @@ -184,7 +184,7 @@ def add_customer_returns_tab(root) TabBuilder.new(customer_returns_tab_config). with_active_check. with_completed_check. - with_index_availability_check(::Spree::CustomerReturn). + with_index_ability_check(::Spree::CustomerReturn). build root.add(tab) @@ -204,7 +204,7 @@ def add_state_changes_tab(root) tab = TabBuilder.new(state_changes_tab_config). with_active_check. - with_update_availability_check. + with_update_ability_check. with_data_hook('admin_order_tabs_state_changes'). build diff --git a/app/models/spree/admin/tabs/product_default_tabs_builder.rb b/app/models/spree/admin/tabs/product_default_tabs_builder.rb index 506283e87b..3f487a9981 100644 --- a/app/models/spree/admin/tabs/product_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/product_default_tabs_builder.rb @@ -24,7 +24,7 @@ def add_details_tab(root) tab = TabBuilder.new(details_config). with_active_check. - with_admin_availability_check(::Spree::Product). + with_admin_ability_check(::Spree::Product). build root.add(tab) diff --git a/app/models/spree/admin/tabs/tab_builder.rb b/app/models/spree/admin/tabs/tab_builder.rb index 69d0f62ecc..06e2ad2a9b 100644 --- a/app/models/spree/admin/tabs/tab_builder.rb +++ b/app/models/spree/admin/tabs/tab_builder.rb @@ -3,7 +3,7 @@ module Admin module Tabs class TabBuilder include ConditionalChecker - include AvailabilityBuilderMethods + include ::Spree::Admin::PermissionChecks include DataHook def initialize(config) From 40a4e5ee4043cf993fe0d73133beeb6a844784c6 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 18 Oct 2023 11:37:13 +0200 Subject: [PATCH 88/98] Lint fix --- app/models/spree/admin/tabs/tab_builder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/spree/admin/tabs/tab_builder.rb b/app/models/spree/admin/tabs/tab_builder.rb index 06e2ad2a9b..8dafdf43f9 100644 --- a/app/models/spree/admin/tabs/tab_builder.rb +++ b/app/models/spree/admin/tabs/tab_builder.rb @@ -8,7 +8,7 @@ class TabBuilder def initialize(config) @icon_name = config[:icon_name] - @key = config[:key] + @key = config[:key] @url = config[:url] @classes = config[:classes] @partial_name = config[:partial_name] From e198cb67ee12024c480388016a7e3393e2bb0f0c Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 18 Oct 2023 13:06:23 +0200 Subject: [PATCH 89/98] Abstract away common tests to shared examples --- .../models/spree/admin/main_menu/root_spec.rb | 152 +---------------- spec/models/spree/admin/tabs/root_spec.rb | 152 +---------------- .../admin/manipulation_and_query_methods.rb | 153 ++++++++++++++++++ 3 files changed, 155 insertions(+), 302 deletions(-) create mode 100644 spec/support/admin/manipulation_and_query_methods.rb diff --git a/spec/models/spree/admin/main_menu/root_spec.rb b/spec/models/spree/admin/main_menu/root_spec.rb index 3885ed20a8..5a5d8f4815 100644 --- a/spec/models/spree/admin/main_menu/root_spec.rb +++ b/spec/models/spree/admin/main_menu/root_spec.rb @@ -10,157 +10,7 @@ module Admin items.each { |i| root.add(i) } end - describe '#add' do - subject { root.add(item) } - let(:item) { double(key: 'test') } - - context "when there's no item with a particular key" do - - it 'appends an item' do - subject - expect(root.items).to include(item) - end - end - - context 'when there is an item with a particular key' do - let(:items) { [item] } - - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - end - - describe '#child_with_key?' do - subject { root.child_with_key?(key) } - let(:key) { 'key' } - - context 'when an item with given key exists' do - let(:items) { [double(key: key), double(key: 'other-key')] } - - it 'returns true' do - expect(subject).to be(true) - end - end - - context 'when an item with given key does not exist' do - let(:items) { [double(key: 'other-key')] } - - it 'returns false' do - expect(subject).to be(false) - end - end - end - - describe '#remove' do - subject { root.remove(key) } - let(:key) { 'key' } - let(:other_key) { 'other-key' } - - context 'when an item with given key exists' do - let(:items) { [double(key: key), double(key: other_key)] } - - it 'removes the item' do - subject - expect(root.items.count).to eq(1) - expect(root.items.first.key).to eq(other_key) - end - end - - context 'when an item with given key does not exist' do - let(:items) { [double(key: 'other-key')] } - - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - end - - describe '#item_for_key' do - subject { root.item_for_key(key) } - let(:key) { 'key' } - - context 'when an item with given key exists' do - let(:items) { [double(key: 'other-key'), item] } - let(:item) { double(key: key) } - - it 'returns the item' do - expect(subject).to be(item) - end - end - - context 'when an item with given key does not exist' do - let(:items) { [double(key: 'other-key')] } - - it 'returns nil' do - expect(subject).to be(nil) - end - end - end - - describe '#insert_before' do - subject { root.insert_before(existing_key, item) } - - let(:item) { double(key: inserted_key) } - let(:inserted_key) { 'test-item' } - let(:existing_key) { 'test-old' } - - context 'when the list is empty' do - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - - context 'when an item with specified key does not exist' do - let(:items) { [double(key: 'other-key'), double(key: 'other-key2')] } - - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - - context 'when an item with specified key exists' do - let(:items) { [double(key: 'other-key'), double(key: existing_key)] } - - it 'inserts the item before the other item' do - subject - expect(root.items.count).to eq(3) - expect(root.items[1].key).to eq(inserted_key) - end - end - end - - describe '#insert_after' do - subject { root.insert_after(existing_key, item) } - - let(:item) { double(key: inserted_key) } - let(:inserted_key) { 'test-item' } - let(:existing_key) { 'test-old' } - - context 'when the list is empty' do - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - - context 'when an item with specified key does not exist' do - let(:items) { [double(key: 'other-key'), double(key: 'other-key2')] } - - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - - context 'when an item with specified key exists' do - let(:items) { [double(key: existing_key), double(key: 'other-key')] } - - it 'inserts the item after the other item' do - subject - expect(root.items.count).to eq(3) - expect(root.items[1].key).to eq(inserted_key) - end - end - end + it_behaves_like "implements item manipulation and query methods" describe '#key' do subject { root.key } diff --git a/spec/models/spree/admin/tabs/root_spec.rb b/spec/models/spree/admin/tabs/root_spec.rb index dd49ce4f67..b8e5301e62 100644 --- a/spec/models/spree/admin/tabs/root_spec.rb +++ b/spec/models/spree/admin/tabs/root_spec.rb @@ -10,157 +10,7 @@ module Admin items.each { |i| root.add(i) } end - describe '#add' do - subject { root.add(item) } - let(:item) { double(key: 'test') } - - context "when there's no item with a particular key" do - - it 'appends an item' do - subject - expect(root.items).to include(item) - end - end - - context 'when there is an item with a particular key' do - let(:items) { [item] } - - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - end - - describe '#child_with_key?' do - subject { root.child_with_key?(key) } - let(:key) { 'key' } - - context 'when an item with given key exists' do - let(:items) { [double(key: key), double(key: 'other-key')] } - - it 'returns true' do - expect(subject).to be(true) - end - end - - context 'when an item with given key does not exist' do - let(:items) { [double(key: 'other-key')] } - - it 'returns false' do - expect(subject).to be(false) - end - end - end - - describe '#remove' do - subject { root.remove(key) } - let(:key) { 'key' } - let(:other_key) { 'other-key' } - - context 'when an item with given key exists' do - let(:items) { [double(key: key), double(key: other_key)] } - - it 'removes the item' do - subject - expect(root.items.count).to eq(1) - expect(root.items.first.key).to eq(other_key) - end - end - - context 'when an item with given key does not exist' do - let(:items) { [double(key: 'other-key')] } - - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - end - - describe '#item_for_key' do - subject { root.item_for_key(key) } - let(:key) { 'key' } - - context 'when an item with given key exists' do - let(:items) { [double(key: 'other-key'), item] } - let(:item) { double(key: key) } - - it 'returns the item' do - expect(subject).to be(item) - end - end - - context 'when an item with given key does not exist' do - let(:items) { [double(key: 'other-key')] } - - it 'returns nil' do - expect(subject).to be(nil) - end - end - end - - describe '#insert_before' do - subject { root.insert_before(existing_key, item) } - - let(:item) { double(key: inserted_key) } - let(:inserted_key) { 'test-item' } - let(:existing_key) { 'test-old' } - - context 'when the list is empty' do - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - - context 'when an item with specified key does not exist' do - let(:items) { [double(key: 'other-key'), double(key: 'other-key2')] } - - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - - context 'when an item with specified key exists' do - let(:items) { [double(key: 'other-key'), double(key: existing_key)] } - - it 'inserts the item before the other item' do - subject - expect(root.items.count).to eq(3) - expect(root.items[1].key).to eq(inserted_key) - end - end - end - - describe '#insert_after' do - subject { root.insert_after(existing_key, item) } - - let(:item) { double(key: inserted_key) } - let(:inserted_key) { 'test-item' } - let(:existing_key) { 'test-old' } - - context 'when the list is empty' do - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - - context 'when an item with specified key does not exist' do - let(:items) { [double(key: 'other-key'), double(key: 'other-key2')] } - - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - - context 'when an item with specified key exists' do - let(:items) { [double(key: existing_key), double(key: 'other-key')] } - - it 'inserts the item after the other item' do - subject - expect(root.items.count).to eq(3) - expect(root.items[1].key).to eq(inserted_key) - end - end - end + it_behaves_like "implements item manipulation and query methods" end end end diff --git a/spec/support/admin/manipulation_and_query_methods.rb b/spec/support/admin/manipulation_and_query_methods.rb new file mode 100644 index 0000000000..72479794d3 --- /dev/null +++ b/spec/support/admin/manipulation_and_query_methods.rb @@ -0,0 +1,153 @@ +RSpec.shared_examples "implements item manipulation and query methods" do + describe '#add' do + subject { root.add(item) } + let(:item) { double(key: 'test') } + + context "when there's no item with a particular key" do + + it 'appends an item' do + subject + expect(root.items).to include(item) + end + end + + context 'when there is an item with a particular key' do + let(:items) { [item] } + + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + end + + describe '#child_with_key?' do + subject { root.child_with_key?(key) } + let(:key) { 'key' } + + context 'when an item with given key exists' do + let(:items) { [double(key: key), double(key: 'other-key')] } + + it 'returns true' do + expect(subject).to be(true) + end + end + + context 'when an item with given key does not exist' do + let(:items) { [double(key: 'other-key')] } + + it 'returns false' do + expect(subject).to be(false) + end + end + end + + describe '#remove' do + subject { root.remove(key) } + let(:key) { 'key' } + let(:other_key) { 'other-key' } + + context 'when an item with given key exists' do + let(:items) { [double(key: key), double(key: other_key)] } + + it 'removes the item' do + subject + expect(root.items.count).to eq(1) + expect(root.items.first.key).to eq(other_key) + end + end + + context 'when an item with given key does not exist' do + let(:items) { [double(key: 'other-key')] } + + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + end + + describe '#item_for_key' do + subject { root.item_for_key(key) } + let(:key) { 'key' } + + context 'when an item with given key exists' do + let(:items) { [double(key: 'other-key'), item] } + let(:item) { double(key: key) } + + it 'returns the item' do + expect(subject).to be(item) + end + end + + context 'when an item with given key does not exist' do + let(:items) { [double(key: 'other-key')] } + + it 'returns nil' do + expect(subject).to be(nil) + end + end + end + + describe '#insert_before' do + subject { root.insert_before(existing_key, item) } + + let(:item) { double(key: inserted_key) } + let(:inserted_key) { 'test-item' } + let(:existing_key) { 'test-old' } + + context 'when the list is empty' do + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + + context 'when an item with specified key does not exist' do + let(:items) { [double(key: 'other-key'), double(key: 'other-key2')] } + + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + + context 'when an item with specified key exists' do + let(:items) { [double(key: 'other-key'), double(key: existing_key)] } + + it 'inserts the item before the other item' do + subject + expect(root.items.count).to eq(3) + expect(root.items[1].key).to eq(inserted_key) + end + end + end + + describe '#insert_after' do + subject { root.insert_after(existing_key, item) } + + let(:item) { double(key: inserted_key) } + let(:inserted_key) { 'test-item' } + let(:existing_key) { 'test-old' } + + context 'when the list is empty' do + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + + context 'when an item with specified key does not exist' do + let(:items) { [double(key: 'other-key'), double(key: 'other-key2')] } + + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + + context 'when an item with specified key exists' do + let(:items) { [double(key: existing_key), double(key: 'other-key')] } + + it 'inserts the item after the other item' do + subject + expect(root.items.count).to eq(3) + expect(root.items[1].key).to eq(inserted_key) + end + end + end +end \ No newline at end of file From 3af140a9d5aacdb96ef0de4e0a7bb7d77aa743d2 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 18 Oct 2023 13:58:39 +0200 Subject: [PATCH 90/98] Remove tab css data from configuration, and embed it into a tab object. --- .../spree/admin/tabs/order_default_tabs_builder.rb | 9 --------- .../spree/admin/tabs/product_default_tabs_builder.rb | 8 -------- app/models/spree/admin/tabs/tab.rb | 8 +++++++- app/models/spree/admin/tabs/tab_builder.rb | 2 -- app/models/spree/admin/tabs/user_default_tabs_builder.rb | 5 ----- 5 files changed, 7 insertions(+), 25 deletions(-) diff --git a/app/models/spree/admin/tabs/order_default_tabs_builder.rb b/app/models/spree/admin/tabs/order_default_tabs_builder.rb index d4bf5ad4c7..35ba049275 100644 --- a/app/models/spree/admin/tabs/order_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/order_default_tabs_builder.rb @@ -43,7 +43,6 @@ def cart_tab_config icon_name: 'cart-check.svg', key: :cart, url: ->(resource) { cart_admin_order_path(resource) }, - classes: 'nav-link', partial_name: :cart } end @@ -64,7 +63,6 @@ def channel_tab_config icon_name: 'funnel.svg', key: :channel, url: ->(resource) { channel_admin_order_path(resource) }, - classes: 'nav-link', partial_name: :channel } end @@ -89,7 +87,6 @@ def customer_tab_config icon_name: 'person-lines-fill.svg', key: :customer, url: ->(resource) { admin_order_customer_path(resource) }, - classes: 'nav-link', partial_name: :customer_details } end @@ -110,7 +107,6 @@ def shipments_tab_config icon_name: 'truck.svg', key: :shipments, url: ->(resource) { edit_admin_order_path(resource) }, - classes: 'nav-link', partial_name: :shipments } end @@ -131,7 +127,6 @@ def adjustments_tab_config icon_name: 'adjust.svg', key: :adjustments, url: ->(resource) { admin_order_adjustments_path(resource) }, - classes: 'nav-link', partial_name: :adjustments } end @@ -152,7 +147,6 @@ def payments_tab_config icon_name: 'credit-card.svg', key: :payments, url: ->(resource) { admin_order_payments_path(resource) }, - classes: 'nav-link', partial_name: :payments } end @@ -174,7 +168,6 @@ def return_authorizations_tab_config icon_name: 'enter.svg', key: :return_authorizations, url: ->(resource) { admin_order_return_authorizations_path(resource) }, - classes: 'nav-link', partial_name: :return_authorizations } end @@ -195,7 +188,6 @@ def customer_returns_tab_config icon_name: 'returns.svg', key: :customer_returns, url: ->(resource) { admin_order_customer_returns_path(resource) }, - classes: 'nav-link', partial_name: :customer_returns } end @@ -216,7 +208,6 @@ def state_changes_tab_config icon_name: 'calendar3.svg', key: :state_changes, url: ->(resource) { admin_order_state_changes_path(resource) }, - classes: 'nav-link', partial_name: :state_changes } end diff --git a/app/models/spree/admin/tabs/product_default_tabs_builder.rb b/app/models/spree/admin/tabs/product_default_tabs_builder.rb index 3f487a9981..e6c2380874 100644 --- a/app/models/spree/admin/tabs/product_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/product_default_tabs_builder.rb @@ -35,7 +35,6 @@ def details_config icon_name: 'edit.svg', key: :details, url: ->(resource) { edit_admin_product_path(resource) }, - classes: 'nav-link', partial_name: :details } end @@ -59,7 +58,6 @@ def images_config icon_name: 'images.svg', key: :images, url: ->(resource) { admin_product_images_path(resource) }, - classes: 'nav-link', partial_name: :images } end @@ -83,7 +81,6 @@ def variants_config icon_name: 'adjust.svg', key: :variants, url: ->(resource) { admin_product_variants_path(resource) }, - classes: 'nav-link', partial_name: :variants } end @@ -107,7 +104,6 @@ def properties_config icon_name: 'list.svg', key: :properties, url: ->(resource) { admin_product_product_properties_path(resource) }, - classes: 'nav-link', partial_name: :properties } end @@ -131,7 +127,6 @@ def stock_config icon_name: 'box-seam.svg', key: :stock, url: ->(resource) { stock_admin_product_path(resource) }, - classes: 'nav-link', partial_name: :stock } end @@ -155,7 +150,6 @@ def prices_config icon_name: 'currency-exchange.svg', key: :prices, url: ->(resource) { admin_product_prices_path(resource) }, - classes: 'nav-link', partial_name: :prices } end @@ -179,7 +173,6 @@ def digitals_config icon_name: 'download.svg', key: 'admin.digitals.digital_assets', url: ->(resource) { admin_product_digitals_path(resource) }, - classes: 'nav-link', partial_name: :digitals } end @@ -203,7 +196,6 @@ def translations_config icon_name: 'translate.svg', key: :translations, url: ->(resource) { translations_admin_product_path(resource) }, - classes: 'nav-link', partial_name: :translations } end diff --git a/app/models/spree/admin/tabs/tab.rb b/app/models/spree/admin/tabs/tab.rb index 4abf220670..b44ee61c31 100644 --- a/app/models/spree/admin/tabs/tab.rb +++ b/app/models/spree/admin/tabs/tab.rb @@ -8,13 +8,13 @@ def initialize(config) @icon_name = config[:icon_name] @key = config[:key] @url = config[:url] - @classes = config[:classes] @partial_name = config[:partial_name] @availability_check = config[:availability_check] @active_check = config[:active_check] @completed_check = config[:completed_check] @text = config[:text] @data_hook = config[:data_hook] + @classes = css_classes end def available?(current_ability, resource) @@ -36,6 +36,12 @@ def complete?(resource) @completed_check.call(resource) end + + private + + def css_classes + 'nav-link' + end end end end diff --git a/app/models/spree/admin/tabs/tab_builder.rb b/app/models/spree/admin/tabs/tab_builder.rb index 8dafdf43f9..86d8c99b19 100644 --- a/app/models/spree/admin/tabs/tab_builder.rb +++ b/app/models/spree/admin/tabs/tab_builder.rb @@ -10,7 +10,6 @@ def initialize(config) @icon_name = config[:icon_name] @key = config[:key] @url = config[:url] - @classes = config[:classes] @partial_name = config[:partial_name] @availability_check = nil @active_check = nil @@ -28,7 +27,6 @@ def build_config icon_name: @icon_name, key: @key, url: @url, - classes: @classes, partial_name: @partial_name, availability_check: @availability_check, active_check: @active_check, diff --git a/app/models/spree/admin/tabs/user_default_tabs_builder.rb b/app/models/spree/admin/tabs/user_default_tabs_builder.rb index b7ba74f225..ea7f0faefe 100644 --- a/app/models/spree/admin/tabs/user_default_tabs_builder.rb +++ b/app/models/spree/admin/tabs/user_default_tabs_builder.rb @@ -30,7 +30,6 @@ def account_config icon_name: 'person.svg', key: 'admin.user.account', url: ->(resource) { edit_admin_user_path(resource) }, - classes: 'nav-link', partial_name: :account } end @@ -49,7 +48,6 @@ def addresses_config icon_name: 'pin-map.svg', key: 'admin.user.addresses', url: ->(resource) { addresses_admin_user_path(resource) }, - classes: 'nav-link', partial_name: :address } end @@ -68,7 +66,6 @@ def orders_config icon_name: 'inbox.svg', key: 'admin.user.orders', url: ->(resource) { orders_admin_user_path(resource) }, - classes: 'nav-link', partial_name: :orders } end @@ -87,7 +84,6 @@ def items_config icon_name: 'tag.svg', key: 'admin.user.items', url: ->(resource) { items_admin_user_path(resource) }, - classes: 'nav-link', partial_name: :items } end @@ -106,7 +102,6 @@ def store_credits_config icon_name: 'gift.svg', key: 'admin.user.store_credits', url: ->(resource) { admin_user_store_credits_path(resource) }, - classes: 'nav-link', partial_name: :store_credits } end From 35a3b965dc9ea420990dd2858bf7787e7c5387c8 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Wed, 18 Oct 2023 17:09:58 +0200 Subject: [PATCH 91/98] Fix spec. Remove comments. --- .../spree/admin/tabs/order_default_tabs_builder_spec.rb | 4 +--- .../spree/admin/tabs/product_default_tabs_builder_spec.rb | 4 +--- .../models/spree/admin/tabs/user_default_tabs_builder_spec.rb | 4 +--- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/spec/models/spree/admin/tabs/order_default_tabs_builder_spec.rb b/spec/models/spree/admin/tabs/order_default_tabs_builder_spec.rb index 296497ad42..d500d93420 100644 --- a/spec/models/spree/admin/tabs/order_default_tabs_builder_spec.rb +++ b/spec/models/spree/admin/tabs/order_default_tabs_builder_spec.rb @@ -20,9 +20,7 @@ module Admin subject { builder.build } it 'builds default tabs' do - # this means that tab items will need to respond to 'text' message, - # just as section items respond to 'key' message - expect(subject.items.map(&:name)).to match(default_tabs) + expect(subject.items.map(&:key)).to match(default_tabs) end end end diff --git a/spec/models/spree/admin/tabs/product_default_tabs_builder_spec.rb b/spec/models/spree/admin/tabs/product_default_tabs_builder_spec.rb index 15515371c7..12bfd23d60 100644 --- a/spec/models/spree/admin/tabs/product_default_tabs_builder_spec.rb +++ b/spec/models/spree/admin/tabs/product_default_tabs_builder_spec.rb @@ -19,9 +19,7 @@ module Admin subject { builder.build } it 'builds default tabs' do - # this means that tab items will need to respond to 'text' message, - # just as section items respond to 'key' message - expect(subject.items.map(&:name)).to match(default_tabs) + expect(subject.items.map(&:key)).to match(default_tabs) end end end diff --git a/spec/models/spree/admin/tabs/user_default_tabs_builder_spec.rb b/spec/models/spree/admin/tabs/user_default_tabs_builder_spec.rb index 2d26376c45..52ab2a47cf 100644 --- a/spec/models/spree/admin/tabs/user_default_tabs_builder_spec.rb +++ b/spec/models/spree/admin/tabs/user_default_tabs_builder_spec.rb @@ -16,9 +16,7 @@ module Admin subject { builder.build } it 'builds default tabs' do - # this means that tab items will need to respond to 'text' message, - # just as section items respond to 'key' message - expect(subject.items.map(&:name)).to match(default_tabs) + expect(subject.items.map(&:key)).to match(default_tabs) end end end From 0c6636398bfd58d00ce815f42c08c9817f1e9275 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Mon, 23 Oct 2023 12:08:31 +0200 Subject: [PATCH 92/98] Move 'add' method to a separate module, so that Section class can include this only. --- app/models/spree/admin/item_appender.rb | 11 +++++++++++ app/models/spree/admin/item_manager.rb | 6 ------ 2 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 app/models/spree/admin/item_appender.rb diff --git a/app/models/spree/admin/item_appender.rb b/app/models/spree/admin/item_appender.rb new file mode 100644 index 0000000000..65bfeaeb72 --- /dev/null +++ b/app/models/spree/admin/item_appender.rb @@ -0,0 +1,11 @@ +module Spree + module Admin + module ItemAppender + def add(item) + raise KeyError, "Item with key #{item.key} already exists" if index_for_key(item.key) + + @items << item + end + end + end +end diff --git a/app/models/spree/admin/item_manager.rb b/app/models/spree/admin/item_manager.rb index 49d7c3f147..5e9b6fc0ee 100644 --- a/app/models/spree/admin/item_manager.rb +++ b/app/models/spree/admin/item_manager.rb @@ -1,12 +1,6 @@ module Spree module Admin module ItemManager - def add(item) - raise KeyError, "Item with key #{item.key} already exists" if index_for_key(item.key) - - @items << item - end - def child_with_key?(key) index_for_key(key).present? end From 925e58d8449a4354160cd202a31395fa6b98b796 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Mon, 23 Oct 2023 12:17:43 +0200 Subject: [PATCH 93/98] Split shared examples --- spec/support/admin/item_appendage.rb | 22 +++++++++++++++++++ .../admin/manipulation_and_query_methods.rb | 21 ------------------ 2 files changed, 22 insertions(+), 21 deletions(-) create mode 100644 spec/support/admin/item_appendage.rb diff --git a/spec/support/admin/item_appendage.rb b/spec/support/admin/item_appendage.rb new file mode 100644 index 0000000000..724908f9de --- /dev/null +++ b/spec/support/admin/item_appendage.rb @@ -0,0 +1,22 @@ +RSpec.shared_examples "implements item appendage" do + describe '#add' do + subject { root.add(item) } + let(:item) { double(key: 'test') } + + context "when there's no item with a particular key" do + + it 'appends an item' do + subject + expect(root.items).to include(item) + end + end + + context 'when there is an item with a particular key' do + let(:items) { [item] } + + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + end +end \ No newline at end of file diff --git a/spec/support/admin/manipulation_and_query_methods.rb b/spec/support/admin/manipulation_and_query_methods.rb index 72479794d3..517a5e2e4a 100644 --- a/spec/support/admin/manipulation_and_query_methods.rb +++ b/spec/support/admin/manipulation_and_query_methods.rb @@ -1,25 +1,4 @@ RSpec.shared_examples "implements item manipulation and query methods" do - describe '#add' do - subject { root.add(item) } - let(:item) { double(key: 'test') } - - context "when there's no item with a particular key" do - - it 'appends an item' do - subject - expect(root.items).to include(item) - end - end - - context 'when there is an item with a particular key' do - let(:items) { [item] } - - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - end - describe '#child_with_key?' do subject { root.child_with_key?(key) } let(:key) { 'key' } From a7fdd6542c6150fe5392d3bfe64ef3bc9ffa4bf3 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Mon, 23 Oct 2023 12:23:41 +0200 Subject: [PATCH 94/98] Include item appendage module in Root and Section --- app/models/spree/admin/main_menu/section.rb | 2 ++ app/models/spree/admin/tabs/root.rb | 1 + spec/models/spree/admin/main_menu/root_spec.rb | 1 + spec/models/spree/admin/main_menu/section_spec.rb | 2 ++ spec/models/spree/admin/tabs/root_spec.rb | 1 + 5 files changed, 7 insertions(+) diff --git a/app/models/spree/admin/main_menu/section.rb b/app/models/spree/admin/main_menu/section.rb index f5541c625d..b2a566ff65 100644 --- a/app/models/spree/admin/main_menu/section.rb +++ b/app/models/spree/admin/main_menu/section.rb @@ -2,6 +2,8 @@ module Spree module Admin module MainMenu class Section + include ::Spree::Admin::ItemAppender + attr_reader :key, :label_translation_key, :icon_key, :items def initialize(key, label_translation_key, icon_key, availability_check, items) diff --git a/app/models/spree/admin/tabs/root.rb b/app/models/spree/admin/tabs/root.rb index 969ca69887..38d646158e 100644 --- a/app/models/spree/admin/tabs/root.rb +++ b/app/models/spree/admin/tabs/root.rb @@ -3,6 +3,7 @@ module Admin module Tabs class Root include ::Spree::Admin::ItemManager + include ::Spree::Admin::ItemAppender attr_reader :items diff --git a/spec/models/spree/admin/main_menu/root_spec.rb b/spec/models/spree/admin/main_menu/root_spec.rb index 5a5d8f4815..de6730b1aa 100644 --- a/spec/models/spree/admin/main_menu/root_spec.rb +++ b/spec/models/spree/admin/main_menu/root_spec.rb @@ -11,6 +11,7 @@ module Admin end it_behaves_like "implements item manipulation and query methods" + it_behaves_like "implements item appendage" describe '#key' do subject { root.key } diff --git a/spec/models/spree/admin/main_menu/section_spec.rb b/spec/models/spree/admin/main_menu/section_spec.rb index 6619efab1e..8c704018aa 100644 --- a/spec/models/spree/admin/main_menu/section_spec.rb +++ b/spec/models/spree/admin/main_menu/section_spec.rb @@ -10,6 +10,8 @@ module Admin let(:availability_check) { nil } let(:items) { [] } + it_behaves_like "implements item appendage" + describe '#key' do subject { section.key } diff --git a/spec/models/spree/admin/tabs/root_spec.rb b/spec/models/spree/admin/tabs/root_spec.rb index b8e5301e62..72ae835005 100644 --- a/spec/models/spree/admin/tabs/root_spec.rb +++ b/spec/models/spree/admin/tabs/root_spec.rb @@ -11,6 +11,7 @@ module Admin end it_behaves_like "implements item manipulation and query methods" + it_behaves_like "implements item appendage" end end end From 763aad1bf604fb790143222d3d80f317f3bd2e61 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Mon, 23 Oct 2023 13:41:41 +0200 Subject: [PATCH 95/98] Contain all item manipulation behaviour in ItemManager. Delete ItemAppender module. --- app/models/spree/admin/item_appender.rb | 11 ---------- app/models/spree/admin/item_manager.rb | 6 +++++ app/models/spree/admin/main_menu/root.rb | 2 -- app/models/spree/admin/main_menu/section.rb | 2 +- app/models/spree/admin/tabs/root.rb | 1 - .../models/spree/admin/main_menu/root_spec.rb | 1 - .../spree/admin/main_menu/section_spec.rb | 2 +- spec/models/spree/admin/tabs/root_spec.rb | 1 - spec/support/admin/item_appendage.rb | 22 ------------------- .../admin/manipulation_and_query_methods.rb | 21 ++++++++++++++++++ 10 files changed, 29 insertions(+), 40 deletions(-) delete mode 100644 app/models/spree/admin/item_appender.rb delete mode 100644 spec/support/admin/item_appendage.rb diff --git a/app/models/spree/admin/item_appender.rb b/app/models/spree/admin/item_appender.rb deleted file mode 100644 index 65bfeaeb72..0000000000 --- a/app/models/spree/admin/item_appender.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Spree - module Admin - module ItemAppender - def add(item) - raise KeyError, "Item with key #{item.key} already exists" if index_for_key(item.key) - - @items << item - end - end - end -end diff --git a/app/models/spree/admin/item_manager.rb b/app/models/spree/admin/item_manager.rb index 5e9b6fc0ee..49d7c3f147 100644 --- a/app/models/spree/admin/item_manager.rb +++ b/app/models/spree/admin/item_manager.rb @@ -1,6 +1,12 @@ module Spree module Admin module ItemManager + def add(item) + raise KeyError, "Item with key #{item.key} already exists" if index_for_key(item.key) + + @items << item + end + def child_with_key?(key) index_for_key(key).present? end diff --git a/app/models/spree/admin/main_menu/root.rb b/app/models/spree/admin/main_menu/root.rb index 25b9ddb930..fd2823aa50 100644 --- a/app/models/spree/admin/main_menu/root.rb +++ b/app/models/spree/admin/main_menu/root.rb @@ -2,8 +2,6 @@ module Spree module Admin module MainMenu class Root < Section - include ::Spree::Admin::ItemManager - attr_reader :items def initialize diff --git a/app/models/spree/admin/main_menu/section.rb b/app/models/spree/admin/main_menu/section.rb index b2a566ff65..7146556fdf 100644 --- a/app/models/spree/admin/main_menu/section.rb +++ b/app/models/spree/admin/main_menu/section.rb @@ -2,7 +2,7 @@ module Spree module Admin module MainMenu class Section - include ::Spree::Admin::ItemAppender + include ::Spree::Admin::ItemManager attr_reader :key, :label_translation_key, :icon_key, :items diff --git a/app/models/spree/admin/tabs/root.rb b/app/models/spree/admin/tabs/root.rb index 38d646158e..969ca69887 100644 --- a/app/models/spree/admin/tabs/root.rb +++ b/app/models/spree/admin/tabs/root.rb @@ -3,7 +3,6 @@ module Admin module Tabs class Root include ::Spree::Admin::ItemManager - include ::Spree::Admin::ItemAppender attr_reader :items diff --git a/spec/models/spree/admin/main_menu/root_spec.rb b/spec/models/spree/admin/main_menu/root_spec.rb index de6730b1aa..5a5d8f4815 100644 --- a/spec/models/spree/admin/main_menu/root_spec.rb +++ b/spec/models/spree/admin/main_menu/root_spec.rb @@ -11,7 +11,6 @@ module Admin end it_behaves_like "implements item manipulation and query methods" - it_behaves_like "implements item appendage" describe '#key' do subject { root.key } diff --git a/spec/models/spree/admin/main_menu/section_spec.rb b/spec/models/spree/admin/main_menu/section_spec.rb index 8c704018aa..f8993133d1 100644 --- a/spec/models/spree/admin/main_menu/section_spec.rb +++ b/spec/models/spree/admin/main_menu/section_spec.rb @@ -10,7 +10,7 @@ module Admin let(:availability_check) { nil } let(:items) { [] } - it_behaves_like "implements item appendage" + it_behaves_like "implements item manipulation and query methods" describe '#key' do subject { section.key } diff --git a/spec/models/spree/admin/tabs/root_spec.rb b/spec/models/spree/admin/tabs/root_spec.rb index 72ae835005..b8e5301e62 100644 --- a/spec/models/spree/admin/tabs/root_spec.rb +++ b/spec/models/spree/admin/tabs/root_spec.rb @@ -11,7 +11,6 @@ module Admin end it_behaves_like "implements item manipulation and query methods" - it_behaves_like "implements item appendage" end end end diff --git a/spec/support/admin/item_appendage.rb b/spec/support/admin/item_appendage.rb deleted file mode 100644 index 724908f9de..0000000000 --- a/spec/support/admin/item_appendage.rb +++ /dev/null @@ -1,22 +0,0 @@ -RSpec.shared_examples "implements item appendage" do - describe '#add' do - subject { root.add(item) } - let(:item) { double(key: 'test') } - - context "when there's no item with a particular key" do - - it 'appends an item' do - subject - expect(root.items).to include(item) - end - end - - context 'when there is an item with a particular key' do - let(:items) { [item] } - - it 'raises an error' do - expect { subject }.to raise_error(KeyError) - end - end - end -end \ No newline at end of file diff --git a/spec/support/admin/manipulation_and_query_methods.rb b/spec/support/admin/manipulation_and_query_methods.rb index 517a5e2e4a..72479794d3 100644 --- a/spec/support/admin/manipulation_and_query_methods.rb +++ b/spec/support/admin/manipulation_and_query_methods.rb @@ -1,4 +1,25 @@ RSpec.shared_examples "implements item manipulation and query methods" do + describe '#add' do + subject { root.add(item) } + let(:item) { double(key: 'test') } + + context "when there's no item with a particular key" do + + it 'appends an item' do + subject + expect(root.items).to include(item) + end + end + + context 'when there is an item with a particular key' do + let(:items) { [item] } + + it 'raises an error' do + expect { subject }.to raise_error(KeyError) + end + end + end + describe '#child_with_key?' do subject { root.child_with_key?(key) } let(:key) { 'key' } From c55de0af4fb0e0b4e50c66718c873918d987c598 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Mon, 23 Oct 2023 13:49:46 +0200 Subject: [PATCH 96/98] Add back deleted method --- app/models/spree/admin/main_menu/root.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/spree/admin/main_menu/root.rb b/app/models/spree/admin/main_menu/root.rb index fd2823aa50..188521a511 100644 --- a/app/models/spree/admin/main_menu/root.rb +++ b/app/models/spree/admin/main_menu/root.rb @@ -7,6 +7,10 @@ class Root < Section def initialize super('root', 'root', nil, nil, []) end + + def add_to_section(section_key, item) + @items.find { |e| e.key == section_key }.add(item) + end end end end From b2e42dbcf6636d74c52adc8e4323efd5cbb65143 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Mon, 23 Oct 2023 15:58:50 +0200 Subject: [PATCH 97/98] Add missing spec for 'add_to_section' --- spec/models/spree/admin/main_menu/root_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spec/models/spree/admin/main_menu/root_spec.rb b/spec/models/spree/admin/main_menu/root_spec.rb index 5a5d8f4815..bc5d1c256b 100644 --- a/spec/models/spree/admin/main_menu/root_spec.rb +++ b/spec/models/spree/admin/main_menu/root_spec.rb @@ -46,6 +46,24 @@ module Admin expect(subject).to be(true) end end + + describe '#add_to_section' do + subject { root.add_to_section(section_key, item) } + + let(:items) { [section] } + let(:section) { double(key: section_key) } + let(:section_key) { 'section' } + let(:item) { double(key: 'test') } + + before do + allow(section).to receive(:add).with(item) + end + + it 'calls add on section' do + expect(section).to receive(:add).with(item) + subject + end + end end end end From 21e4db5a2e47dab3511ac291a084a892dbb677e2 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Mon, 23 Oct 2023 15:59:31 +0200 Subject: [PATCH 98/98] Lint fix --- spec/models/spree/admin/main_menu/root_spec.rb | 2 +- spec/models/spree/admin/main_menu/section_spec.rb | 2 +- spec/models/spree/admin/tabs/root_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/models/spree/admin/main_menu/root_spec.rb b/spec/models/spree/admin/main_menu/root_spec.rb index bc5d1c256b..9790f11a7e 100644 --- a/spec/models/spree/admin/main_menu/root_spec.rb +++ b/spec/models/spree/admin/main_menu/root_spec.rb @@ -10,7 +10,7 @@ module Admin items.each { |i| root.add(i) } end - it_behaves_like "implements item manipulation and query methods" + it_behaves_like 'implements item manipulation and query methods' describe '#key' do subject { root.key } diff --git a/spec/models/spree/admin/main_menu/section_spec.rb b/spec/models/spree/admin/main_menu/section_spec.rb index f8993133d1..a62b00c0c0 100644 --- a/spec/models/spree/admin/main_menu/section_spec.rb +++ b/spec/models/spree/admin/main_menu/section_spec.rb @@ -10,7 +10,7 @@ module Admin let(:availability_check) { nil } let(:items) { [] } - it_behaves_like "implements item manipulation and query methods" + it_behaves_like 'implements item manipulation and query methods' describe '#key' do subject { section.key } diff --git a/spec/models/spree/admin/tabs/root_spec.rb b/spec/models/spree/admin/tabs/root_spec.rb index b8e5301e62..9057af6443 100644 --- a/spec/models/spree/admin/tabs/root_spec.rb +++ b/spec/models/spree/admin/tabs/root_spec.rb @@ -10,7 +10,7 @@ module Admin items.each { |i| root.add(i) } end - it_behaves_like "implements item manipulation and query methods" + it_behaves_like 'implements item manipulation and query methods' end end end