Class: ApiMaker::DestroyCommand

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

Instance Attribute Summary collapse

Attributes inherited from BaseCommand

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

Instance Method Summary collapse

Methods inherited from BaseCommand

#each_command, execute_in_thread!, goldiloader?, #initialize

Constructor Details

This class inherits a constructor from ApiMaker::BaseCommand

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



2
3
4
# File 'app/services/api_maker/destroy_command.rb', line 2

def command
  @command
end

#modelObject (readonly)

Returns the value of attribute model.



2
3
4
# File 'app/services/api_maker/destroy_command.rb', line 2

def model
  @model
end

#paramsObject (readonly)

Returns the value of attribute params.



2
3
4
# File 'app/services/api_maker/destroy_command.rb', line 2

def params
  @params
end

#serializerObject (readonly)

Returns the value of attribute serializer.



2
3
4
# File 'app/services/api_maker/destroy_command.rb', line 2

def serializer
  @serializer
end

Instance Method Details

#execute!Object



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

def execute!
  each_command do |command|
    @command = command
    @model = command.model
    @params = command.args || {}
    @serializer = serialized_resource(model)

    if command.model.destroy
      success_response
    else
      failure_response
    end
  end

  ServicePattern::Response.new(success: true)
end

#failure_responseObject



21
22
23
24
25
26
27
# File 'app/services/api_maker/destroy_command.rb', line 21

def failure_response
  command.fail(
    model: serializer.result,
    success: false,
    errors: model.errors.full_messages
  )
end

#serialized_resource(model) ⇒ Object



29
30
31
# File 'app/services/api_maker/destroy_command.rb', line 29

def serialized_resource(model)
  ApiMaker::Serializer.new(ability: current_ability, args: api_maker_args, model: model)
end

#success_responseObject



33
34
35
36
37
38
# File 'app/services/api_maker/destroy_command.rb', line 33

def success_response
  command.result(
    model: serializer.result,
    success: true
  )
end