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, #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 Attribute Details

#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
# File 'app/services/api_maker/valid_command.rb', line 4

def execute!
  ApiMaker::Configuration.profile(-> { "ValidCommand: #{model_class.name}" }) do
    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))

    succeed!(valid: model.valid?, errors: model.errors.full_messages)
  end
end

#resource_instance_classObject



19
20
21
# File 'app/services/api_maker/valid_command.rb', line 19

def resource_instance_class
  collection.klass
end

#sanitize_parameters(serializer) ⇒ Object



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

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