discourse/app/controllers/admin/user_fields_controller.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
1.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class Admin::UserFieldsController < Admin::AdminController
def self.columns
%i(name field_type editable description required show_on_profile show_on_user_card position searchable)
end
def create
field = UserField.new(params.require(:user_field).permit(*Admin::UserFieldsController.columns))
2015-07-30 14:52:53 -04:00
field.position = (UserField.maximum(:position) || 0) + 1
field.required = params[:user_field][:required] == "true"
update_options(field)
json_result(field, serializer: UserFieldSerializer) do
field.save
end
end
def index
2015-07-30 14:52:53 -04:00
user_fields = UserField.all.includes(:user_field_options).order(:position)
render_serialized(user_fields, UserFieldSerializer, root: 'user_fields')
end
def update
2015-07-30 14:52:53 -04:00
field_params = params[:user_field]
field = UserField.where(id: params.require(:id)).first
Admin::UserFieldsController.columns.each do |col|
2015-07-30 14:52:53 -04:00
unless field_params[col].nil?
2019-05-06 21:27:05 -04:00
field.public_send("#{col}=", field_params[col])
2015-07-30 14:52:53 -04:00
end
end
update_options(field)
2015-07-30 14:52:53 -04:00
if field.save
if !field.show_on_profile && !field.show_on_user_card
DirectoryColumn.where(user_field_id: field.id).destroy_all
end
2015-07-30 14:52:53 -04:00
render_serialized(field, UserFieldSerializer, root: 'user_field')
else
render_json_error(field)
end
end
def destroy
field = UserField.where(id: params.require(:id)).first
field.destroy if field.present?
2015-07-30 14:52:53 -04:00
render json: success_json
end
protected
def update_options(field)
options = params[:user_field][:options]
if options.present?
UserFieldOption.where(user_field_id: field.id).delete_all
field.user_field_options_attributes = options.map { |o| { value: o } }.uniq
end
2018-06-07 01:28:18 -04:00
end
end