object_manager_attributes_controller.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class ObjectManagerAttributesController < ApplicationController
  3. prepend_before_action { authentication_check(permission: 'admin.object') }
  4. # GET /object_manager_attributes_list
  5. def list
  6. render json: {
  7. objects: ObjectManager.list_frontend_objects,
  8. }
  9. end
  10. # GET /object_manager_attributes
  11. def index
  12. render json: ObjectManager::Attribute.list_full
  13. end
  14. # GET /object_manager_attributes/1
  15. def show
  16. model_show_render(ObjectManager::Attribute, params)
  17. end
  18. # POST /object_manager_attributes
  19. def create
  20. check_params
  21. # check if attribute already exists
  22. exists = ObjectManager::Attribute.get(
  23. object: params[:object],
  24. name: params[:name],
  25. )
  26. raise Exceptions::UnprocessableEntity, 'already exists' if exists
  27. local_params = params.permit!.to_h
  28. begin
  29. object_manager_attribute = ObjectManager::Attribute.add(
  30. object: local_params[:object],
  31. name: local_params[:name],
  32. display: local_params[:display],
  33. data_type: local_params[:data_type],
  34. data_option: local_params[:data_option],
  35. active: local_params[:active],
  36. screens: local_params[:screens],
  37. position: 1550,
  38. editable: true,
  39. )
  40. render json: object_manager_attribute.attributes_with_association_ids, status: :created
  41. rescue => e
  42. raise Exceptions::UnprocessableEntity, e
  43. end
  44. end
  45. # PUT /object_manager_attributes/1
  46. def update
  47. check_params
  48. local_params = params.permit!.to_h
  49. begin
  50. object_manager_attribute = ObjectManager::Attribute.add(
  51. object: local_params[:object],
  52. name: local_params[:name],
  53. display: local_params[:display],
  54. data_type: local_params[:data_type],
  55. data_option: local_params[:data_option],
  56. active: local_params[:active],
  57. screens: local_params[:screens],
  58. position: 1550,
  59. editable: true,
  60. )
  61. render json: object_manager_attribute.attributes_with_association_ids, status: :ok
  62. rescue => e
  63. raise Exceptions::UnprocessableEntity, e
  64. end
  65. end
  66. # DELETE /object_manager_attributes/1
  67. def destroy
  68. object_manager_attribute = ObjectManager::Attribute.find(params[:id])
  69. ObjectManager::Attribute.remove(
  70. object_lookup_id: object_manager_attribute.object_lookup_id,
  71. name: object_manager_attribute.name,
  72. )
  73. model_destroy_render_item
  74. end
  75. # POST /object_manager_attributes_discard_changes
  76. def discard_changes
  77. ObjectManager::Attribute.discard_changes
  78. render json: {}, status: :ok
  79. end
  80. # POST /object_manager_attributes_execute_migrations
  81. def execute_migrations
  82. ObjectManager::Attribute.migration_execute
  83. render json: {}, status: :ok
  84. end
  85. private
  86. def check_params
  87. if params[:data_type] =~ /^(boolean)$/
  88. if params[:data_option][:options]
  89. if params[:data_option][:options][:false]
  90. params[:data_option][:options][false] = params[:data_option][:options][:false]
  91. params[:data_option][:options].delete(:false)
  92. end
  93. if params[:data_option][:options][:true]
  94. params[:data_option][:options][true] = params[:data_option][:options][:true]
  95. params[:data_option][:options].delete(:true)
  96. end
  97. end
  98. end
  99. if params[:data_option] && !params[:data_option].key?(:default)
  100. params[:data_option][:default] = if params[:data_type] =~ /^(input|select|tree_select)$/
  101. ''
  102. end
  103. end
  104. return if !params[:data_option][:null].nil?
  105. params[:data_option][:null] = true
  106. end
  107. end