Skip to content

Commit

Permalink
Add support for args in link_to_if_authorized helper
Browse files Browse the repository at this point in the history
  • Loading branch information
0x7466 committed Apr 15, 2021
1 parent 9e55341 commit 1f25aed
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/helpers/active_entry/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ def authorized_for? controller_name, action, **args
policy::Authorization.pass? action, **args
end

def link_to_if_authorized name = nil, options = nil, html_options = nil, &block
def link_to_if_authorized name = nil, options = nil, html_options = nil, **args, &block
url = url_for options
method = options&.is_a?(Hash) && options[:method] ? options[:method].to_s.upcase : "GET"

recognized_path = Rails.application.routes.recognize_path(url, method: method)

authorized = authorized_for? recognized_path[:controller], recognized_path[:action]
authorized = authorized_for? recognized_path[:controller], recognized_path[:action], **args

link_to name, options, html_options, &block if authorized
end
Expand Down
4 changes: 2 additions & 2 deletions coverage/coverage_badge_total.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions spec/dummy/app/controllers/test_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ def authenticated_authorized
pass!
head :no_content
end

def authenticated_authorized_with_arg
pass!
head :no_content
end
end
8 changes: 8 additions & 0 deletions spec/dummy/app/policies/test_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def authenticated_unauthorized?
def authenticated_authorized?
success
end

def authenticated_authorized_with_arg?
#@arg.present?
end
end

class Authorization < ActiveEntry::Base::Authorization
Expand All @@ -25,5 +29,9 @@ def authenticated_unauthorized?
def authenticated_authorized?
success
end

def authenticated_authorized_with_arg?
@arg.present?
end
end
end
1 change: 1 addition & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
get "unauthenticated" => "test#unauthenticated"
get "authenticated_unauthorized" => "test#authenticated_unauthorized"
get "authenticated_authorized" => "test#authenticated_authorized"
get "authenticated_authorized_with_arg" => "test#authenticated_authorized_with_arg"
end
6 changes: 6 additions & 0 deletions spec/helpers/active_entry/view_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ class Authorization < ActiveEntry::Base::Authorization
it "is a link" do
expect(helper.link_to_if_authorized("demo", "/authenticated_authorized")).to eq "<a href=\"/authenticated_authorized\">demo</a>"
end

context "with args" do
it "is a link" do
expect(helper.link_to_if_authorized("demo", "/authenticated_authorized_with_arg", arg: :demo)).to eq "<a href=\"/authenticated_authorized_with_arg\">demo</a>"
end
end
end

context "if not authorized" do
Expand Down

0 comments on commit 1f25aed

Please sign in to comment.