mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-03 20:07:47 +08:00
The `base.rb` file (640+ lines) contained 9 step classes, the `Context` class, and the `StepsHelpers` DSL module alongside the core concern. Each is now in its own file under `lib/service/base/`, following the existing `lib/service.rb` + `lib/service/*.rb` pattern. Also: - Update stale inline documentation in `lib/service.rb` - Use `NotImplementedError` instead of generic raise in `PolicyBase` and `ActionBase` - Move YARD DSL docs to a `@!parse` block at the top of `base.rb`
35 lines
955 B
Ruby
35 lines
955 B
Ruby
# frozen_string_literal: true
|
|
|
|
module Service
|
|
module Base
|
|
# @!visibility private
|
|
class ModelStep < Step
|
|
class NotFound < StandardError
|
|
end
|
|
|
|
attr_reader :optional
|
|
|
|
def initialize(name, method_name = name, class_name: nil, optional: nil)
|
|
super(name, method_name, class_name: class_name)
|
|
@optional = optional.present?
|
|
end
|
|
|
|
def run_step
|
|
context[name] = super
|
|
raise NotFound if !optional && (!context[name] || context[name].try(:empty?))
|
|
if context[name].try(:has_changes_to_save?) && context[name].try(:invalid?)
|
|
context[result_key].fail(invalid: true)
|
|
context.fail!
|
|
end
|
|
rescue Failure, DefaultValuesNotAllowed
|
|
raise
|
|
rescue => exception
|
|
context[result_key].fail(
|
|
not_found: true,
|
|
exception: (exception unless exception.is_a?(NotFound)),
|
|
)
|
|
context.fail!
|
|
end
|
|
end
|
|
end
|
|
end
|