Class: ApiMaker::CreateCommand

Inherits:
BaseCommand show all
Defined in:
app/services/api_maker/create_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

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#serializerObject (readonly)

Returns the value of attribute serializer.



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

def serializer
  @serializer
end

Instance Method Details

#api_maker_resource_classObject



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

def api_maker_resource_class
  @api_maker_resource_class ||= "Resources::#{collection.klass.name}Resource".constantize
end

#execute!Object



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

def execute!
  ApiMaker::Configuration.profile(-> { "CreateCommand: #{model_class.name}" }) do
    @model = collection.klass.new
    @serializer = serialized_resource(model)
    sanitized_parameters = sanitize_parameters
    @model.assign_attributes(sanitized_parameters)

    if !current_ability.can?(:create, model)
      failure_response(errors: ["No access to create that resource"])
    elsif model.save
      success_response
    else
      failure_save_response(model: model, params: sanitized_parameters)
    end
  end
end

#resource_instance_classObject



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

def resource_instance_class
  @resource_instance_class ||= api_maker_resource_class.model_class
end

#resource_instance_class_nameObject



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

def resource_instance_class_name
  @resource_instance_class_name ||= self.class.name.split("::").last.gsub(/Controller$/, "").singularize
end

#resource_variable_nameObject



33
34
35
# File 'app/services/api_maker/create_command.rb', line 33

def resource_variable_name
  @resource_variable_name ||= resource_instance_class_name.underscore.parameterize
end

#sanitize_parametersObject



37
38
39
# File 'app/services/api_maker/create_command.rb', line 37

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

#success_responseObject



41
42
43
44
45
46
# File 'app/services/api_maker/create_command.rb', line 41

def success_response
  succeed!(
    model: serialized_model(model),
    success: true
  )
end