Class: Reaction::Action
- Inherits:
-
Object
- Object
- Reaction::Action
- Includes:
- HasAttributes, HasErrors, HasParams
- Defined in:
- lib/reaction/action.rb
Instance Method Summary collapse
- #cleanup ⇒ Object
-
#initialize(params = {}, meta = {}) ⇒ Action
constructor
meta isn’t used by default, but it is supported for use cases where you need to attach metadata to parameters, such as who or what set the attribute.
- #invoke ⇒ Object
- #perform ⇒ Object
- #validate! ⇒ Object
Methods included from HasParams
included, #param, #params, #raw_param, #raw_params, #set_param, #set_params, #typed_params, #validate_params
Methods included from HasErrors
Methods included from HasAttributes
included, #set_attributes, #validate_attributes
Constructor Details
#initialize(params = {}, meta = {}) ⇒ Action
meta isn’t used by default, but it is supported for use cases where you need to attach metadata to parameters, such as who or what set the attribute. For example, Paid currently has a flow like:
action = Action.new(request.params, via: :request)
action.set_params(account: api_key.account, via: :api_key)
Which allows us to use a validator to define who can set various attributes. Specifically, we limit certain attributes from being set by the request so end users can’t set params that we don’t expect them to set.
20 21 22 |
# File 'lib/reaction/action.rb', line 20 def initialize(params = {}, = {}) set_params(params, ) end |
Instance Method Details
#cleanup ⇒ Object
44 45 46 47 |
# File 'lib/reaction/action.rb', line 44 def cleanup cleanup_types cleanup_validators end |
#invoke ⇒ Object
24 25 26 27 28 29 |
# File 'lib/reaction/action.rb', line 24 def invoke validate! ret = perform ensure cleanup end |
#perform ⇒ Object
31 32 33 |
# File 'lib/reaction/action.rb', line 31 def perform # Implement this for your action end |
#validate! ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/reaction/action.rb', line 35 def validate! errors.clear validate_attributes validate_params if errors.any? raise ArgumentError.new("Validations failed: #{errors.full_messages.join(',')}") end end |