Class: ApiMaker::CollectionCommandService

Inherits:
CommandService show all
Defined in:
app/services/api_maker/collection_command_service.rb

Instance Attribute Summary

Attributes inherited from CommandService

#ability, #api_maker_args, #command_name, #command_response, #commands, #controller, #resource_name

Instance Method Summary collapse

Methods inherited from CommandService

#initialize, #model_class, #namespace, #resource

Methods inherited from ApplicationService

#api_maker_json

Constructor Details

This class inherits a constructor from ApiMaker::CommandService

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'app/services/api_maker/collection_command_service.rb', line 21

def authorized?
  ability.can?(command_name.to_sym, model_class)
end

#constantObject



25
26
27
# File 'app/services/api_maker/collection_command_service.rb', line 25

def constant
  @constant ||= "Commands::#{namespace}::#{command_name.camelize}".constantize
end

#fail_with_no_accessObject



29
30
31
32
33
34
35
36
37
# File 'app/services/api_maker/collection_command_service.rb', line 29

def fail_with_no_access
  commands.each_key do |command_id|
    command_response.error_for_command(
      command_id,
      success: false,
      errors: ["No access to '#{command_name}' on '#{model_class.name}'"]
    )
  end
end

#performObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/services/api_maker/collection_command_service.rb', line 2

def perform
  ApiMaker::Configuration.profile(-> { "CollectionCommand: #{namespace}::#{command_name}" }) do
    if authorized?
      constant.execute_in_thread!(
        ability: ability,
        api_maker_args: api_maker_args,
        collection: nil,
        commands: commands,
        command_response: command_response,
        controller: controller
      )
    else
      fail_with_no_access
    end

    succeed!
  end
end