Class: ApiMaker::ServiceCommand

Inherits:
BaseCommand show all
Defined in:
app/services/api_maker/service_command.rb

Instance Attribute Summary

Attributes inherited from BaseCommand

#api_maker_args, #collection, #collection_instance, #command, #command_response, #commands, #controller, #current_ability

Instance Method Summary collapse

Methods inherited from BaseCommand

command_error_message, each_command, execute_in_thread!, #execute_service_or_fail, #execute_with_response, #fail!, #fail_command_from_service_error_response, #failure_response, #failure_save_response, goldiloader?, #initialize, #inspect, #model_class, run_command, #save_models_or_fail, #serialize_service_errors, #succeed!

Constructor Details

This class inherits a constructor from ApiMaker::BaseCommand

Instance Method Details

#execute!Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/services/api_maker/service_command.rb', line 2

def execute!
  service_name = args.fetch(:service_name)

  ApiMaker::Configuration.profile(-> { "ServiceCommand: #{service_name}" }) do
    service_constant_name = "Services::#{service_name}"
    service_constant = service_constant_name.constantize
    service_args = args.fetch(:service_args)&.permit!&.to_h
    response = service_constant.execute(
      ability: current_ability,
      args: service_args,
      api_maker_args: api_maker_args,
      controller: controller
    )

    if response.success?
      succeed!(response.result)
    else
      errors = response.errors.map do |error|
        {message: error.message, type: error.type}
      end

      fail!(errors: errors)
    end
  end
end