renders_models.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # Copyright (C) 2012-2023 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.order(Arel.sql(order_sql)).offset(pagination.offset).limit(pagination.limit)
  89. if response_expand?
  90. list = []
  91. generic_objects.each do |generic_object|
  92. list.push generic_object.attributes_with_association_names
  93. end
  94. render json: list, status: :ok
  95. return
  96. end
  97. if response_full?
  98. assets = {}
  99. item_ids = []
  100. generic_objects.each do |item|
  101. item_ids.push item.id
  102. assets = item.assets(assets)
  103. end
  104. render json: {
  105. record_ids: item_ids,
  106. assets: assets,
  107. total_count: object.count
  108. }, status: :ok
  109. return
  110. end
  111. generic_objects_with_associations = []
  112. generic_objects.each do |item|
  113. generic_objects_with_associations.push item.attributes_with_association_ids
  114. end
  115. model_index_render_result(generic_objects_with_associations)
  116. end
  117. def model_index_render_result(generic_objects)
  118. render json: generic_objects, status: :ok
  119. end
  120. def model_references_check(object, params)
  121. generic_object = object.find(params[:id])
  122. result = Models.references(object, generic_object.id)
  123. return false if result.blank?
  124. raise Exceptions::UnprocessableEntity, __('Can\'t delete, object has references.')
  125. rescue => e
  126. raise Exceptions::UnprocessableEntity, e
  127. end
  128. end