mirror of
https://github.com/discourse/discourse.git
synced 2024-12-16 04:46:00 +08:00
b8762172e4
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.
27 lines
693 B
Ruby
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
|