DEV: Drop WithServiceHelper

This patch removes the `with_service` helper from the code base.
Instead, we can pass a block with actions directly to the `.call` method
of a service.

This simplifies how to use services:
- use `.call` without a block to run the service and get its result
  object.
- use `.call` with a block of actions to run the service and execute
  arbitrary code depending on the service outcome.

It also means a service is now “self-contained” and can be used anywhere
without having to include a helper or whatever.
This commit is contained in:
Loïc Guitaut
2024-09-03 18:30:22 +02:00
committed by Loïc Guitaut
parent c76ff5c994
commit e94707acdf
39 changed files with 99 additions and 167 deletions

View File

@ -2,8 +2,6 @@
module ChatSDK
class Thread
include WithServiceHelper
# Updates the title of a specified chat thread.
#
# @param title [String] The new title for the chat thread.
@ -76,13 +74,7 @@ module ChatSDK
end
def messages(thread_id:, guardian:, direction: "future", **params)
with_service(
Chat::ListChannelThreadMessages,
thread_id: thread_id,
guardian: guardian,
direction: direction,
**params,
) do
Chat::ListChannelThreadMessages.call(thread_id:, guardian:, direction:, **params) do
on_success { result.messages }
on_failed_policy(:can_view_thread) { raise "Guardian can't view thread" }
on_failed_policy(:target_message_exists) { raise "Target message doesn't exist" }
@ -91,7 +83,7 @@ module ChatSDK
end
def update(**params)
with_service(Chat::UpdateThread, **params) do
Chat::UpdateThread.call(params) do
on_model_not_found(:channel) do
raise "Couldn’t find channel with id: `#{params[:channel_id]}`"
end