Skip to content

Commit

Permalink
Include MenuItem#sections while matching path
Browse files Browse the repository at this point in the history
Using sections for matching the path was originally done inside the
#tab helper, when the responsibility was moved to MenuItem this use-
case was lost. Not anymore.

(cherry picked from commit d6cb238)
  • Loading branch information
elia committed Oct 3, 2023
1 parent 44f2172 commit 0f17bdf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
37 changes: 18 additions & 19 deletions backend/lib/spree/backend_configuration/menu_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def initialize(
end

@condition = condition || -> { true }
@sections = sections
@sections = sections || []
@icon = icon
@label = label
@partial = partial
Expand All @@ -78,27 +78,26 @@ def render_partial?
end

def match_path?(request)
if match_path.is_a? Regexp
request.fullpath =~ match_path
elsif match_path.respond_to?(:call)
match_path.call(request)
elsif match_path
request.fullpath.starts_with?("#{spree.admin_path}#{match_path}")
else
request.fullpath.to_s.starts_with?(url.to_s)
end
matches =
if match_path.is_a? Regexp
request.fullpath =~ match_path
elsif match_path.respond_to?(:call)
match_path.call(request)
elsif match_path
request.fullpath.starts_with?("#{spree.admin_path}#{match_path}")
end
matches ||= request.fullpath.to_s.starts_with?(url.to_s) if url.present?
matches ||= @sections.include?(request.controller_class.controller_name.to_sym) if @sections.present?

matches
end

def url
if @url.respond_to?(:call)
@url.call
elsif @url.is_a?(Symbol)
spree.public_send(@url)
elsif @url.nil? && @label
spree.send("admin_#{@label}_path")
else
@url
end
url = @url.call if @url.respond_to?(:call)
url ||= spree.public_send(@url) if @url.is_a?(Symbol) && spree.respond_to?(@url)
url ||= spree.send("admin_#{@label}_path") if @url.nil? && @label && spree.respond_to?("admin_#{@label}_path")
url ||= @url.to_s
url
end

private
Expand Down
20 changes: 20 additions & 0 deletions backend/spec/lib/spree/backend_configuration/menu_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@

expect(subject.match_path?(request)).to be_truthy
end

it 'matches the item on the (deprecated) sections against the controller name' do
allow(Spree.deprecator).to receive(:warn).with(a_string_matching(/icon/))
allow(Spree.deprecator).to receive(:warn).with(a_string_matching(/sections/))

subject = described_class.new([:foo, :bar], :baz_icon)
matching_request = double(
ActionDispatch::Request,
controller_class: double(ActionController::Base, controller_name: 'bar'),
fullpath: '/qux',
)
other_request = double(
ActionDispatch::Request,
controller_class: double(ActionController::Base, controller_name: 'baz'),
fullpath: '/qux',
)

expect(subject.match_path?(matching_request)).to be true
expect(subject.match_path?(other_request)).to be false
end
end

describe "#url" do
Expand Down

0 comments on commit 0f17bdf

Please sign in to comment.