discourse/app/controllers/directory_columns_controller.rb
Mark VanLandingham 0c42a29dc4
DEV: Plugin API to allow creation of directory columns with item query (#13402)
The first thing we needed here was an enum rather than a boolean to determine how a directory_column was created. Now we have `automatic`, `user_field` and `plugin` directory columns.

This plugin API is assuming that the plugin has added a migration to a column to the `directory_items` table.

This was created to be initially used by discourse-solved. PR with API usage - https://github.com/discourse/discourse-solved/pull/137/
2021-06-17 09:06:18 -05:00

63 lines
2.0 KiB
Ruby

# frozen_string_literal: true
class DirectoryColumnsController < ApplicationController
requires_login
def index
raise Discourse::NotFound unless guardian.is_staff?
ensure_user_fields_have_columns
columns = DirectoryColumn.includes(:user_field).all
render_json_dump(directory_columns: serialize_data(columns, DirectoryColumnSerializer))
end
def update
raise Discourse::NotFound unless guardian.is_staff?
params.require(:directory_columns)
directory_column_params = params.permit(directory_columns: {})
directory_columns = DirectoryColumn.all
has_enabled_column = directory_column_params[:directory_columns].values.any? do |column_data|
column_data[:enabled].to_s == "true"
end
raise Discourse::InvalidParameters, "Must have at least one column enabled" unless has_enabled_column
directory_column_params[:directory_columns].values.each do |column_data|
existing_column = directory_columns.detect { |c| c.id == column_data[:id].to_i }
if (existing_column.enabled != column_data[:enabled] || existing_column.position != column_data[:position].to_i)
existing_column.update(enabled: column_data[:enabled], position: column_data[:position])
end
end
render json: success_json
end
private
def ensure_user_fields_have_columns
user_fields_without_column =
UserField.left_outer_joins(:directory_column)
.where(directory_column: { user_field_id: nil })
.where("show_on_profile=? OR show_on_user_card=?", true, true)
return unless user_fields_without_column.count > 0
next_position = DirectoryColumn.maximum("position") + 1
new_directory_column_attrs = []
user_fields_without_column.each do |user_field|
new_directory_column_attrs.push({
user_field_id: user_field.id,
enabled: false,
type: DirectoryColumn.types[:user_field],
position: next_position
})
next_position += 1
end
DirectoryColumn.insert_all(new_directory_column_attrs)
end
end