From 04f4214bb88d4832725ad4b893274faab406e14e Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Thu, 22 Nov 2018 21:59:28 +0100 Subject: [PATCH 1/2] Be able to set and pass match_path from MenuItem Refs #2950 --- .../app/models/spree/backend_configuration.rb | 8 ++++++-- .../app/views/spree/admin/shared/_tabs.html.erb | 3 ++- .../backend_configuration/menu_item_spec.rb | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 backend/spec/models/spree/backend_configuration/menu_item_spec.rb diff --git a/backend/app/models/spree/backend_configuration.rb b/backend/app/models/spree/backend_configuration.rb index f6ba1d8d6fd..972bc196d78 100644 --- a/backend/app/models/spree/backend_configuration.rb +++ b/backend/app/models/spree/backend_configuration.rb @@ -22,7 +22,7 @@ class BackendConfiguration < Preferences::Configuration # An item which should be drawn in the admin menu class MenuItem - attr_reader :icon, :label, :partial, :condition, :sections, :url + attr_reader :icon, :label, :partial, :condition, :sections, :url, :match_path attr_accessor :position @@ -39,6 +39,8 @@ class MenuItem # @param url [String] A url where this link should send the user to # @param position [Integer] The position in which the menu item should render # nil will cause the item to render last + # @param match_path [String, Regexp] (nil) If the {url} to determine the active tab is ambigous + # you can pass a String or Regexp to identify this menu item def initialize( sections, icon, @@ -46,7 +48,8 @@ def initialize( label: nil, partial: nil, url: nil, - position: nil + position: nil, + match_path: nil ) @condition = condition || -> { true } @@ -56,6 +59,7 @@ def initialize( @partial = partial @url = url @position = position + @match_path = match_path end end diff --git a/backend/app/views/spree/admin/shared/_tabs.html.erb b/backend/app/views/spree/admin/shared/_tabs.html.erb index 7be1b6e943f..60e84498c7e 100644 --- a/backend/app/views/spree/admin/shared/_tabs.html.erb +++ b/backend/app/views/spree/admin/shared/_tabs.html.erb @@ -5,7 +5,8 @@ *menu_item.sections, icon: menu_item.icon, label: menu_item.label, - url: menu_item.url.is_a?(Symbol) ? spree.public_send(menu_item.url) : menu_item.url + url: menu_item.url.is_a?(Symbol) ? spree.public_send(menu_item.url) : menu_item.url, + match_path: menu_item.match_path, ) do %> <%- render partial: menu_item.partial if menu_item.partial %> diff --git a/backend/spec/models/spree/backend_configuration/menu_item_spec.rb b/backend/spec/models/spree/backend_configuration/menu_item_spec.rb new file mode 100644 index 00000000000..c74e7e123ae --- /dev/null +++ b/backend/spec/models/spree/backend_configuration/menu_item_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Spree::BackendConfiguration::MenuItem do + describe '#match_path' do + subject do + described_class.new([], nil, { + match_path: '/stock_items' + }).match_path + end + + it 'can be read' do + is_expected.to eq('/stock_items') + end + end +end From bc5825ffa6493770037e54f1a19a04bb4c173e80 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Thu, 22 Nov 2018 22:00:43 +0100 Subject: [PATCH 2/2] Set match_path on stock items MenuItem Without setting match_path the tab helper uses the current controller name as criteria to activate the tab in the menu. Since the stock items controller can be accessed for a single product or all products we need to make sure it only matches the /stock_items path for the Stock menu tab. Closes #2950 --- .../app/models/spree/backend_configuration.rb | 1 + .../spree/backend_configuration_spec.rb | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 backend/spec/models/spree/backend_configuration_spec.rb diff --git a/backend/app/models/spree/backend_configuration.rb b/backend/app/models/spree/backend_configuration.rb index 972bc196d78..5e02b28ef2a 100644 --- a/backend/app/models/spree/backend_configuration.rb +++ b/backend/app/models/spree/backend_configuration.rb @@ -115,6 +115,7 @@ def menu_items condition: -> { can?(:admin, Spree::StockItem) }, label: :stock, url: :admin_stock_items_path, + match_path: '/stock_items', position: 3 ), MenuItem.new( diff --git a/backend/spec/models/spree/backend_configuration_spec.rb b/backend/spec/models/spree/backend_configuration_spec.rb new file mode 100644 index 00000000000..1e9ea5d5d7d --- /dev/null +++ b/backend/spec/models/spree/backend_configuration_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Spree::BackendConfiguration do + describe '#menu_items' do + subject do + described_class.new.menu_items + end + + describe 'menu tab for stock items' do + let(:stock_menu_item) do + subject.detect { |item| item.label == :stock } + end + + # Regression for https://github.com/solidusio/solidus/issues/2950 + it 'has match_path set to /stock_items' do + expect(stock_menu_item.match_path).to eq('/stock_items') + end + end + end +end