class ApiMaker::BaseCommand

Attributes

api_maker_args[R]
collection[R]
command_response[R]
commands[R]
controller[R]
current_ability[R]

Public Class Methods

execute_in_thread!(**args) click to toggle source
# File lib/api_maker/base_command.rb, line 22
def self.execute_in_thread!(**args)
  args.fetch(:command_response).with_thread do
    new(**args).execute!
  end
end
goldiloader?() click to toggle source

Returns true if the gem “goldiloader” is present in the app

# File lib/api_maker/base_command.rb, line 5
def self.goldiloader?
  @goldiloader = Gem::Specification.find_all_by_name("goldiloader").any? if @goldiloader.nil?
  @goldiloader
end
new(ability:, args:, collection:, commands:, command_response:, controller:) click to toggle source
# File lib/api_maker/base_command.rb, line 10
def initialize(ability:, args:, collection:, commands:, command_response:, controller:)
  @api_maker_args = args
  @current_ability = ability
  @collection = collection
  @commands = commands
  @command_response = command_response
  @controller = controller

  # Make it possible to do custom preloads (useful in threadded mode that doesnt support Goldiloader)
  @collection = custom_collection(@collection) if respond_to?(:custom_collection)
end

Public Instance Methods

each_command(args = {}, &blk) click to toggle source
# File lib/api_maker/base_command.rb, line 28
def each_command(args = {}, &blk)
  if args[:threadded]
    # Goldiloader doesn't work with threads (loads all relationships for each thread)
    @collection = @collection.auto_include(false) if ApiMaker::BaseCommand.goldiloader?

    # Load relationship before commands so each command doesn't query on its own
    @collection.load
  end

  @commands.each do |command_id, command_data|
    if args[:threadded]
      command_response.with_thread do
        run_command(command_id, command_data, &blk)
      end
    else
      run_command(command_id, command_data, &blk)
    end
  end
end

Private Instance Methods

command_error_message(error) click to toggle source
# File lib/api_maker/base_command.rb, line 74
def command_error_message(error)
  if Rails.application.config.consider_all_requests_local
    "#{error.class.name}: #{error.message}"
  else
    "Internal server error"
  end
end
run_command(command_id, command_data) { |command| ... } click to toggle source
# File lib/api_maker/base_command.rb, line 52
def run_command(command_id, command_data)
  command = ApiMaker::IndividualCommand.new(
    args: command_data[:args],
    collection: @collection,
    command: self,
    id: command_id,
    primary_key: command_data[:primary_key],
    response: command_response
  )

  begin
    yield command
  rescue => e # rubocop:disable Style/RescueStandardError
    command.error(success: false, errors: [command_error_message(e)])

    Rails.logger.error e.message
    Rails.logger.error e.backtrace.join("\n")

    ApiMaker::Configuration.current.report_error(controller: controller, error: e)
  end
end