Class: SkinnyControllers::Operation::Base
- Inherits:
-
Object
- Object
- SkinnyControllers::Operation::Base
- Includes:
- ModelHelpers
- Defined in:
- lib/skinny_controllers/operation/base.rb
Overview
An example Operation may looy like
module EventOperations
class Read < SkinnyControllers::Policy::Base
def run
model if allowed?
end
end
end
TODO: make the above the ‘default’ and not require to be defined
Direct Known Subclasses
Instance Attribute Summary collapse
-
#authorized_via_parent ⇒ Object
Returns the value of attribute authorized_via_parent.
-
#current_user ⇒ Object
Returns the value of attribute current_user.
-
#params ⇒ Object
Returns the value of attribute params.
Class Method Summary collapse
Instance Method Summary collapse
- #allowed? ⇒ Boolean
-
#allowed_for?(object) ⇒ Boolean
checks the policy.
- #association_name_from_object ⇒ Object
- #id_from_params ⇒ Object
-
#initialize(current_user, params) ⇒ Base
constructor
A new instance of Base.
- #model_class ⇒ Object
- #model_name ⇒ Object
-
#policy_class ⇒ Object
Takes the class name of self and converts it to a Policy class name.
-
#policy_for(object) ⇒ Object
A new policy object and caches it.
-
#policy_method_name ⇒ Object
Converts the class name to the method name to call on the policy.
Methods included from ModelHelpers
#model, #model_from_id, #model_from_named_id, #model_from_params, #model_from_scope, #sanitized_params, #scoped_model
Constructor Details
#initialize(current_user, params) ⇒ Base
Returns a new instance of Base.
25 26 27 28 29 |
# File 'lib/skinny_controllers/operation/base.rb', line 25 def initialize(current_user, params) self.current_user = current_user self.params = params self. = false end |
Instance Attribute Details
#authorized_via_parent ⇒ Object
Returns the value of attribute authorized_via_parent.
18 19 20 |
# File 'lib/skinny_controllers/operation/base.rb', line 18 def end |
#current_user ⇒ Object
Returns the value of attribute current_user.
18 19 20 |
# File 'lib/skinny_controllers/operation/base.rb', line 18 def current_user @current_user end |
#params ⇒ Object
Returns the value of attribute params.
18 19 20 |
# File 'lib/skinny_controllers/operation/base.rb', line 18 def params @params end |
Class Method Details
.run(current_user, params) ⇒ Object
20 21 22 23 |
# File 'lib/skinny_controllers/operation/base.rb', line 20 def self.run(current_user, params) object = new(current_user, params) object.run end |
Instance Method Details
#allowed? ⇒ Boolean
76 77 78 |
# File 'lib/skinny_controllers/operation/base.rb', line 76 def allowed? allowed_for?(model) end |
#allowed_for?(object) ⇒ Boolean
checks the policy
81 82 83 |
# File 'lib/skinny_controllers/operation/base.rb', line 81 def allowed_for?(object) policy_for(object).send(policy_method_name) end |
#association_name_from_object ⇒ Object
50 51 52 |
# File 'lib/skinny_controllers/operation/base.rb', line 50 def association_name_from_object model_name.tableize end |
#id_from_params ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/skinny_controllers/operation/base.rb', line 31 def id_from_params unless @id_from_params @id_from_params = params[:id] if filter = params[:filter] @id_from_params = filter[:id].split(',') end end @id_from_params end |
#model_class ⇒ Object
42 43 44 |
# File 'lib/skinny_controllers/operation/base.rb', line 42 def model_class @model_class ||= Lookup::Model.class_from_operation(self.class.name) end |
#model_name ⇒ Object
46 47 48 |
# File 'lib/skinny_controllers/operation/base.rb', line 46 def model_name @object_type_name ||= Lookup::Model.name_from_operation(self.class.name) end |
#policy_class ⇒ Object
Takes the class name of self and converts it to a Policy class name
57 58 59 |
# File 'lib/skinny_controllers/operation/base.rb', line 57 def policy_class @policy_class ||= Lookup::Policy.class_from_model(model_name) end |
#policy_for(object) ⇒ Object
Returns a new policy object and caches it.
69 70 71 72 73 74 |
# File 'lib/skinny_controllers/operation/base.rb', line 69 def policy_for(object) @policy ||= policy_class.new( current_user, object, authorized_via_parent: ) end |
#policy_method_name ⇒ Object
Converts the class name to the method name to call on the policy
64 65 66 |
# File 'lib/skinny_controllers/operation/base.rb', line 64 def policy_method_name @policy_method_name ||= Lookup::Policy.method_name_for_operation(self.class.name) end |