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.
  • Loading branch information
elia committed Oct 2, 2023
1 parent d9bc849 commit d6cb238
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 || []

Check warning on line 57 in backend/lib/spree/backend_configuration/menu_item.rb

View check run for this annotation

Codecov / codecov/patch

backend/lib/spree/backend_configuration/menu_item.rb#L57

Added line #L57 was not covered by tests
@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}")

Check warning on line 87 in backend/lib/spree/backend_configuration/menu_item.rb

View check run for this annotation

Codecov / codecov/patch

backend/lib/spree/backend_configuration/menu_item.rb#L82-L87

Added lines #L82 - L87 were not covered by tests
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?

Check warning on line 90 in backend/lib/spree/backend_configuration/menu_item.rb

View check run for this annotation

Codecov / codecov/patch

backend/lib/spree/backend_configuration/menu_item.rb#L89-L90

Added lines #L89 - L90 were not covered by tests

matches

Check warning on line 92 in backend/lib/spree/backend_configuration/menu_item.rb

View check run for this annotation

Codecov / codecov/patch

backend/lib/spree/backend_configuration/menu_item.rb#L92

Added line #L92 was not covered by tests
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

Check warning on line 100 in backend/lib/spree/backend_configuration/menu_item.rb

View check run for this annotation

Codecov / codecov/patch

backend/lib/spree/backend_configuration/menu_item.rb#L96-L100

Added lines #L96 - L100 were not covered by tests
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 d6cb238

Please sign in to comment.