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
46 47 48 49 |
# File 'lib/pundit_extra/resource_autoload.rb', line 46 def resource = resource_instance || resource_class resource end |
#load_resource ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# 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 ['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
61 62 63 |
# File 'lib/pundit_extra/resource_autoload.rb', line 61 def resource_class resource_name.classify.constantize end |
#resource_instance ⇒ Object
65 66 67 |
# File 'lib/pundit_extra/resource_autoload.rb', line 65 def resource_instance instance_variable_get "@#{resource_name}" end |
#resource_name ⇒ Object
57 58 59 |
# File 'lib/pundit_extra/resource_autoload.rb', line 57 def resource_name controller_name.singularize end |
#skip_authorization_and_scope ⇒ Object
51 52 53 54 55 |
# File 'lib/pundit_extra/resource_autoload.rb', line 51 def action = params[:action] skip_policy_scope if action == 'index' end |