2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-25 23:32:08 +08:00
|
|
|
class Admin::UserFieldsController < Admin::AdminController
|
2014-10-09 02:38:18 +08:00
|
|
|
def self.columns
|
2023-12-28 23:24:24 +08:00
|
|
|
columns = %i[
|
2021-04-27 13:52:45 +08:00
|
|
|
name
|
|
|
|
field_type
|
|
|
|
editable
|
|
|
|
description
|
2024-05-23 19:18:25 +08:00
|
|
|
requirement
|
2021-04-27 13:52:45 +08:00
|
|
|
show_on_profile
|
|
|
|
show_on_user_card
|
|
|
|
position
|
|
|
|
searchable
|
2023-01-09 20:20:10 +08:00
|
|
|
]
|
2023-12-28 23:24:24 +08:00
|
|
|
DiscoursePluginRegistry.apply_modifier(:admin_user_fields_columns, columns)
|
2014-10-09 02:38:18 +08:00
|
|
|
end
|
|
|
|
|
2014-09-25 23:32:08 +08:00
|
|
|
def create
|
2014-10-09 02:38:18 +08:00
|
|
|
field = UserField.new(params.require(:user_field).permit(*Admin::UserFieldsController.columns))
|
2015-07-31 02:52:53 +08:00
|
|
|
|
|
|
|
field.position = (UserField.maximum(:position) || 0) + 1
|
2015-08-18 01:01:15 +08:00
|
|
|
update_options(field)
|
2015-07-29 00:29:40 +08:00
|
|
|
|
2014-10-03 03:56:28 +08:00
|
|
|
json_result(field, serializer: UserFieldSerializer) { field.save }
|
2014-09-25 23:32:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def index
|
2015-07-31 02:52:53 +08:00
|
|
|
user_fields = UserField.all.includes(:user_field_options).order(:position)
|
2015-07-29 00:29:40 +08:00
|
|
|
render_serialized(user_fields, UserFieldSerializer, root: "user_fields")
|
2014-09-25 23:32:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2015-07-31 02:52:53 +08:00
|
|
|
field_params = params[:user_field]
|
2014-09-25 23:32:08 +08:00
|
|
|
field = UserField.where(id: params.require(:id)).first
|
2014-10-09 02:38:18 +08:00
|
|
|
|
|
|
|
Admin::UserFieldsController.columns.each do |col|
|
2019-05-07 09:27:05 +08:00
|
|
|
field.public_send("#{col}=", field_params[col]) unless field_params[col].nil?
|
2014-10-09 02:38:18 +08:00
|
|
|
end
|
2015-08-18 01:01:15 +08:00
|
|
|
update_options(field)
|
2014-09-25 23:32:08 +08:00
|
|
|
|
2015-07-31 02:52:53 +08:00
|
|
|
if field.save
|
2021-06-08 01:34:01 +08:00
|
|
|
if !field.show_on_profile && !field.show_on_user_card
|
|
|
|
DirectoryColumn.where(user_field_id: field.id).destroy_all
|
|
|
|
end
|
2015-07-31 02:52:53 +08:00
|
|
|
render_serialized(field, UserFieldSerializer, root: "user_field")
|
|
|
|
else
|
|
|
|
render_json_error(field)
|
2014-10-03 03:56:28 +08:00
|
|
|
end
|
2014-09-25 23:32:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
field = UserField.where(id: params.require(:id)).first
|
|
|
|
field.destroy if field.present?
|
2015-07-31 02:52:53 +08:00
|
|
|
render json: success_json
|
2014-09-25 23:32:08 +08:00
|
|
|
end
|
|
|
|
|
2015-07-29 00:29:40 +08:00
|
|
|
protected
|
|
|
|
|
2015-08-18 01:01:15 +08:00
|
|
|
def update_options(field)
|
2015-07-29 00:29:40 +08:00
|
|
|
options = params[:user_field][:options]
|
|
|
|
if options.present?
|
2015-08-18 01:01:15 +08:00
|
|
|
UserFieldOption.where(user_field_id: field.id).delete_all
|
2015-07-29 00:29:40 +08:00
|
|
|
field.user_field_options_attributes = options.map { |o| { value: o } }.uniq
|
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2014-09-25 23:32:08 +08:00
|
|
|
end
|