123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- module ApplicationController::RendersModels
- extend ActiveSupport::Concern
- include CanPaginate
- 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)
- if object.included_modules.include?(ChecksCoreWorkflow)
- clean_params[:screen] = 'create'
- end
- # create object
- generic_object = object.new(clean_params)
- # set relations
- generic_object.associations_from_param(params)
- # save object
- generic_object.save!
- if response_expand?
- render json: generic_object.attributes_with_association_names, status: :created
- return
- end
- if response_full?
- render json: generic_object.class.full(generic_object.id), status: :created
- return
- end
- 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)
- if object.included_modules.include?(ChecksCoreWorkflow)
- clean_params[:screen] = 'edit'
- end
- generic_object.with_lock do
- # set relations
- generic_object.associations_from_param(params)
- # set attributes
- generic_object.update!(clean_params)
- end
- if response_expand?
- render json: generic_object.attributes_with_association_names, status: :ok
- return
- end
- if response_full?
- render json: generic_object.class.full(generic_object.id), status: :ok
- return
- end
- 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)
- if response_expand?
- generic_object = object.find(params[:id])
- render json: generic_object.attributes_with_association_names, status: :ok
- return
- end
- if response_full?
- render json: object.full(params[:id]), status: :ok
- return
- end
- model_show_render_item(object.find(params[:id]))
- 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)
- paginate_with(default: 500)
- 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)
- generic_objects = object.reorder(Arel.sql(order_sql)).offset(pagination.offset).limit(pagination.limit)
- if response_expand?
- list = generic_objects.map(&:attributes_with_association_names)
- render json: list, status: :ok
- return
- end
- if response_full?
- assets = {}
- item_ids = []
- generic_objects.each do |item|
- item_ids.push item.id
- assets = item.assets(assets)
- end
- render json: {
- record_ids: item_ids,
- assets: assets,
- total_count: object.count
- }, status: :ok
- return
- end
- generic_objects_with_associations = generic_objects.map(&:attributes_with_association_ids)
- 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)
- return false if result.blank?
- raise Exceptions::UnprocessableEntity, __('Can\'t delete, object has references.')
- rescue => e
- raise Exceptions::UnprocessableEntity, e
- end
- def model_search_render(object, params)
- paginate_with(max: 200, default: 50)
- generic_objects = object.search(
- query: params[:query] || params[:term],
- condition: params[:condition],
- ids: params[:ids],
- role_ids: params[:role_ids],
- group_ids: params[:group_ids],
- permissions: params[:permissions],
- only_total_count: response_only_total_count?,
- sort_by: params[:sort_by],
- order_by: params[:order_by],
- offset: pagination.offset,
- limit: pagination.limit,
- current_user: current_user,
- full: true,
- with_total_count: true,
- ) || { objects: [], total_count: 0 }
- if response_only_total_count?
- model_search_render_result_only_total_count(generic_objects[:total_count])
- elsif response_full?
- model_search_render_result_full(generic_objects)
- elsif response_expand?
- model_search_render_result_expand(generic_objects)
- elsif params[:label] || params[:term]
- model_search_render_result_label(object, generic_objects)
- else
- generic_objects_with_associations = generic_objects[:objects].map(&:attributes_with_association_ids)
- model_index_render_result(generic_objects_with_associations)
- end
- end
- def model_search_render_result_only_total_count(total)
- render json: {
- total_count: total,
- }, status: :ok
- end
- def model_search_render_result_full(generic_objects)
- assets = {}
- item_ids = []
- generic_objects[:objects].each do |item|
- item_ids.push item.id
- assets = item.assets(assets)
- end
- render json: {
- record_ids: item_ids,
- assets: assets,
- total_count: generic_objects[:total_count],
- }, status: :ok
- end
- def model_search_render_result_expand(generic_objects)
- list = generic_objects[:objects].map(&:attributes_with_association_names)
- render json: list, status: :ok
- end
- def model_search_render_result_label(object, generic_objects)
- rows = generic_objects[:objects].map do |row|
- realname = row.try(:fullname, recipient_line: true) || row.try(:fullname) || row.try(:name) || row.try(:id)
- value = row.try(:email) || realname
- if params[:term] && object.column_names.include?('active')
- { id: row.id, label: realname, value: value, inactive: !row.active }
- else
- { id: row.id, label: realname, value: realname }
- end
- end
- render json: rows
- end
- end
|