Skip to content

Commit

Permalink
Merge pull request #2969 from tvdeyen/fix-2950
Browse files Browse the repository at this point in the history
Fix "Stock" admin nav double highlight
  • Loading branch information
jacobherrington authored Dec 17, 2018
2 parents a8e77ac + bc5825f commit af6a140
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
9 changes: 7 additions & 2 deletions backend/app/models/spree/backend_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -39,14 +39,17 @@ 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,
condition: nil,
label: nil,
partial: nil,
url: nil,
position: nil
position: nil,
match_path: nil
)

@condition = condition || -> { true }
Expand All @@ -56,6 +59,7 @@ def initialize(
@partial = partial
@url = url
@position = position
@match_path = match_path
end
end

Expand Down Expand Up @@ -111,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(
Expand Down
3 changes: 2 additions & 1 deletion backend/app/views/spree/admin/shared/_tabs.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
Expand Down
17 changes: 17 additions & 0 deletions backend/spec/models/spree/backend_configuration/menu_item_spec.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions backend/spec/models/spree/backend_configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit af6a140

Please sign in to comment.