object_manager_attributes_controller.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 if attribute already exists
  21. exists = ObjectManager::Attribute.get(
  22. object: permitted_params[:object],
  23. name: permitted_params[:name],
  24. )
  25. raise Exceptions::UnprocessableEntity, 'already exists' if exists
  26. begin
  27. object_manager_attribute = ObjectManager::Attribute.add(
  28. object: permitted_params[:object],
  29. name: permitted_params[:name],
  30. display: permitted_params[:display],
  31. data_type: permitted_params[:data_type],
  32. data_option: permitted_params[:data_option],
  33. active: permitted_params[:active],
  34. screens: permitted_params[:screens],
  35. position: 1550,
  36. editable: true,
  37. )
  38. render json: object_manager_attribute.attributes_with_association_ids, status: :created
  39. rescue => e
  40. logger.error e
  41. raise Exceptions::UnprocessableEntity, e
  42. end
  43. end
  44. # PUT /object_manager_attributes/1
  45. def update
  46. object_manager_attribute = ObjectManager::Attribute.add(
  47. object: permitted_params[:object],
  48. name: permitted_params[:name],
  49. display: permitted_params[:display],
  50. data_type: permitted_params[:data_type],
  51. data_option: permitted_params[:data_option],
  52. active: permitted_params[:active],
  53. screens: permitted_params[:screens],
  54. position: 1550,
  55. editable: true,
  56. )
  57. render json: object_manager_attribute.attributes_with_association_ids, status: :ok
  58. rescue => e
  59. logger.error e
  60. raise Exceptions::UnprocessableEntity, e
  61. end
  62. # DELETE /object_manager_attributes/1
  63. def destroy
  64. object_manager_attribute = ObjectManager::Attribute.find(params[:id])
  65. ObjectManager::Attribute.remove(
  66. object_lookup_id: object_manager_attribute.object_lookup_id,
  67. name: object_manager_attribute.name,
  68. )
  69. model_destroy_render_item
  70. rescue => e
  71. logger.error e
  72. raise Exceptions::UnprocessableEntity, e
  73. end
  74. # POST /object_manager_attributes_discard_changes
  75. def discard_changes
  76. ObjectManager::Attribute.discard_changes
  77. render json: {}, status: :ok
  78. end
  79. # POST /object_manager_attributes_execute_migrations
  80. def execute_migrations
  81. ObjectManager::Attribute.migration_execute
  82. render json: {}, status: :ok
  83. end
  84. private
  85. def permitted_params
  86. @permitted_params ||= begin
  87. permitted = params.permit!.to_h
  88. if permitted[:data_type].match?(/^(boolean)$/)
  89. if permitted[:data_option][:options]
  90. # rubocop:disable Lint/BooleanSymbol
  91. if permitted[:data_option][:options][:false]
  92. permitted[:data_option][:options][false] = permitted[:data_option][:options].delete(:false)
  93. end
  94. if permitted[:data_option][:options][:true]
  95. permitted[:data_option][:options][true] = permitted[:data_option][:options].delete(:true)
  96. end
  97. if permitted[:data_option][:default] == 'true'
  98. permitted[:data_option][:default] = true
  99. elsif permitted[:data_option][:default] == 'false'
  100. permitted[:data_option][:default] = false
  101. end
  102. # rubocop:enable Lint/BooleanSymbol
  103. end
  104. end
  105. if permitted[:data_option]
  106. if !permitted[:data_option].key?(:default)
  107. permitted[:data_option][:default] = if permitted[:data_type].match?(/^(input|select|tree_select)$/)
  108. ''
  109. end
  110. end
  111. if permitted[:data_option][:null].nil?
  112. permitted[:data_option][:null] = true
  113. end
  114. if !permitted[:data_option][:options].is_a?(Hash) &&
  115. !permitted[:data_option][:options].is_a?(Array)
  116. permitted[:data_option][:options] = {}
  117. end
  118. if !permitted[:data_option][:relation].is_a?(String)
  119. permitted[:data_option][:relation] = ''
  120. end
  121. else
  122. permitted[:data_option] = {
  123. default: '',
  124. options: {},
  125. relation: '',
  126. null: true
  127. }
  128. end
  129. permitted
  130. end
  131. end
  132. end