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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pragma/rails/controller.rb', line 17

def run(operation_klass)
  result = operation_klass.to_s.constantize.call(
    params: operation_params.to_unsafe_h,
    current_user: operation_user
  )

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

  if result.resource
    render status: result.status, json: result.resource.to_json(user_options: {
      expand: params[:expand]
    })
  else
    head result.status
  end
end