mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Replace params by the contract object in services
This patch replaces the parameters provided to a service through `params` by the contract object. That way, it allows better consistency when accessing input params. For example, if you have a service without a contract, to access a parameter, you need to use `params[:my_parameter]`. But with a contract, you do this through `contract.my_parameter`. Now, with this patch, you’ll be able to access it through `params.my_parameter` or `params[:my_parameter]`. Some methods have been added to the contract object to better mimic a Hash. That way, when accessing/using `params`, you don’t have to think too much about it: - `params.my_key` is also accessible through `params[:my_key]`. - `params.my_key = value` can also be done through `params[:my_key] = value`. - `#slice` and `#merge` are available. - `#to_hash` has been implemented, so the contract object will be automatically cast as a hash by Ruby depending on the context. For example, with an AR model, you can do this: `user.update(**params)`.
This commit is contained in:
committed by
Loïc Guitaut
parent
c7db44cfe7
commit
584424594e
@@ -3,9 +3,9 @@
|
||||
class User::Action::SilenceAll < Service::ActionBase
|
||||
option :users, []
|
||||
option :actor
|
||||
option :contract
|
||||
option :params
|
||||
|
||||
delegate :message, :post_id, :silenced_till, :reason, to: :contract, private: true
|
||||
delegate :message, :post_id, :silenced_till, :reason, to: :params, private: true
|
||||
|
||||
def call
|
||||
silenced_users.first.try(:user_history).try(:details)
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
class User::Action::SuspendAll < Service::ActionBase
|
||||
option :users, []
|
||||
option :actor
|
||||
option :contract
|
||||
option :params
|
||||
|
||||
delegate :message, :post_id, :suspend_until, :reason, to: :contract, private: true
|
||||
delegate :message, :post_id, :suspend_until, :reason, to: :params, private: true
|
||||
|
||||
def call
|
||||
suspended_users.first.try(:user_history).try(:details)
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
class User::Action::TriggerPostAction < Service::ActionBase
|
||||
option :guardian
|
||||
option :post
|
||||
option :contract
|
||||
option :params
|
||||
|
||||
delegate :post_action, to: :contract, private: true
|
||||
delegate :post_action, to: :params, private: true
|
||||
delegate :user, to: :guardian, private: true
|
||||
|
||||
def call
|
||||
@@ -30,7 +30,7 @@ class User::Action::TriggerPostAction < Service::ActionBase
|
||||
# Take what the moderator edited in as gospel
|
||||
PostRevisor.new(post).revise!(
|
||||
user,
|
||||
{ raw: contract.post_edit },
|
||||
{ raw: params.post_edit },
|
||||
skip_validations: true,
|
||||
skip_revision: true,
|
||||
)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
class User::Silence
|
||||
include Service::Base
|
||||
|
||||
contract do
|
||||
params do
|
||||
attribute :user_id, :integer
|
||||
attribute :reason, :string
|
||||
attribute :message, :string
|
||||
@@ -29,27 +29,27 @@ class User::Silence
|
||||
|
||||
private
|
||||
|
||||
def fetch_user(contract:)
|
||||
User.find_by(id: contract.user_id)
|
||||
def fetch_user(params:)
|
||||
User.find_by(id: params[:user_id])
|
||||
end
|
||||
|
||||
def fetch_users(user:, contract:)
|
||||
[user, *User.where(id: contract.other_user_ids.to_a.uniq).to_a]
|
||||
def fetch_users(user:, params:)
|
||||
[user, *User.where(id: params[:other_user_ids].to_a.uniq).to_a]
|
||||
end
|
||||
|
||||
def can_silence_all_users(guardian:, users:)
|
||||
users.all? { guardian.can_silence_user?(_1) }
|
||||
end
|
||||
|
||||
def silence(guardian:, users:, contract:)
|
||||
context[:full_reason] = User::Action::SilenceAll.call(users:, actor: guardian.user, contract:)
|
||||
def silence(guardian:, users:, params:)
|
||||
context[:full_reason] = User::Action::SilenceAll.call(users:, actor: guardian.user, params:)
|
||||
end
|
||||
|
||||
def fetch_post(contract:)
|
||||
Post.find_by(id: contract.post_id)
|
||||
def fetch_post(params:)
|
||||
Post.find_by(id: params[:post_id])
|
||||
end
|
||||
|
||||
def perform_post_action(guardian:, post:, contract:)
|
||||
User::Action::TriggerPostAction.call(guardian:, post:, contract:)
|
||||
def perform_post_action(guardian:, post:, params:)
|
||||
User::Action::TriggerPostAction.call(guardian:, post:, params:)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
class User::Suspend
|
||||
include Service::Base
|
||||
|
||||
contract do
|
||||
params do
|
||||
attribute :user_id, :integer
|
||||
attribute :reason, :string
|
||||
attribute :message, :string
|
||||
@@ -29,27 +29,27 @@ class User::Suspend
|
||||
|
||||
private
|
||||
|
||||
def fetch_user(contract:)
|
||||
User.find_by(id: contract.user_id)
|
||||
def fetch_user(params:)
|
||||
User.find_by(id: params[:user_id])
|
||||
end
|
||||
|
||||
def fetch_users(user:, contract:)
|
||||
[user, *User.where(id: contract.other_user_ids.to_a.uniq).to_a]
|
||||
def fetch_users(user:, params:)
|
||||
[user, *User.where(id: params[:other_user_ids].to_a.uniq).to_a]
|
||||
end
|
||||
|
||||
def can_suspend_all_users(guardian:, users:)
|
||||
users.all? { guardian.can_suspend?(_1) }
|
||||
end
|
||||
|
||||
def suspend(guardian:, users:, contract:)
|
||||
context[:full_reason] = User::Action::SuspendAll.call(users:, actor: guardian.user, contract:)
|
||||
def suspend(guardian:, users:, params:)
|
||||
context[:full_reason] = User::Action::SuspendAll.call(users:, actor: guardian.user, params:)
|
||||
end
|
||||
|
||||
def fetch_post(contract:)
|
||||
Post.find_by(id: contract.post_id)
|
||||
def fetch_post(params:)
|
||||
Post.find_by(id: params[:post_id])
|
||||
end
|
||||
|
||||
def perform_post_action(guardian:, post:, contract:)
|
||||
User::Action::TriggerPostAction.call(guardian:, post:, contract:)
|
||||
def perform_post_action(guardian:, post:, params:)
|
||||
User::Action::TriggerPostAction.call(guardian:, post:, params:)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user