DEV: Remove hash-like access from service contracts

We decided to keep only one way to access values from a contract. This
patch thus removes the hash-like access from contracts.
This commit is contained in:
Loïc Guitaut
2024-10-28 17:21:59 +01:00
committed by Loïc Guitaut
parent 4d0ed2e146
commit 2f334964f2
50 changed files with 293 additions and 257 deletions

View File

@@ -4,6 +4,11 @@ class AdminNotices::Dismiss
include Service::Base
policy :invalid_access
params do
attribute :id, :integer
validates :id, presence: true
end
model :admin_notice, optional: true
transaction do
step :destroy
@@ -17,7 +22,7 @@ class AdminNotices::Dismiss
end
def fetch_admin_notice(params:)
AdminNotice.find_by(id: params[:id])
AdminNotice.find_by(id: params.id)
end
def destroy(admin_notice:)

View File

@@ -18,13 +18,13 @@ class Experiments::Toggle
end
def setting_is_available(params:)
SiteSetting.respond_to?(params[:setting_name])
SiteSetting.respond_to?(params.setting_name)
end
def toggle(params:, guardian:)
SiteSetting.set_and_log(
params[:setting_name],
!SiteSetting.public_send(params[:setting_name]),
params.setting_name,
!SiteSetting.public_send(params.setting_name),
guardian.user,
)
end

View File

@@ -32,7 +32,7 @@ class Flags::CreateFlag
end
def unique_name(params:)
!Flag.custom.where(name: params[:name]).exists?
!Flag.custom.where(name: params.name).exists?
end
def instantiate_flag(params:)

View File

@@ -3,6 +3,11 @@
class Flags::DestroyFlag
include Service::Base
params do
attribute :id, :integer
validates :id, presence: true
end
model :flag
policy :not_system
policy :not_used
@@ -15,7 +20,7 @@ class Flags::DestroyFlag
private
def fetch_flag(params:)
Flag.find_by(id: params[:id])
Flag.find_by(id: params.id)
end
def not_system(flag:)

View File

@@ -22,7 +22,7 @@ class Flags::ReorderFlag
private
def fetch_flag(params:)
Flag.find_by(id: params[:flag_id])
Flag.find_by(id: params.flag_id)
end
def invalid_access(guardian:, flag:)
@@ -34,15 +34,15 @@ class Flags::ReorderFlag
end
def invalid_move(flag:, params:, all_flags:)
return false if all_flags.first == flag && params[:direction] == "up"
return false if all_flags.last == flag && params[:direction] == "down"
return false if all_flags.first == flag && params.direction == "up"
return false if all_flags.last == flag && params.direction == "down"
true
end
def move(flag:, params:, all_flags:)
old_position = flag.position
index = all_flags.index(flag)
target_flag = all_flags[params[:direction] == "up" ? index - 1 : index + 1]
target_flag = all_flags[params.direction == "up" ? index - 1 : index + 1]
flag.update!(position: target_flag.position)
target_flag.update!(position: old_position)
@@ -51,7 +51,7 @@ class Flags::ReorderFlag
def log(guardian:, flag:, params:)
StaffActionLogger.new(guardian.user).log_custom(
"move_flag",
{ flag: flag.name, direction: params[:direction] },
{ flag: flag.name, direction: params.direction },
)
end
end

View File

@@ -22,7 +22,7 @@ class Flags::ToggleFlag
end
def fetch_flag(params:)
Flag.find_by(id: params[:flag_id])
Flag.find_by(id: params.flag_id)
end
def toggle(flag:)

View File

@@ -32,7 +32,7 @@ class Flags::UpdateFlag
private
def fetch_flag(params:)
Flag.find_by(id: params[:id])
Flag.find_by(id: params.id)
end
def not_system(flag:)
@@ -48,7 +48,7 @@ class Flags::UpdateFlag
end
def unique_name(params:)
!Flag.custom.where(name: params[:name]).where.not(id: params[:id]).exists?
!Flag.custom.where(name: params.name).where.not(id: params.id).exists?
end
def update(flag:, params:)

View File

@@ -45,16 +45,16 @@ class SiteSetting::Update
end
def setting_is_visible(params:, options:)
options.allow_changing_hidden || !SiteSetting.hidden_settings.include?(params[:setting_name])
options.allow_changing_hidden || !SiteSetting.hidden_settings.include?(params.setting_name)
end
def setting_is_configurable(params:)
return true if !SiteSetting.plugins[params[:setting_name]]
return true if !SiteSetting.plugins[params.setting_name]
Discourse.plugins_by_name[SiteSetting.plugins[params[:setting_name]]].configurable?
Discourse.plugins_by_name[SiteSetting.plugins[params.setting_name]].configurable?
end
def save(params:, guardian:)
SiteSetting.set_and_log(params[:setting_name], params[:new_value], guardian.user)
SiteSetting.set_and_log(params.setting_name, params.new_value, guardian.user)
end
end

View File

@@ -30,11 +30,11 @@ class User::Silence
private
def fetch_user(params:)
User.find_by(id: params[:user_id])
User.find_by(id: params.user_id)
end
def fetch_users(user:, params:)
[user, *User.where(id: params[:other_user_ids].to_a.uniq).to_a]
[user, *User.where(id: params.other_user_ids.to_a.uniq).to_a]
end
def can_silence_all_users(guardian:, users:)
@@ -46,7 +46,7 @@ class User::Silence
end
def fetch_post(params:)
Post.find_by(id: params[:post_id])
Post.find_by(id: params.post_id)
end
def perform_post_action(guardian:, post:, params:)

View File

@@ -30,11 +30,11 @@ class User::Suspend
private
def fetch_user(params:)
User.find_by(id: params[:user_id])
User.find_by(id: params.user_id)
end
def fetch_users(user:, params:)
[user, *User.where(id: params[:other_user_ids].to_a.uniq).to_a]
[user, *User.where(id: params.other_user_ids.to_a.uniq).to_a]
end
def can_suspend_all_users(guardian:, users:)
@@ -46,7 +46,7 @@ class User::Suspend
end
def fetch_post(params:)
Post.find_by(id: params[:post_id])
Post.find_by(id: params.post_id)
end
def perform_post_action(guardian:, post:, params:)