discourse/plugins/chat/app/helpers/with_service_helper.rb
Loïc Guitaut b8762172e4 DEV: Allow with_service in jobs
This patch introduces a new `ServiceJob` class allowing the use of
`with_service` in jobs.

This way, it’s easier to use the chat service objects in jobs and
provides the same level of functionality than the one we have in
controllers.
2023-02-23 09:28:53 +01:00

27 lines
693 B
Ruby

# frozen_string_literal: true
module Chat
module WithServiceHelper
def result
@_result
end
def with_service(service, default_actions: true, **dependencies, &block)
object = self
merged_block =
proc do
instance_exec(&object.method(:default_actions_for_service).call) if default_actions
instance_exec(&(block || proc {}))
end
Chat::ServiceRunner.call(service, object, **dependencies, &merged_block)
end
def run_service(service, dependencies)
@_result = service.call(params.to_unsafe_h.merge(guardian: guardian, **dependencies))
end
def default_actions_for_service
proc {}
end
end
end