object_manager_attributes_controller.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class ObjectManagerAttributesController < ApplicationController
  3. prepend_before_action { authentication_check && authorize! }
  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. add_attribute_using_params(permitted_params.merge(position: 1550), status: :created)
  27. end
  28. # PUT /object_manager_attributes/1
  29. def update
  30. # check if attribute already exists
  31. exists = ObjectManager::Attribute.get(
  32. object: permitted_params[:object],
  33. name: permitted_params[:name],
  34. )
  35. raise Exceptions::UnprocessableEntity, 'does not exist' if !exists
  36. add_attribute_using_params(permitted_params, status: :ok)
  37. end
  38. # DELETE /object_manager_attributes/1
  39. def destroy
  40. object_manager_attribute = ObjectManager::Attribute.find(params[:id])
  41. ObjectManager::Attribute.remove(
  42. object_lookup_id: object_manager_attribute.object_lookup_id,
  43. name: object_manager_attribute.name,
  44. )
  45. model_destroy_render_item
  46. rescue => e
  47. logger.error e
  48. raise Exceptions::UnprocessableEntity, e
  49. end
  50. # POST /object_manager_attributes_discard_changes
  51. def discard_changes
  52. ObjectManager::Attribute.discard_changes
  53. render json: {}, status: :ok
  54. end
  55. # POST /object_manager_attributes_execute_migrations
  56. def execute_migrations
  57. ObjectManager::Attribute.migration_execute
  58. render json: {}, status: :ok
  59. end
  60. private
  61. def permitted_params
  62. @permitted_params ||= begin
  63. permitted = params.permit!.to_h
  64. if permitted[:data_type].match?(/^(boolean)$/)
  65. if permitted[:data_option][:options]
  66. # rubocop:disable Lint/BooleanSymbol
  67. if permitted[:data_option][:options][:false]
  68. permitted[:data_option][:options][false] = permitted[:data_option][:options].delete(:false)
  69. end
  70. if permitted[:data_option][:options][:true]
  71. permitted[:data_option][:options][true] = permitted[:data_option][:options].delete(:true)
  72. end
  73. case permitted[:data_option][:default]
  74. when 'true'
  75. permitted[:data_option][:default] = true
  76. when 'false'
  77. permitted[:data_option][:default] = false
  78. end
  79. # rubocop:enable Lint/BooleanSymbol
  80. end
  81. end
  82. if permitted[:data_option]
  83. if !permitted[:data_option].key?(:default)
  84. permitted[:data_option][:default] = if permitted[:data_type].match?(/^(input|select|tree_select)$/)
  85. ''
  86. end
  87. end
  88. if permitted[:data_option][:null].nil?
  89. permitted[:data_option][:null] = true
  90. end
  91. if !permitted[:data_option][:options].is_a?(Hash) &&
  92. !permitted[:data_option][:options].is_a?(Array)
  93. permitted[:data_option][:options] = {}
  94. end
  95. if !permitted[:data_option][:relation].is_a?(String)
  96. permitted[:data_option][:relation] = ''
  97. end
  98. else
  99. permitted[:data_option] = {
  100. default: '',
  101. options: {},
  102. relation: '',
  103. null: true
  104. }
  105. end
  106. permitted
  107. end
  108. end
  109. def add_attribute_using_params(given_params, status:)
  110. attributes = given_params.slice(:object, :name, :display, :data_type, :data_option, :active, :screens, :position)
  111. attributes[:editable] = true
  112. object_manager_attribute = ObjectManager::Attribute.add(attributes)
  113. render json: object_manager_attribute.attributes_with_association_ids, status: status
  114. rescue => e
  115. logger.error e
  116. raise Exceptions::UnprocessableEntity, e
  117. end
  118. end