Class: Stockpot::RecordsController
Instance Method Summary
collapse
#rescue_error, #return_error
Instance Method Details
#create ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'app/controllers/stockpot/records_controller.rb', line 35
def create
ActiveRecord::Base.transaction do
factories.each do |element|
ids = []
list = element[:list] || 1
factory = element[:factory]
traits = element[:traits].map(&:to_sym) if element[:traits].present?
list.times do |n|
attributes = element[:attributes][n].to_h if element[:attributes].present?
factory_arguments = [ factory, *traits, attributes ].compact
@factory_instance = FactoryBot.create(*factory_arguments)
ids << @factory_instance.id
end
factory_name = pluralize(factory).camelize(:lower)
@response_data[factory_name] = [] unless @response_data.key?(factory_name)
@response_data[factory_name].concat(@factory_instance.class.name.constantize.where(id: ids))
end
end
render json: @response_data, status: :accepted
end
|
#destroy ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'app/controllers/stockpot/records_controller.rb', line 59
def destroy
ActiveRecord::Base.transaction do
models.each_with_index do |element, i|
model = element[:model]
class_name = find_correct_class_name(model)
formatted_model = pluralize(model).camelize(:lower).gsub("::", "")
class_name.constantize.where(models[i].except(:model)).each do |record|
record.destroy!
@response_data[formatted_model] = [] unless @response_data.key?(formatted_model)
@response_data[formatted_model] << record
end
end
end
render json: @response_data, status: :accepted
end
|
#index ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'app/controllers/stockpot/records_controller.rb', line 22
def index
models.each_with_index do |element, i|
model = element[:model]
class_name = find_correct_class_name(model)
formatted_model = pluralize(model).camelize(:lower).gsub("::", "")
@response_data[formatted_model] = [] unless @response_data.key?(formatted_model)
@response_data[formatted_model].concat(class_name.constantize.where(models[i].except(:model)))
@response_data[formatted_model].reverse!.uniq! { |obj| obj["id"] }
end
render json: @response_data, status: :ok
end
|
#update ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'app/controllers/stockpot/records_controller.rb', line 76
def update
ActiveRecord::Base.transaction do
models.each_with_index do |element, i|
model = element[:model]
class_name = find_correct_class_name(model)
update_params = params.permit![:models][i][:update].to_h
attributes_to_search = models[i].except(:model, :update)
formatted_model = pluralize(model).camelize(:lower).gsub("::", "")
class_name.constantize.where(attributes_to_search).each do |record|
record.update!(update_params)
@response_data[formatted_model] = [] unless @response_data.key?(formatted_model)
@response_data[formatted_model] << class_name.constantize.find(record.id)
@response_data[formatted_model].reverse!.uniq! { |obj| obj["id"] }
end
end
end
render json: @response_data, status: :accepted
end
|