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::Operation::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
-
#_lookup ⇒ Object
Returns the value of attribute _lookup.
-
#action ⇒ Object
Returns the value of attribute action.
-
#association_name ⇒ Object
Returns the value of attribute association_name.
-
#authorized_via_parent ⇒ Object
Returns the value of attribute authorized_via_parent.
-
#current_user ⇒ Object
Returns the value of attribute current_user.
-
#model_key ⇒ Object
Returns the value of attribute model_key.
-
#options ⇒ Object
Returns the value of attribute options.
-
#params ⇒ Object
Returns the value of attribute params.
-
#params_for_action ⇒ Object
Returns the value of attribute params_for_action.
Class Method Summary collapse
-
.call ⇒ Object
To support the shorthand ruby/block syntax e.g.: MyOperation.().
- .run(current_user, params) ⇒ Object
Instance Method Summary collapse
- #allowed? ⇒ Boolean
-
#allowed_for?(object) ⇒ Boolean
checks the policy.
-
#association_name_from_object ⇒ Object
TODO: maybe make this configurable?.
- #check_allowed!(*args) ⇒ Object
- #id_from_params ⇒ Object
-
#initialize(current_user, controller_params, params_for_action = nil, action = nil, lookup = nil, options = {}) ⇒ Base
constructor
TODO: too many optional parameters.
- #lookup ⇒ Object
-
#policy_for(object) ⇒ Object
A new policy object and caches it.
-
#run ⇒ Object
(also: #call)
To be overridden.
Methods included from ModelHelpers
#find_model, #model, #model_from_id, #model_from_named_id, #model_from_params, #model_from_parent, #model_from_scope, #model_param_name, #model_params, #sanitized_params, #scoped_model
Constructor Details
#initialize(current_user, controller_params, params_for_action = nil, action = nil, lookup = nil, options = {}) ⇒ Base
TODO: too many optional parameters. Group into options hash
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/skinny_controllers/operation/base.rb', line 48 def initialize(current_user, controller_params, params_for_action = nil, action = nil, lookup = nil, = {}) self. = false self.current_user = current_user self.action = action || controller_params[:action] self.params = controller_params self.params_for_action = params_for_action || controller_params self._lookup = lookup self. = self.model_key = [:model_params_key] self.association_name = [:association_name] end |
Instance Attribute Details
#_lookup ⇒ Object
Returns the value of attribute _lookup.
19 20 21 |
# File 'lib/skinny_controllers/operation/base.rb', line 19 def _lookup @_lookup end |
#action ⇒ Object
Returns the value of attribute action.
19 20 21 |
# File 'lib/skinny_controllers/operation/base.rb', line 19 def action @action end |
#association_name ⇒ Object
Returns the value of attribute association_name.
19 20 21 |
# File 'lib/skinny_controllers/operation/base.rb', line 19 def association_name @association_name end |
#authorized_via_parent ⇒ Object
Returns the value of attribute authorized_via_parent.
19 20 21 |
# File 'lib/skinny_controllers/operation/base.rb', line 19 def end |
#current_user ⇒ Object
Returns the value of attribute current_user.
19 20 21 |
# File 'lib/skinny_controllers/operation/base.rb', line 19 def current_user @current_user end |
#model_key ⇒ Object
Returns the value of attribute model_key.
19 20 21 |
# File 'lib/skinny_controllers/operation/base.rb', line 19 def model_key @model_key end |
#options ⇒ Object
Returns the value of attribute options.
19 20 21 |
# File 'lib/skinny_controllers/operation/base.rb', line 19 def end |
#params ⇒ Object
Returns the value of attribute params.
19 20 21 |
# File 'lib/skinny_controllers/operation/base.rb', line 19 def params @params end |
#params_for_action ⇒ Object
Returns the value of attribute params_for_action.
19 20 21 |
# File 'lib/skinny_controllers/operation/base.rb', line 19 def params_for_action @params_for_action end |
Class Method Details
.call ⇒ Object
To support the shorthand ruby/block syntax e.g.: MyOperation.()
32 33 34 35 |
# File 'lib/skinny_controllers/operation/base.rb', line 32 def run(current_user, params) object = new(current_user, params) object.run end |
.run(current_user, params) ⇒ Object
25 26 27 28 |
# File 'lib/skinny_controllers/operation/base.rb', line 25 def run(current_user, params) object = new(current_user, params) object.run end |
Instance Method Details
#allowed? ⇒ Boolean
106 107 108 |
# File 'lib/skinny_controllers/operation/base.rb', line 106 def allowed? allowed_for?(model) end |
#allowed_for?(object) ⇒ Boolean
checks the policy
115 116 117 |
# File 'lib/skinny_controllers/operation/base.rb', line 115 def allowed_for?(object) policy_for(object).send(policy_method_name) end |
#association_name_from_object ⇒ Object
TODO: maybe make this configurable?
93 94 95 |
# File 'lib/skinny_controllers/operation/base.rb', line 93 def association_name_from_object association_name || model_name.tableize.split('/').last end |
#check_allowed!(*args) ⇒ Object
110 111 112 |
# File 'lib/skinny_controllers/operation/base.rb', line 110 def check_allowed!(*args) raise DeniedByPolicy.new(*args.presence || action) unless allowed? end |
#id_from_params ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/skinny_controllers/operation/base.rb', line 73 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 |
#lookup ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/skinny_controllers/operation/base.rb', line 64 def lookup @lookup ||= begin _lookup || Lookup.from_operation( operation_class: self.class, model_class: [:model_class] ) end end |
#policy_for(object) ⇒ Object
Returns a new policy object and caches it.
98 99 100 101 102 103 104 |
# File 'lib/skinny_controllers/operation/base.rb', line 98 def policy_for(object) @policy ||= policy_class.new( current_user, object, authorized_via_parent: ) end |
#run ⇒ Object Also known as: call
To be overridden
36 |
# File 'lib/skinny_controllers/operation/base.rb', line 36 def run; end |