renders_models.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module ApplicationController::RendersModels
  3. extend ActiveSupport::Concern
  4. include CanPaginate
  5. private
  6. # model helper
  7. def model_create_render(object, params)
  8. clean_params = object.association_name_to_id_convert(params)
  9. clean_params = object.param_cleanup(clean_params, true)
  10. if object.included_modules.include?(ChecksCoreWorkflow)
  11. clean_params[:screen] = 'create'
  12. end
  13. # create object
  14. generic_object = object.new(clean_params)
  15. # set relations
  16. generic_object.associations_from_param(params)
  17. # save object
  18. generic_object.save!
  19. if response_expand?
  20. render json: generic_object.attributes_with_association_names, status: :created
  21. return
  22. end
  23. if response_full?
  24. render json: generic_object.class.full(generic_object.id), status: :created
  25. return
  26. end
  27. model_create_render_item(generic_object)
  28. end
  29. def model_create_render_item(generic_object)
  30. render json: generic_object.attributes_with_association_ids, status: :created
  31. end
  32. def model_update_render(object, params)
  33. # find object
  34. generic_object = object.find(params[:id])
  35. clean_params = object.association_name_to_id_convert(params)
  36. clean_params = object.param_cleanup(clean_params, true)
  37. if object.included_modules.include?(ChecksCoreWorkflow)
  38. clean_params[:screen] = 'edit'
  39. end
  40. generic_object.with_lock do
  41. # set relations
  42. generic_object.associations_from_param(params)
  43. # set attributes
  44. generic_object.update!(clean_params)
  45. end
  46. if response_expand?
  47. render json: generic_object.attributes_with_association_names, status: :ok
  48. return
  49. end
  50. if response_full?
  51. render json: generic_object.class.full(generic_object.id), status: :ok
  52. return
  53. end
  54. model_update_render_item(generic_object)
  55. end
  56. def model_update_render_item(generic_object)
  57. render json: generic_object.attributes_with_association_ids, status: :ok
  58. end
  59. def model_destroy_render(object, params)
  60. generic_object = object.find(params[:id])
  61. generic_object.destroy!
  62. model_destroy_render_item
  63. end
  64. def model_destroy_render_item()
  65. render json: {}, status: :ok
  66. end
  67. def model_show_render(object, params)
  68. if response_expand?
  69. generic_object = object.find(params[:id])
  70. render json: generic_object.attributes_with_association_names, status: :ok
  71. return
  72. end
  73. if response_full?
  74. render json: object.full(params[:id]), status: :ok
  75. return
  76. end
  77. model_show_render_item(object.find(params[:id]))
  78. end
  79. def model_show_render_item(generic_object)
  80. render json: generic_object.attributes_with_association_ids, status: :ok
  81. end
  82. def model_index_render(object, params)
  83. paginate_with(default: 500)
  84. sql_helper = ::SqlHelper.new(object: object)
  85. sort_by = sql_helper.get_sort_by(params, 'id')
  86. order_by = sql_helper.get_order_by(params, 'ASC')
  87. order_sql = sql_helper.get_order(sort_by, order_by)
  88. generic_objects = object.reorder(Arel.sql(order_sql)).offset(pagination.offset).limit(pagination.limit)
  89. if response_expand?
  90. list = generic_objects.map(&:attributes_with_association_names)
  91. render json: list, status: :ok
  92. return
  93. end
  94. if response_full?
  95. assets = {}
  96. item_ids = []
  97. generic_objects.each do |item|
  98. item_ids.push item.id
  99. assets = item.assets(assets)
  100. end
  101. render json: {
  102. record_ids: item_ids,
  103. assets: assets,
  104. total_count: object.count
  105. }, status: :ok
  106. return
  107. end
  108. generic_objects_with_associations = generic_objects.map(&:attributes_with_association_ids)
  109. model_index_render_result(generic_objects_with_associations)
  110. end
  111. def model_index_render_result(generic_objects)
  112. render json: generic_objects, status: :ok
  113. end
  114. def model_references_check(object, params)
  115. generic_object = object.find(params[:id])
  116. result = Models.references(object, generic_object.id)
  117. return false if result.blank?
  118. raise Exceptions::UnprocessableEntity, __('Can\'t delete, object has references.')
  119. rescue => e
  120. raise Exceptions::UnprocessableEntity, e
  121. end
  122. def model_search_render(object, params)
  123. paginate_with(max: 200, default: 50)
  124. generic_objects = object.search(
  125. query: params[:query] || params[:term],
  126. condition: params[:condition],
  127. ids: params[:ids],
  128. role_ids: params[:role_ids],
  129. group_ids: params[:group_ids],
  130. permissions: params[:permissions],
  131. only_total_count: response_only_total_count?,
  132. sort_by: params[:sort_by],
  133. order_by: params[:order_by],
  134. offset: pagination.offset,
  135. limit: pagination.limit,
  136. current_user: current_user,
  137. full: true,
  138. with_total_count: true,
  139. ) || { objects: [], total_count: 0 }
  140. if response_only_total_count?
  141. model_search_render_result_only_total_count(generic_objects[:total_count])
  142. elsif response_full?
  143. model_search_render_result_full(generic_objects)
  144. elsif response_expand?
  145. model_search_render_result_expand(generic_objects)
  146. elsif params[:label] || params[:term]
  147. model_search_render_result_label(object, generic_objects)
  148. else
  149. generic_objects_with_associations = generic_objects[:objects].map(&:attributes_with_association_ids)
  150. model_index_render_result(generic_objects_with_associations)
  151. end
  152. end
  153. def model_search_render_result_only_total_count(total)
  154. render json: {
  155. total_count: total,
  156. }, status: :ok
  157. end
  158. def model_search_render_result_full(generic_objects)
  159. assets = {}
  160. item_ids = []
  161. generic_objects[:objects].each do |item|
  162. item_ids.push item.id
  163. assets = item.assets(assets)
  164. end
  165. render json: {
  166. record_ids: item_ids,
  167. assets: assets,
  168. total_count: generic_objects[:total_count],
  169. }, status: :ok
  170. end
  171. def model_search_render_result_expand(generic_objects)
  172. list = generic_objects[:objects].map(&:attributes_with_association_names)
  173. render json: list, status: :ok
  174. end
  175. def model_search_render_result_label(object, generic_objects)
  176. rows = generic_objects[:objects].map do |row|
  177. realname = row.try(:fullname, recipient_line: true) || row.try(:fullname) || row.try(:name) || row.try(:id)
  178. value = row.try(:email) || realname
  179. if params[:term] && object.column_names.include?('active')
  180. { id: row.id, label: realname, value: value, inactive: !row.active }
  181. else
  182. { id: row.id, label: realname, value: realname }
  183. end
  184. end
  185. render json: rows
  186. end
  187. end