Class: ApiMaker::ValidCommand

Inherits:
BaseCommand show all
Defined in:
app/services/api_maker/valid_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/valid_command.rb', line 2

def command
  @command
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#serializerObject (readonly)

Returns the value of attribute serializer.



2
3
4
# File 'app/services/api_maker/valid_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
20
21
22
# File 'app/services/api_maker/valid_command.rb', line 4

def execute!
  each_command do |command|
    @command = command
    @params = command.args || {}

    if command.model_id.present?
      model = resource_instance_class.find(command.model_id)
    else
      model = resource_instance_class.new
    end

    serializer = serialized_resource(model)
    model.assign_attributes(sanitize_parameters(serializer))

    command.result(valid: model.valid?, errors: model.errors.full_messages)
  end

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

#resource_instance_classObject



24
25
26
# File 'app/services/api_maker/valid_command.rb', line 24

def resource_instance_class
  collection.klass
end

#sanitize_parameters(serializer) ⇒ Object



28
29
30
# File 'app/services/api_maker/valid_command.rb', line 28

def sanitize_parameters(serializer)
  serializer.resource_instance.permitted_params(ApiMaker::PermittedParamsArgument.new(command: command, model: serializer.model))
end

#serialized_resource(model) ⇒ Object



32
33
34
# File 'app/services/api_maker/valid_command.rb', line 32

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