renders_models.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. end