Module: Service::Base
- Extended by:
- ActiveSupport::Concern
- Included in:
- AdminNotices::Dismiss, Experiments::Toggle, Flags::CreateFlag, Flags::DestroyFlag, Flags::ReorderFlag, Flags::ToggleFlag, Flags::UpdateFlag, SiteSetting::Update, User::BulkDestroy, User::Silence, User::Suspend
- Defined in:
- lib/service/base.rb
Defined Under Namespace
Class Method Summary collapse
-
.model(name = :model, step_name = :"fetch_#{name}", optional: false) ⇒ Object
Evaluates arbitrary code to build or fetch a model (typically from the DB).
-
.options(&block) ⇒ Object
This is used to define options allowing to parameterize the service behavior.
-
.params(name = :default, default_values_from: nil, &block) ⇒ Object
Checks the validity of the input parameters.
-
.policy(name = :default, class_name: nil) ⇒ Object
Performs checks related to the state of the system.
-
.step(name) ⇒ Object
Runs arbitrary code.
-
.transaction(&block) ⇒ Object
Runs steps inside a DB transaction.
Class Method Details
.model(name = :model, step_name = :"fetch_#{name}", optional: false) ⇒ Object
Evaluates arbitrary code to build or fetch a model (typically from the DB). If the step returns a falsy value, then the step will fail.
It stores the resulting model in context[:model]
by default (can be customized by providing the name
argument).
|
# File 'lib/service/base.rb', line 315
|
.options(&block) ⇒ Object
This is used to define options allowing to parameterize the service behavior. The resulting options are available in ‘context`.
|
# File 'lib/service/base.rb', line 425
|
.params(name = :default, default_values_from: nil, &block) ⇒ Object
Checks the validity of the input parameters. Implements ActiveModel::Validations and ActiveModel::Attributes.
It stores the resulting contract in context[:params]
by default (can be customized by providing the name
argument).
|
# File 'lib/service/base.rb', line 373
|
.policy(name = :default, class_name: nil) ⇒ Object
Performs checks related to the state of the system. If the step doesn’t return a truthy value, then the policy will fail.
When using a policy object, there is no need to define a method on the service for the policy step. The policy object ‘#call` method will be called and if the result isn’t truthy, a `#reason` method is expected to be implemented to explain the failure.
Policy objects are usually useful for more complex logic.
|
# File 'lib/service/base.rb', line 335
|
.step(name) ⇒ Object
Runs arbitrary code. To mark a step as failed, a call to #fail! needs to be made explicitly.
|
# File 'lib/service/base.rb', line 390
|
.transaction(&block) ⇒ Object
Runs steps inside a DB transaction.
|
# File 'lib/service/base.rb', line 413
|