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
|
2017-05-23 09:26:46 +00:00
|
|
|
prepend_before_action { authentication_check(permission: 'admin.object') }
|
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-19 08:20:38 +00:00
|
|
|
check_params
|
2016-05-23 22:48:32 +00:00
|
|
|
|
|
|
|
# check if attribute already exists
|
|
|
|
exists = ObjectManager::Attribute.get(
|
2016-05-19 08:20:38 +00:00
|
|
|
object: params[:object],
|
|
|
|
name: params[:name],
|
|
|
|
)
|
2016-06-30 08:24:03 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, 'already exists' if exists
|
2016-05-23 22:48:32 +00:00
|
|
|
|
2017-09-08 08:28:34 +00:00
|
|
|
local_params = params.permit!.to_h
|
2016-05-23 22:48:32 +00:00
|
|
|
begin
|
|
|
|
object_manager_attribute = ObjectManager::Attribute.add(
|
2017-09-08 08:28:34 +00:00
|
|
|
object: local_params[:object],
|
|
|
|
name: local_params[:name],
|
|
|
|
display: local_params[:display],
|
|
|
|
data_type: local_params[:data_type],
|
|
|
|
data_option: local_params[:data_option],
|
|
|
|
active: local_params[:active],
|
|
|
|
screens: local_params[:screens],
|
2016-05-23 22:48:32 +00:00
|
|
|
position: 1550,
|
|
|
|
editable: true,
|
|
|
|
)
|
2017-01-31 17:13:45 +00:00
|
|
|
render json: object_manager_attribute.attributes_with_association_ids, status: :created
|
2016-05-23 22:48:32 +00:00
|
|
|
rescue => e
|
2016-06-30 08:24:03 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, e
|
2016-05-23 22:48:32 +00:00
|
|
|
end
|
2014-11-02 19:44:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# PUT /object_manager_attributes/1
|
|
|
|
def update
|
2016-05-19 08:20:38 +00:00
|
|
|
check_params
|
2017-09-08 08:28:34 +00:00
|
|
|
|
|
|
|
local_params = params.permit!.to_h
|
2016-05-23 22:48:32 +00:00
|
|
|
begin
|
|
|
|
object_manager_attribute = ObjectManager::Attribute.add(
|
2017-09-08 08:28:34 +00:00
|
|
|
object: local_params[:object],
|
|
|
|
name: local_params[:name],
|
|
|
|
display: local_params[:display],
|
|
|
|
data_type: local_params[:data_type],
|
|
|
|
data_option: local_params[:data_option],
|
|
|
|
active: local_params[:active],
|
|
|
|
screens: local_params[:screens],
|
2016-05-23 22:48:32 +00:00
|
|
|
position: 1550,
|
|
|
|
editable: true,
|
|
|
|
)
|
2017-01-31 17:13:45 +00:00
|
|
|
render json: object_manager_attribute.attributes_with_association_ids, status: :ok
|
2016-05-23 22:48:32 +00:00
|
|
|
rescue => e
|
2016-06-30 08:24:03 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, e
|
2016-05-23 22:48:32 +00:00
|
|
|
end
|
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,
|
|
|
|
name: object_manager_attribute.name,
|
|
|
|
)
|
2016-11-30 10:30:03 +00:00
|
|
|
model_destroy_render_item
|
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
|
|
|
|
|
|
|
|
def check_params
|
2017-11-23 08:09:44 +00:00
|
|
|
if params[:data_type].match?(/^(boolean)$/)
|
2016-05-28 22:04:26 +00:00
|
|
|
if params[:data_option][:options]
|
2017-11-23 08:09:44 +00:00
|
|
|
# rubocop:disable Lint/BooleanSymbol
|
2016-05-28 22:04:26 +00:00
|
|
|
if params[:data_option][:options][:false]
|
2017-11-23 08:09:44 +00:00
|
|
|
params[:data_option][:options][false] = params[:data_option][:options].delete(:false)
|
2016-05-28 22:04:26 +00:00
|
|
|
end
|
|
|
|
if params[:data_option][:options][:true]
|
2017-11-23 08:09:44 +00:00
|
|
|
params[:data_option][:options][true] = params[:data_option][:options].delete(:true)
|
2016-05-28 22:04:26 +00:00
|
|
|
end
|
2017-11-07 10:40:58 +00:00
|
|
|
if params[:data_option][:default] == 'true'
|
2017-11-07 10:01:12 +00:00
|
|
|
params[:data_option][:default] = true
|
|
|
|
elsif params[:data_option][:default] == 'false'
|
|
|
|
params[:data_option][:default] = false
|
|
|
|
end
|
2017-11-23 08:09:44 +00:00
|
|
|
# rubocop:enable Lint/BooleanSymbol
|
2016-05-28 22:04:26 +00:00
|
|
|
end
|
|
|
|
end
|
2018-01-17 14:24:17 +00:00
|
|
|
|
|
|
|
if params[:data_option]
|
|
|
|
|
|
|
|
if !params[:data_option].key?(:default)
|
|
|
|
params[:data_option][:default] = if params[:data_type].match?(/^(input|select|tree_select)$/)
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if params[:data_option][:null].nil?
|
|
|
|
params[:data_option][:null] = true
|
|
|
|
end
|
|
|
|
if params[:data_option][:options].nil?
|
|
|
|
params[:data_option][:options] = ''
|
|
|
|
end
|
|
|
|
if params[:data_option][:relation].nil?
|
|
|
|
params[:data_option][:relation] = ''
|
|
|
|
end
|
|
|
|
else
|
|
|
|
params[:data_option] = {
|
|
|
|
default: '',
|
|
|
|
options: '',
|
|
|
|
relation: '',
|
|
|
|
null: true
|
|
|
|
}
|
2016-05-26 08:14:51 +00:00
|
|
|
end
|
2018-01-25 08:33:30 +00:00
|
|
|
|
2014-11-02 19:44:15 +00:00
|
|
|
end
|
|
|
|
end
|