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, #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/create_command.rb', line 2

def command
  @command
end

#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

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
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



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

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

def execute!
  each_command do |command|
    @command = command
    @model = collection.klass.new
    @params = command.args || {}
    @serializer = serialized_resource(model)
    @model.assign_attributes(sanitize_parameters)

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

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

#failure_response(errors) ⇒ Object



28
29
30
31
32
33
34
# File 'app/services/api_maker/create_command.rb', line 28

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

#resource_instance_classObject



40
41
42
# File 'app/services/api_maker/create_command.rb', line 40

def resource_instance_class
  @resource_instance_class ||= api_maker_resource_class.model_class
end

#resource_instance_class_nameObject



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

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

#resource_variable_nameObject



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

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

#sanitize_parametersObject



48
49
50
# File 'app/services/api_maker/create_command.rb', line 48

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

#serialized_resource(model) ⇒ Object



52
53
54
# File 'app/services/api_maker/create_command.rb', line 52

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

#success_responseObject



56
57
58
59
60
61
# File 'app/services/api_maker/create_command.rb', line 56

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