object_manager_attributes_controller.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class ObjectManagerAttributesController < ApplicationController
  3. 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. begin
  28. object_manager_attribute = ObjectManager::Attribute.add(
  29. object: params[:object],
  30. name: params[:name],
  31. display: params[:display],
  32. data_type: params[:data_type],
  33. data_option: params[:data_option],
  34. active: params[:active],
  35. screens: params[:screens],
  36. position: 1550,
  37. editable: true,
  38. )
  39. render json: object_manager_attribute.attributes_with_association_ids, status: :created
  40. rescue => e
  41. raise Exceptions::UnprocessableEntity, e
  42. end
  43. end
  44. # PUT /object_manager_attributes/1
  45. def update
  46. check_params
  47. begin
  48. object_manager_attribute = ObjectManager::Attribute.add(
  49. object: params[:object],
  50. name: params[:name],
  51. display: params[:display],
  52. data_type: params[:data_type],
  53. data_option: params[:data_option],
  54. active: params[:active],
  55. screens: params[:screens],
  56. position: 1550,
  57. editable: true,
  58. )
  59. render json: object_manager_attribute.attributes_with_association_ids, status: :ok
  60. rescue => e
  61. raise Exceptions::UnprocessableEntity, e
  62. end
  63. end
  64. # DELETE /object_manager_attributes/1
  65. def destroy
  66. object_manager_attribute = ObjectManager::Attribute.find(params[:id])
  67. ObjectManager::Attribute.remove(
  68. object_lookup_id: object_manager_attribute.object_lookup_id,
  69. name: object_manager_attribute.name,
  70. )
  71. model_destroy_render_item
  72. end
  73. # POST /object_manager_attributes_discard_changes
  74. def discard_changes
  75. ObjectManager::Attribute.discard_changes
  76. render json: {}, status: :ok
  77. end
  78. # POST /object_manager_attributes_execute_migrations
  79. def execute_migrations
  80. ObjectManager::Attribute.migration_execute
  81. render json: {}, status: :ok
  82. end
  83. private
  84. def check_params
  85. if params[:data_type] =~ /^(boolean)$/
  86. if params[:data_option][:options]
  87. if params[:data_option][:options][:false]
  88. params[:data_option][:options][false] = params[:data_option][:options][:false]
  89. params[:data_option][:options].delete(:false)
  90. end
  91. if params[:data_option][:options][:true]
  92. params[:data_option][:options][true] = params[:data_option][:options][:true]
  93. params[:data_option][:options].delete(:true)
  94. end
  95. end
  96. end
  97. if params[:data_option] && !params[:data_option].key?(:default)
  98. params[:data_option][:default] = if params[:data_type] =~ /^(input|select)$/
  99. ''
  100. end
  101. end
  102. return if !params[:data_option][:null].nil?
  103. params[:data_option][:null] = true
  104. end
  105. end