Fix menu indicator on bulk order page

This commit is contained in:
PHAN QUANG LAM
2021-10-26 06:41:30 +09:00
parent 6f2fe06c27
commit 77524c633d
4 changed files with 75 additions and 4 deletions

View File

@@ -9,6 +9,8 @@ module Spree
# * :route to override automatically determining the default route
# * :match_path as an alternative way to control when the tab is active,
# /products would match /admin/products, /admin/products/5/variants etc.
# * :except_paths to reject subpaths that have their own menu,
# e.g. match_path = '/admin/orders', except_paths = ['/admin/orders/bulk_management']
def tab(*args)
options = { label: args.first.to_s }
if args.last.is_a?(Hash)
@@ -36,9 +38,9 @@ module Spree
end
selected = if options[:match_path]
request.
fullpath.
starts_with?("#{main_app.root_path}admin#{options[:match_path]}")
PathChecker
.new(request.fullpath, self)
.active_path?(options[:match_path], options[:except_paths])
else
args.include?(controller.controller_name.to_sym)
end

View File

@@ -0,0 +1,21 @@
# frozen_string_literal: false
class PathChecker
def initialize(fullpath, view_context)
@fullpath = fullpath
@view_context = view_context
end
def active_path?(match_path, except_paths = nil)
root_path = @view_context.main_app.root_path
active = @fullpath.starts_with?("#{root_path}admin#{match_path}")
return false unless active
return true if except_paths.blank?
except_paths.each do |path|
return false if @fullpath.starts_with?("#{root_path}admin#{path}")
end
true
end
end

View File

@@ -1,6 +1,6 @@
- content_for :sub_menu do
%ul#sub_nav.inline-menu
= tab :orders, :match_path => '/orders'
= tab :orders, match_path: '/orders', except_paths: ['/orders/bulk_management']
= tab :bulk_order_management, :match_path => '/orders/bulk_management'
- if subscriptions_enabled?
= tab :subscriptions, :match_path => '/subscriptions', url: main_app.admin_subscriptions_path

View File

@@ -0,0 +1,48 @@
# frozen_string_literal: true
require 'spec_helper'
describe PathChecker do
describe "#active_path?" do
let(:view_context) { double("view context") }
before do
allow(view_context).to receive_message_chain("main_app.root_path") { "/" }
end
context "when fullpath starts with match_path and except_paths is blank" do
it "returns true" do
checker = described_class.new("/admin/products", view_context)
expect(checker.active_path?("/products")).to be true
expect(checker.active_path?("/products", nil)).to be true
expect(checker.active_path?("/products", [])).to be true
checker = described_class.new("/admin/products/5/variants", view_context)
expect(checker.active_path?("/products")).to be true
expect(checker.active_path?("/products", nil)).to be true
expect(checker.active_path?("/products", [])).to be true
end
end
context "when fullpath doesn't start with match_path" do
it "returns false" do
checker = described_class.new("/admin/products", view_context)
expect(checker.active_path?("/orders")).to be false
end
end
context "when fullpath starts with match_path and doesn't start with any of except_paths" do
it "returns true" do
checker = described_class.new("/admin/products", view_context)
expect(checker.active_path?("/products", ["/orders/bulk_management"])).to be true
end
end
context "when fullpath starts with match_path also with one of except_paths" do
it "returns false" do
checker = described_class.new("/admin/orders/bulk_management", view_context)
expect(checker.active_path?("/orders", ["/orders/bulk_management"])).to be false
end
end
end
end