Module: PunditExtra::ResourceAutoload
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/pundit_extra/resource_autoload.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #authorize_resource ⇒ Object
- #has_permitted_attributes?(resource, action) ⇒ Boolean
- #load_resource ⇒ Object
- #resource_attributes(resource, action) ⇒ Object
- #resource_class ⇒ Object
- #resource_instance ⇒ Object
- #resource_name ⇒ Object
- #skip_authorization_and_scope ⇒ Object
Instance Method Details
#authorize_resource ⇒ Object
58 59 60 61 |
# File 'lib/pundit_extra/resource_autoload.rb', line 58 def resource = resource_instance || resource_class resource end |
#has_permitted_attributes?(resource, action) ⇒ Boolean
94 95 96 97 98 99 |
# File 'lib/pundit_extra/resource_autoload.rb', line 94 def has_permitted_attributes?(resource, action) return true if policy(resource).respond_to? :"permitted_attributes_for_#{action}" return true if policy(resource).respond_to? :permitted_attributes false end |
#load_resource ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/pundit_extra/resource_autoload.rb', line 28 def load_resource scope = resource_class action = params[:action] varname = resource_name if action == 'index' varname = controller_name resource = policy_scope resource_class elsif action == 'new' resource = scope.new elsif action == 'create' resource = scope.new resource.attributes = resource_attributes resource, action elsif action == 'update' resource = scope.find params[:id] resource.attributes = resource_attributes resource, action elsif params[:id] resource = scope.find params[:id] else resource = nil end instance_variable_set :"@#{varname}", resource end |
#resource_attributes(resource, action) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/pundit_extra/resource_autoload.rb', line 81 def resource_attributes(resource, action) if has_permitted_attributes? resource, action return permitted_attributes resource end candidates = ["#{action}_params", "#{resource_name}_params"] candidates.each do |candidate| return send(candidate) if respond_to? candidate, true end {} end |
#resource_class ⇒ Object
73 74 75 |
# File 'lib/pundit_extra/resource_autoload.rb', line 73 def resource_class resource_name.classify.constantize end |
#resource_instance ⇒ Object
77 78 79 |
# File 'lib/pundit_extra/resource_autoload.rb', line 77 def resource_instance instance_variable_get :"@#{resource_name}" end |
#resource_name ⇒ Object
69 70 71 |
# File 'lib/pundit_extra/resource_autoload.rb', line 69 def resource_name controller_name.singularize end |
#skip_authorization_and_scope ⇒ Object
63 64 65 66 67 |
# File 'lib/pundit_extra/resource_autoload.rb', line 63 def action = params[:action] skip_policy_scope if action == 'index' end |