renders_models.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. module ApplicationController::RendersModels
  2. extend ActiveSupport::Concern
  3. private
  4. # model helper
  5. def model_create_render(object, params)
  6. clean_params = object.association_name_to_id_convert(params)
  7. clean_params = object.param_cleanup(clean_params, true)
  8. # create object
  9. generic_object = object.new(clean_params)
  10. # set relations
  11. generic_object.associations_from_param(params)
  12. # save object
  13. generic_object.save!
  14. if response_expand?
  15. render json: generic_object.attributes_with_association_names, status: :created
  16. return
  17. end
  18. if response_full?
  19. render json: generic_object.class.full(generic_object.id), status: :created
  20. return
  21. end
  22. model_create_render_item(generic_object)
  23. end
  24. def model_create_render_item(generic_object)
  25. render json: generic_object.attributes_with_association_ids, status: :created
  26. end
  27. def model_update_render(object, params)
  28. # find object
  29. generic_object = object.find(params[:id])
  30. clean_params = object.association_name_to_id_convert(params)
  31. clean_params = object.param_cleanup(clean_params, true)
  32. generic_object.with_lock do
  33. # set relations
  34. generic_object.associations_from_param(params)
  35. # set attributes
  36. generic_object.update!(clean_params)
  37. end
  38. if response_expand?
  39. render json: generic_object.attributes_with_association_names, status: :ok
  40. return
  41. end
  42. if response_full?
  43. render json: generic_object.class.full(generic_object.id), status: :ok
  44. return
  45. end
  46. model_update_render_item(generic_object)
  47. end
  48. def model_update_render_item(generic_object)
  49. render json: generic_object.attributes_with_association_ids, status: :ok
  50. end
  51. def model_destroy_render(object, params)
  52. generic_object = object.find(params[:id])
  53. generic_object.destroy!
  54. model_destroy_render_item()
  55. end
  56. def model_destroy_render_item ()
  57. render json: {}, status: :ok
  58. end
  59. def model_show_render(object, params)
  60. if response_expand?
  61. generic_object = object.find(params[:id])
  62. render json: generic_object.attributes_with_association_names, status: :ok
  63. return
  64. end
  65. if response_full?
  66. render json: object.full(params[:id]), status: :ok
  67. return
  68. end
  69. model_show_render_item(object.find(params[:id]))
  70. end
  71. def model_show_render_item(generic_object)
  72. render json: generic_object.attributes_with_association_ids, status: :ok
  73. end
  74. def model_index_render(object, params)
  75. offset = 0
  76. per_page = 500
  77. if params[:page] && params[:per_page]
  78. offset = (params[:page].to_i - 1) * params[:per_page].to_i
  79. limit = params[:per_page].to_i
  80. end
  81. if per_page > 500
  82. per_page = 500
  83. end
  84. generic_objects = if offset.positive?
  85. object.limit(params[:per_page]).order(id: 'ASC').offset(offset).limit(limit)
  86. else
  87. object.all.order(id: 'ASC').offset(offset).limit(limit)
  88. end
  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. }, status: :ok
  108. return
  109. end
  110. generic_objects_with_associations = []
  111. generic_objects.each do |item|
  112. generic_objects_with_associations.push item.attributes_with_association_ids
  113. end
  114. model_index_render_result(generic_objects_with_associations)
  115. end
  116. def model_index_render_result(generic_objects)
  117. render json: generic_objects, status: :ok
  118. end
  119. def model_references_check(object, params)
  120. generic_object = object.find(params[:id])
  121. result = Models.references(object, generic_object.id)
  122. return false if result.blank?
  123. raise Exceptions::UnprocessableEntity, 'Can\'t delete, object has references.'
  124. rescue => e
  125. raise Exceptions::UnprocessableEntity, e
  126. end
  127. end