mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Provide user input to services using params key
Currently in services, we don’t make a distinction between input
parameters, options and dependencies.
This can lead to user input modifying the service behavior, whereas it
was not the developer intention.
This patch addresses the issue by changing how data is provided to
services:
- `params` is now used to hold all data coming from outside (typically
user input from a controller) and a contract will take its values from
`params`.
- `options` is a new key to provide options to a service. This typically
allows changing a service behavior at runtime. It is, of course,
totally optional.
- `dependencies` is actually anything else provided to the service (like
`guardian`) and available directly from the context object.
The `service_params` helper in controllers has been updated to reflect
those changes, so most of the existing services didn’t need specific
changes.
The options block has the same DSL as contracts, as it’s also based on
`ActiveModel`. There aren’t any validations, though. Here’s an example:
```ruby
options do
attribute :allow_changing_hidden, :boolean, default: false
end
```
And here’s an example of how to call a service with the new keys:
```ruby
MyService.call(params: { key1: value1, … }, options: { my_option: true }, guardian:, …)
```
This commit is contained in:
committed by
Loïc Guitaut
parent
a89767913d
commit
41584ab40c
@@ -40,9 +40,9 @@ module Service
|
||||
#
|
||||
# @example An example from the {TrashChannel} service
|
||||
# class TrashChannel
|
||||
# include Base
|
||||
# include Service::Base
|
||||
#
|
||||
# model :channel, :fetch_channel
|
||||
# model :channel
|
||||
# policy :invalid_access
|
||||
# transaction do
|
||||
# step :prevents_slug_collision
|
||||
@@ -79,17 +79,15 @@ module Service
|
||||
# end
|
||||
# @example An example from the {UpdateChannelStatus} service which uses a contract
|
||||
# class UpdateChannelStatus
|
||||
# include Base
|
||||
# include Service::Base
|
||||
#
|
||||
# model :channel, :fetch_channel
|
||||
# contract
|
||||
# policy :check_channel_permission
|
||||
# step :change_status
|
||||
#
|
||||
# class Contract
|
||||
# model :channel
|
||||
# contract do
|
||||
# attribute :status
|
||||
# validates :status, inclusion: { in: Chat::Channel.editable_statuses.keys }
|
||||
# end
|
||||
# policy :check_channel_permission
|
||||
# step :change_status
|
||||
#
|
||||
# …
|
||||
# end
|
||||
|
||||
@@ -18,7 +18,7 @@ module Service
|
||||
|
||||
# Simple structure to hold the context of the service during its whole lifecycle.
|
||||
class Context
|
||||
delegate :slice, to: :store
|
||||
delegate :slice, :dig, to: :store
|
||||
|
||||
def initialize(context = {})
|
||||
@store = context.symbolize_keys
|
||||
@@ -115,6 +115,12 @@ module Service
|
||||
def transaction(&block)
|
||||
steps << TransactionStep.new(&block)
|
||||
end
|
||||
|
||||
def options(&block)
|
||||
klass = Class.new(Service::OptionsBase).tap { _1.class_eval(&block) }
|
||||
const_set("Options", klass)
|
||||
steps << OptionsStep.new(:default, class_name: klass)
|
||||
end
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
@@ -196,7 +202,7 @@ 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.slice(*attributes)))
|
||||
contract = class_name.new(default_values.merge(context[:params].slice(*attributes)))
|
||||
context[contract_name] = contract
|
||||
context[result_key] = Context.build
|
||||
if contract.invalid?
|
||||
@@ -208,9 +214,13 @@ module Service
|
||||
private
|
||||
|
||||
def contract_name
|
||||
return :contract if name.to_sym == :default
|
||||
return :contract if default?
|
||||
:"#{name}_contract"
|
||||
end
|
||||
|
||||
def default?
|
||||
name.to_sym == :default
|
||||
end
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
@@ -229,6 +239,14 @@ module Service
|
||||
end
|
||||
end
|
||||
|
||||
# @!visibility private
|
||||
class OptionsStep < Step
|
||||
def call(instance, context)
|
||||
context[result_key] = Context.build
|
||||
context[:options] = class_name.new(context[:options])
|
||||
end
|
||||
end
|
||||
|
||||
included do
|
||||
# The global context which is available from any step.
|
||||
attr_reader :context
|
||||
@@ -263,7 +281,7 @@ module Service
|
||||
# customized by providing the +name+ argument).
|
||||
#
|
||||
# @example
|
||||
# model :channel, :fetch_channel
|
||||
# model :channel
|
||||
#
|
||||
# private
|
||||
#
|
||||
@@ -361,6 +379,17 @@ module Service
|
||||
# step :log_channel_deletion
|
||||
# end
|
||||
|
||||
# @!scope class
|
||||
# @!method options(&block)
|
||||
# @param block [Proc] a block containing options definition
|
||||
# This is used to define options allowing to parameterize the service
|
||||
# behavior. The resulting options are available in `context[:options]`.
|
||||
#
|
||||
# @example
|
||||
# options do
|
||||
# attribute :my_option, :boolean, default: false
|
||||
# end
|
||||
|
||||
# @!visibility private
|
||||
def initialize(initial_context = {})
|
||||
@context = Context.build(initial_context.merge(__steps__: self.class.steps))
|
||||
|
||||
7
lib/service/options_base.rb
Normal file
7
lib/service/options_base.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Service::OptionsBase
|
||||
include ActiveModel::API
|
||||
include ActiveModel::Attributes
|
||||
include ActiveModel::AttributeMethods
|
||||
end
|
||||
@@ -98,6 +98,10 @@ class Service::StepsInspector
|
||||
nil
|
||||
end
|
||||
end
|
||||
#
|
||||
# @!visibility private
|
||||
class Options < Step
|
||||
end
|
||||
|
||||
attr_reader :steps, :result
|
||||
|
||||
|
||||
Reference in New Issue
Block a user