2014-09-25 10:32:08 -05:00
|
|
|
class Admin::UserFieldsController < Admin::AdminController
|
|
|
|
|
2014-10-08 13:38:18 -05:00
|
|
|
def self.columns
|
2015-07-30 13:52:53 -05:00
|
|
|
[:name, :field_type, :editable, :description, :required, :show_on_profile, :position]
|
2014-10-08 13:38:18 -05:00
|
|
|
end
|
|
|
|
|
2014-09-25 10:32:08 -05:00
|
|
|
def create
|
2014-10-08 13:38:18 -05:00
|
|
|
field = UserField.new(params.require(:user_field).permit(*Admin::UserFieldsController.columns))
|
2015-07-30 13:52:53 -05:00
|
|
|
|
|
|
|
field.position = (UserField.maximum(:position) || 0) + 1
|
2014-10-14 16:21:34 -05:00
|
|
|
field.required = params[:required] == "true"
|
2015-08-17 12:01:15 -05:00
|
|
|
update_options(field)
|
2015-07-28 11:29:40 -05:00
|
|
|
|
2014-10-02 14:56:28 -05:00
|
|
|
json_result(field, serializer: UserFieldSerializer) do
|
|
|
|
field.save
|
|
|
|
end
|
2014-09-25 10:32:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def index
|
2015-07-30 13:52:53 -05:00
|
|
|
user_fields = UserField.all.includes(:user_field_options).order(:position)
|
2015-07-28 11:29:40 -05:00
|
|
|
render_serialized(user_fields, UserFieldSerializer, root: 'user_fields')
|
2014-09-25 10:32:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2015-07-30 13:52:53 -05:00
|
|
|
field_params = params[:user_field]
|
2014-09-25 10:32:08 -05:00
|
|
|
field = UserField.where(id: params.require(:id)).first
|
2014-10-08 13:38:18 -05:00
|
|
|
|
|
|
|
Admin::UserFieldsController.columns.each do |col|
|
2015-07-30 13:52:53 -05:00
|
|
|
unless field_params[col].nil?
|
|
|
|
field.send("#{col}=", field_params[col])
|
|
|
|
end
|
2014-10-08 13:38:18 -05:00
|
|
|
end
|
2015-08-17 12:01:15 -05:00
|
|
|
update_options(field)
|
2014-09-25 10:32:08 -05:00
|
|
|
|
2015-07-30 13:52:53 -05:00
|
|
|
if field.save
|
|
|
|
render_serialized(field, UserFieldSerializer, root: 'user_field')
|
|
|
|
else
|
|
|
|
render_json_error(field)
|
2014-10-02 14:56:28 -05:00
|
|
|
end
|
2014-09-25 10:32:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
field = UserField.where(id: params.require(:id)).first
|
|
|
|
field.destroy if field.present?
|
2015-07-30 13:52:53 -05:00
|
|
|
render json: success_json
|
2014-09-25 10:32:08 -05:00
|
|
|
end
|
|
|
|
|
2015-07-28 11:29:40 -05:00
|
|
|
protected
|
|
|
|
|
2015-08-17 12:01:15 -05:00
|
|
|
def update_options(field)
|
2015-07-28 11:29:40 -05:00
|
|
|
options = params[:user_field][:options]
|
|
|
|
if options.present?
|
2015-08-17 12:01:15 -05:00
|
|
|
UserFieldOption.where(user_field_id: field.id).delete_all
|
2015-07-28 11:29:40 -05:00
|
|
|
field.user_field_options_attributes = options.map {|o| {value: o} }.uniq
|
|
|
|
end
|
|
|
|
end
|
2014-09-25 10:32:08 -05:00
|
|
|
end
|
|
|
|
|