Module: ActiveEntry::ViewHelper

Defined in:
app/helpers/active_entry/view_helper.rb

Instance Method Summary collapse

Instance Method Details

#authorized_for?(controller_name, action, **args) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
# File 'app/helpers/active_entry/view_helper.rb', line 3

def authorized_for? controller_name, action, **args
  controller_name = controller_name.to_s.camelize.remove "Controller"
  policy = ActiveEntry::PolicyFinder.policy_for controller_name
  policy::Authorization.pass? action, **args
end


9
10
11
12
13
14
15
16
17
18
# File 'app/helpers/active_entry/view_helper.rb', line 9

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], **args
  
  link_to name, options, html_options, &block if authorized
end