Class: ApiMaker::CreateCommand
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
Instance Attribute Details
#command ⇒ Object
Returns the value of attribute command.
2
3
4
|
# File 'app/services/api_maker/create_command.rb', line 2
def command
@command
end
|
#model ⇒ Object
Returns the value of attribute model.
2
3
4
|
# File 'app/services/api_maker/create_command.rb', line 2
def model
@model
end
|
#params ⇒ Object
Returns the value of attribute params.
2
3
4
|
# File 'app/services/api_maker/create_command.rb', line 2
def params
@params
end
|
#serializer ⇒ Object
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_class ⇒ Object
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_class ⇒ Object
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_name ⇒ Object
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_name ⇒ Object
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_parameters ⇒ Object
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_response ⇒ Object
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
|