Группирование действий
Группируйте несколько методов в одну группу выполнения через метод stage.
INFO
Использование опции position для make будет сортировать только внутри stage.
ruby
stage do
make :create_user!
make :create_blog_for_user!
make :create_post_for_user_blog!
endОпция only_if
Проверяет условие only_if перед вызовом методов внутри stage.
ruby
stage do
only_if ->(context:) { Settings.features.preview.enabled }
make :create_user!
make :create_blog_for_user!
make :create_post_for_user_blog!
endОпция only_unless
Противоположность опции only_if.
ruby
stage do
only_unless ->(context:) { Settings.features.preview.disabled }
make :create_user!
make :create_blog_for_user!
make :create_post_for_user_blog!
endОпция wrap_in
Оборачивайте методы в stage обёрткой. Пример: ActiveRecord::Base.transaction от Rails.
ruby
stage do
wrap_in ->(methods:, context:) { ActiveRecord::Base.transaction { methods.call } }
make :create_user!
make :create_blog_for_user!
make :create_post_for_user_blog!
endОпция rollback
Обрабатывайте исключения из методов группы или из wrap_in через метод rollback.
ruby
stage do
wrap_in ->(methods:, context:) { ActiveRecord::Base.transaction { methods.call } }
rollback :clear_data_and_fail!
make :create_user!
make :create_blog_for_user!
make :create_post_for_user_blog!
end
# ...
def clear_data_and_fail!(e)
# ...
fail!(message: "Failed to create data: #{e.message}")
end