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
- #load_resource ⇒ Object
- #resource_class ⇒ Object
- #resource_instance ⇒ Object
- #resource_name ⇒ Object
- #skip_authorization_and_scope ⇒ Object
Instance Method Details
#authorize_resource ⇒ Object
44 45 46 47 |
# File 'lib/pundit_extra/resource_autoload.rb', line 44 def resource = resource_instance || resource_class resource end |
#load_resource ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/pundit_extra/resource_autoload.rb', line 26 def load_resource scope = resource_class action = params[:action] varname = resource_name if action == 'index' varname = controller_name resource = policy_scope resource_class elsif ['new', 'create'].include? action resource = scope.new resource.attributes = send("#{resource_name}_params") if action == 'create' elsif params[:id] resource = scope.find params[:id] else resource = nil end instance_variable_set "@#{varname}", resource end |
#resource_class ⇒ Object
63 64 65 |
# File 'lib/pundit_extra/resource_autoload.rb', line 63 def resource_class resource_name.classify.constantize end |
#resource_instance ⇒ Object
67 68 69 |
# File 'lib/pundit_extra/resource_autoload.rb', line 67 def resource_instance instance_variable_get "@#{resource_name}" end |
#resource_name ⇒ Object
59 60 61 |
# File 'lib/pundit_extra/resource_autoload.rb', line 59 def resource_name controller_name.singularize end |
#skip_authorization_and_scope ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/pundit_extra/resource_autoload.rb', line 49 def action = params[:action] if action == 'index' skip_policy_scope else end end |