Using actions
Actions in the service are sequential calls to methods. Service methods are called using the make method.
Examples
Minimal
In its minimal form, calling methods via make is optional. The call method can be used instead.
ruby
class PostsService::Create < ApplicationService::Base
def call
# something
end
endSeveral methods
ruby
class PostsService::Create < ApplicationService::Base
# ...
make :assign_api_model
make :perform_api_request
make :process_result
def assign_api_model
internals.api_model = APIModel.new(...)
end
def perform_api_request
internals.response = APIClient.resource.create(internals.api_model)
end
def process_result
ARModel.create!(internals.response)
end
endOptions
See the options section for details.
Group of multiple actions
See the grouping section for details.
Aliases for make
Add alternatives to the make method via action_aliases configuration.
ruby
configuration do
action_aliases %i[execute]
end
execute :something
def something
# ...
endCustomization for make
Add frequently used method name prefixes via action_shortcuts configuration. Method names stay the same length, but make lines become shorter and more readable.
Simple mode
In simple mode, values are passed as an array of symbols.
ruby
configuration do
action_shortcuts %i[assign perform]
endruby
class CMSService::API::Posts::Create < CMSService::API::Base
# ...
assign :model
perform :request
private
def assign_model
# Build model for API request
end
def perform_request
# Perform API request
end
# ...
endAdvanced mode Since 2.14.0
In advanced mode, values are passed as a hash.
ruby
configuration do
action_shortcuts(
%i[assign],
{
restrict: { # replacement for make
prefix: :create, # method name prefix
suffix: :restriction # method name suffix
}
}
)
endruby
class PaymentsService::Restrictions::Create < ApplicationService::Base
input :payment, type: Payment
# The exclamation mark will be moved to the end of the method name
restrict :payment!
private
def create_payment_restriction!
inputs.payment.restrictions.create!(
reason: "Suspicion of fraud"
)
end
end