mirror of
https://github.com/discourse/discourse.git
synced 2025-05-28 13:51:18 +08:00
DEV: Make service contracts immutable
We decided to make contracts immutable once their validations have run. Indeed, it doesn’t make a lot of sense to modify a contract value outside the contract itself. If processing is needed, then it should happen inside the contract itself.
This commit is contained in:

committed by
Loïc Guitaut

parent
6d918c6307
commit
c78211cf8d
@ -202,13 +202,18 @@ module Service
|
||||
attributes = class_name.attribute_names.map(&:to_sym)
|
||||
default_values = {}
|
||||
default_values = context[default_values_from].slice(*attributes) if default_values_from
|
||||
contract = class_name.new(default_values.merge(context[:params].slice(*attributes)))
|
||||
contract =
|
||||
class_name.new(
|
||||
**default_values.merge(context[:params].slice(*attributes)),
|
||||
options: context[:options],
|
||||
)
|
||||
context[contract_name] = contract
|
||||
context[result_key] = Context.build
|
||||
if contract.invalid?
|
||||
context[result_key].fail(errors: contract.errors, parameters: contract.raw_attributes)
|
||||
context.fail!
|
||||
end
|
||||
contract.freeze
|
||||
end
|
||||
|
||||
private
|
||||
|
Reference in New Issue
Block a user