2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
module ApplicationController::RendersModels
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2021-08-17 05:44:54 +00:00
|
|
|
include ApplicationController::Paginates
|
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
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)
|
2021-08-25 12:24:42 +00:00
|
|
|
if object.included_modules.include?(ChecksCoreWorkflow)
|
|
|
|
clean_params[:screen] = 'create'
|
|
|
|
end
|
2017-03-09 11:44:51 +00:00
|
|
|
|
|
|
|
# create object
|
|
|
|
generic_object = object.new(clean_params)
|
|
|
|
|
|
|
|
# set relations
|
|
|
|
generic_object.associations_from_param(params)
|
|
|
|
|
2018-01-09 07:39:50 +00:00
|
|
|
# save object
|
|
|
|
generic_object.save!
|
|
|
|
|
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)
|
2021-08-25 12:24:42 +00:00
|
|
|
if object.included_modules.include?(ChecksCoreWorkflow)
|
|
|
|
clean_params[:screen] = 'update'
|
|
|
|
end
|
2017-03-09 11:44:51 +00:00
|
|
|
|
|
|
|
generic_object.with_lock do
|
|
|
|
|
2018-01-26 18:57:27 +00:00
|
|
|
# set relations
|
|
|
|
generic_object.associations_from_param(params)
|
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
# set attributes
|
2017-09-11 11:16:08 +00:00
|
|
|
generic_object.update!(clean_params)
|
2017-03-09 11:44:51 +00:00
|
|
|
|
|
|
|
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!
|
2021-07-16 13:29:38 +00:00
|
|
|
model_destroy_render_item
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
|
2021-07-16 13:33:48 +00:00
|
|
|
def model_destroy_render_item()
|
2017-03-09 11:44:51 +00:00
|
|
|
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)
|
2021-08-17 05:44:54 +00:00
|
|
|
paginate_with(default: 500)
|
2017-03-09 11:44:51 +00:00
|
|
|
|
2020-10-30 07:59:32 +00:00
|
|
|
sql_helper = ::SqlHelper.new(object: object)
|
|
|
|
sort_by = sql_helper.get_sort_by(params, 'id')
|
|
|
|
order_by = sql_helper.get_order_by(params, 'ASC')
|
|
|
|
order_sql = sql_helper.get_order(sort_by, order_by)
|
|
|
|
|
2021-08-17 05:44:54 +00:00
|
|
|
generic_objects = object.order(Arel.sql(order_sql)).offset(pagination.offset).limit(pagination.limit)
|
2017-03-09 11:44:51 +00:00
|
|
|
|
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: {
|
2020-10-30 07:59:32 +00:00
|
|
|
record_ids: item_ids,
|
|
|
|
assets: assets,
|
|
|
|
total_count: object.count
|
2017-03-09 11:44:51 +00:00
|
|
|
}, 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?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
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
|