2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2014-11-02 19:44:15 +00:00
|
|
|
|
|
|
|
class ObjectManagerAttributesController < ApplicationController
|
2020-03-19 09:39:51 +00:00
|
|
|
prepend_before_action { authentication_check && authorize! }
|
2014-11-02 19:44:15 +00:00
|
|
|
|
|
|
|
# GET /object_manager_attributes_list
|
|
|
|
def list
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
2015-05-05 10:51:19 +00:00
|
|
|
objects: ObjectManager.list_frontend_objects,
|
2014-11-02 19:44:15 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET /object_manager_attributes
|
|
|
|
def index
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: ObjectManager::Attribute.list_full
|
2014-11-02 19:44:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /object_manager_attributes/1
|
|
|
|
def show
|
|
|
|
model_show_render(ObjectManager::Attribute, params)
|
|
|
|
end
|
|
|
|
|
|
|
|
# POST /object_manager_attributes
|
|
|
|
def create
|
2016-05-23 22:48:32 +00:00
|
|
|
# check if attribute already exists
|
|
|
|
exists = ObjectManager::Attribute.get(
|
2018-03-23 14:26:09 +00:00
|
|
|
object: permitted_params[:object],
|
2018-12-19 17:31:51 +00:00
|
|
|
name: permitted_params[:name],
|
2016-05-19 08:20:38 +00:00
|
|
|
)
|
2016-06-30 08:24:03 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, 'already exists' if exists
|
2016-05-23 22:48:32 +00:00
|
|
|
|
2020-05-08 09:19:54 +00:00
|
|
|
add_attribute_using_params(permitted_params.merge(position: 1550), status: :created)
|
2014-11-02 19:44:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# PUT /object_manager_attributes/1
|
|
|
|
def update
|
2020-05-08 09:19:54 +00:00
|
|
|
# check if attribute already exists
|
|
|
|
exists = ObjectManager::Attribute.get(
|
|
|
|
object: permitted_params[:object],
|
|
|
|
name: permitted_params[:name],
|
2018-03-23 14:26:09 +00:00
|
|
|
)
|
|
|
|
|
2020-05-08 09:19:54 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, 'does not exist' if !exists
|
|
|
|
|
|
|
|
add_attribute_using_params(permitted_params, status: :ok)
|
2014-11-02 19:44:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# DELETE /object_manager_attributes/1
|
|
|
|
def destroy
|
2016-05-19 08:20:38 +00:00
|
|
|
object_manager_attribute = ObjectManager::Attribute.find(params[:id])
|
|
|
|
ObjectManager::Attribute.remove(
|
|
|
|
object_lookup_id: object_manager_attribute.object_lookup_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
name: object_manager_attribute.name,
|
2016-05-19 08:20:38 +00:00
|
|
|
)
|
2016-11-30 10:30:03 +00:00
|
|
|
model_destroy_render_item
|
2018-06-20 12:13:35 +00:00
|
|
|
rescue => e
|
|
|
|
logger.error e
|
|
|
|
raise Exceptions::UnprocessableEntity, e
|
2016-05-19 08:20:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# POST /object_manager_attributes_discard_changes
|
|
|
|
def discard_changes
|
|
|
|
ObjectManager::Attribute.discard_changes
|
|
|
|
render json: {}, status: :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
# POST /object_manager_attributes_execute_migrations
|
|
|
|
def execute_migrations
|
|
|
|
ObjectManager::Attribute.migration_execute
|
|
|
|
render json: {}, status: :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-03-23 14:26:09 +00:00
|
|
|
def permitted_params
|
|
|
|
@permitted_params ||= begin
|
|
|
|
permitted = params.permit!.to_h
|
|
|
|
|
2021-05-12 11:37:44 +00:00
|
|
|
if permitted[:data_type].match?(%r{^(boolean)$}) && permitted[:data_option][:options]
|
2020-09-30 09:07:01 +00:00
|
|
|
# rubocop:disable Lint/BooleanSymbol
|
|
|
|
if permitted[:data_option][:options][:false]
|
|
|
|
permitted[:data_option][:options][false] = permitted[:data_option][:options].delete(:false)
|
2016-05-28 22:04:26 +00:00
|
|
|
end
|
2020-09-30 09:07:01 +00:00
|
|
|
|
|
|
|
if permitted[:data_option][:options][:true]
|
|
|
|
permitted[:data_option][:options][true] = permitted[:data_option][:options].delete(:true)
|
|
|
|
end
|
|
|
|
|
|
|
|
case permitted[:data_option][:default]
|
|
|
|
when 'true'
|
|
|
|
permitted[:data_option][:default] = true
|
|
|
|
when 'false'
|
|
|
|
permitted[:data_option][:default] = false
|
|
|
|
end
|
|
|
|
# rubocop:enable Lint/BooleanSymbol
|
2018-03-23 14:26:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if permitted[:data_option]
|
|
|
|
|
|
|
|
if !permitted[:data_option].key?(:default)
|
2021-05-12 11:37:44 +00:00
|
|
|
permitted[:data_option][:default] = if permitted[:data_type].match?(%r{^(input|select|tree_select)$})
|
2018-03-23 14:26:09 +00:00
|
|
|
''
|
|
|
|
end
|
2016-05-28 22:04:26 +00:00
|
|
|
end
|
2018-03-23 14:26:09 +00:00
|
|
|
|
|
|
|
if permitted[:data_option][:null].nil?
|
|
|
|
permitted[:data_option][:null] = true
|
2017-11-07 10:01:12 +00:00
|
|
|
end
|
2018-01-17 14:24:17 +00:00
|
|
|
|
2018-03-29 11:46:15 +00:00
|
|
|
if !permitted[:data_option][:options].is_a?(Hash) &&
|
|
|
|
!permitted[:data_option][:options].is_a?(Array)
|
2018-03-23 14:26:09 +00:00
|
|
|
permitted[:data_option][:options] = {}
|
|
|
|
end
|
2018-01-17 14:24:17 +00:00
|
|
|
|
2018-03-23 14:26:09 +00:00
|
|
|
if !permitted[:data_option][:relation].is_a?(String)
|
|
|
|
permitted[:data_option][:relation] = ''
|
|
|
|
end
|
|
|
|
else
|
|
|
|
permitted[:data_option] = {
|
|
|
|
default: '',
|
|
|
|
options: {},
|
|
|
|
relation: '',
|
|
|
|
null: true
|
|
|
|
}
|
2018-01-17 14:24:17 +00:00
|
|
|
end
|
|
|
|
|
2018-03-23 14:26:09 +00:00
|
|
|
permitted
|
2016-05-26 08:14:51 +00:00
|
|
|
end
|
2014-11-02 19:44:15 +00:00
|
|
|
end
|
2020-05-08 09:19:54 +00:00
|
|
|
|
|
|
|
def add_attribute_using_params(given_params, status:)
|
|
|
|
attributes = given_params.slice(:object, :name, :display, :data_type, :data_option, :active, :screens, :position)
|
|
|
|
attributes[:editable] = true
|
|
|
|
|
|
|
|
object_manager_attribute = ObjectManager::Attribute.add(attributes)
|
|
|
|
|
|
|
|
render json: object_manager_attribute.attributes_with_association_ids, status: status
|
|
|
|
rescue => e
|
|
|
|
logger.error e
|
|
|
|
raise Exceptions::UnprocessableEntity, e
|
|
|
|
end
|
2014-11-02 19:44:15 +00:00
|
|
|
end
|