Module: Pragma::Rails::Controller::InstanceMethods

Defined in:
lib/pragma/rails/controller.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#run(operation_klass) ⇒ Object

Runs an operation, then responds with the returned status code, headers and resource.

Parameters:

  • operation_klass (Class|String)

    a subclass of Pragma::Operation::Base



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pragma/rails/controller.rb', line 18

def run(operation_klass)
  operation_const = if operation_klass.is_a?(Class)
    operation_klass
  else
    operation_klass.to_s.constantize
  end

  result = operation_const.call(
    operation_params,
    'current_user' => operation_user,
    'policy.context' => policy_context
  )

  result['result.response'].headers.each_pair do |key, value|
    response.headers[key] = value
  end

  if result['result.response'].entity
    render(
      status: result['result.response'].status,
      json: entity_to_hash(result['result.response'].entity).as_json
    )
  else
    head result['result.response'].status
  end
end