2017-03-09 11:44:51 +00:00
|
|
|
module ApplicationController::RendersModels
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# model helper
|
|
|
|
def model_create_render(object, params)
|
|
|
|
|
|
|
|
clean_params = object.association_name_to_id_convert(params)
|
|
|
|
clean_params = object.param_cleanup(clean_params, true)
|
|
|
|
|
|
|
|
# create object
|
|
|
|
generic_object = object.new(clean_params)
|
|
|
|
|
|
|
|
# save object
|
|
|
|
generic_object.save!
|
|
|
|
|
|
|
|
# set relations
|
|
|
|
generic_object.associations_from_param(params)
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_expand?
|
2017-03-09 11:44:51 +00:00
|
|
|
render json: generic_object.attributes_with_association_names, status: :created
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_full?
|
|
|
|
render json: generic_object.class.full(generic_object.id), status: :created
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
model_create_render_item(generic_object)
|
|
|
|
end
|
|
|
|
|
|
|
|
def model_create_render_item(generic_object)
|
|
|
|
render json: generic_object.attributes_with_association_ids, status: :created
|
|
|
|
end
|
|
|
|
|
|
|
|
def model_update_render(object, params)
|
|
|
|
|
|
|
|
# find object
|
|
|
|
generic_object = object.find(params[:id])
|
|
|
|
|
|
|
|
clean_params = object.association_name_to_id_convert(params)
|
|
|
|
clean_params = object.param_cleanup(clean_params, true)
|
|
|
|
|
|
|
|
generic_object.with_lock do
|
|
|
|
|
|
|
|
# set attributes
|
2017-09-11 11:16:08 +00:00
|
|
|
generic_object.update!(clean_params)
|
2017-03-09 11:44:51 +00:00
|
|
|
|
|
|
|
# set relations
|
|
|
|
generic_object.associations_from_param(params)
|
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_expand?
|
2017-03-09 11:44:51 +00:00
|
|
|
render json: generic_object.attributes_with_association_names, status: :ok
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_full?
|
|
|
|
render json: generic_object.class.full(generic_object.id), status: :ok
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
model_update_render_item(generic_object)
|
|
|
|
end
|
|
|
|
|
|
|
|
def model_update_render_item(generic_object)
|
|
|
|
render json: generic_object.attributes_with_association_ids, status: :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def model_destroy_render(object, params)
|
|
|
|
generic_object = object.find(params[:id])
|
|
|
|
generic_object.destroy!
|
|
|
|
model_destroy_render_item()
|
|
|
|
end
|
|
|
|
|
|
|
|
def model_destroy_render_item ()
|
|
|
|
render json: {}, status: :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def model_show_render(object, params)
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_expand?
|
2017-03-09 11:44:51 +00:00
|
|
|
generic_object = object.find(params[:id])
|
|
|
|
render json: generic_object.attributes_with_association_names, status: :ok
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_full?
|
|
|
|
render json: object.full(params[:id]), status: :ok
|
2017-03-09 11:44:51 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
model_show_render_item(object.find(params[:id]))
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def model_show_render_item(generic_object)
|
|
|
|
render json: generic_object.attributes_with_association_ids, status: :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def model_index_render(object, params)
|
|
|
|
offset = 0
|
|
|
|
per_page = 500
|
|
|
|
if params[:page] && params[:per_page]
|
|
|
|
offset = (params[:page].to_i - 1) * params[:per_page].to_i
|
|
|
|
limit = params[:per_page].to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
if per_page > 500
|
|
|
|
per_page = 500
|
|
|
|
end
|
|
|
|
|
|
|
|
generic_objects = if offset.positive?
|
|
|
|
object.limit(params[:per_page]).order(id: 'ASC').offset(offset).limit(limit)
|
|
|
|
else
|
|
|
|
object.all.order(id: 'ASC').offset(offset).limit(limit)
|
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_expand?
|
2017-03-09 11:44:51 +00:00
|
|
|
list = []
|
2017-10-01 12:25:52 +00:00
|
|
|
generic_objects.each do |generic_object|
|
2017-03-09 11:44:51 +00:00
|
|
|
list.push generic_object.attributes_with_association_names
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-03-09 11:44:51 +00:00
|
|
|
render json: list, status: :ok
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-12-14 13:19:24 +00:00
|
|
|
if response_full?
|
2017-03-09 11:44:51 +00:00
|
|
|
assets = {}
|
|
|
|
item_ids = []
|
2017-10-01 12:25:52 +00:00
|
|
|
generic_objects.each do |item|
|
2017-03-09 11:44:51 +00:00
|
|
|
item_ids.push item.id
|
|
|
|
assets = item.assets(assets)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-03-09 11:44:51 +00:00
|
|
|
render json: {
|
|
|
|
record_ids: item_ids,
|
|
|
|
assets: assets,
|
|
|
|
}, status: :ok
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
generic_objects_with_associations = []
|
2017-10-01 12:25:52 +00:00
|
|
|
generic_objects.each do |item|
|
2017-03-09 11:44:51 +00:00
|
|
|
generic_objects_with_associations.push item.attributes_with_association_ids
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-03-09 11:44:51 +00:00
|
|
|
model_index_render_result(generic_objects_with_associations)
|
|
|
|
end
|
|
|
|
|
|
|
|
def model_index_render_result(generic_objects)
|
|
|
|
render json: generic_objects, status: :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def model_references_check(object, params)
|
|
|
|
generic_object = object.find(params[:id])
|
|
|
|
result = Models.references(object, generic_object.id)
|
2017-11-23 08:09:44 +00:00
|
|
|
return false if result.blank?
|
2017-03-09 11:44:51 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, 'Can\'t delete, object has references.'
|
|
|
|
rescue => e
|
|
|
|
raise Exceptions::UnprocessableEntity, e
|
|
|
|
end
|
|
|
|
end
|