discourse/app/helpers/with_service_helper.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

28 lines
722 B
Ruby
Raw Normal View History

# frozen_string_literal: true
module WithServiceHelper
def result
@_result
end
# @param service [Class] A class including {Service::Base}
# @param dependencies [kwargs] Any additional params to load into the service context,
# in addition to controller @params.
def with_service(service, **dependencies, &block)
object = self
ServiceRunner.call(
service,
object,
**dependencies,
&proc { instance_exec(&(block || proc {})) }
)
end
def run_service(service, dependencies)
params = self.try(:params) || ActionController::Parameters.new
@_result =
service.call(params.to_unsafe_h.merge(guardian: self.try(:guardian) || nil, **dependencies))
end
end