|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -# name: darren-test |
4 | | -# about: TODO |
5 | | -# meta_topic_id: TODO |
6 | | -# version: 0.0.1 |
7 | | -# authors: Discourse |
8 | | -# url: TODO |
9 | | -# required_version: 2.7.0 |
10 | | - |
11 | | -enabled_site_setting :darren_test_enabled |
12 | | - |
13 | | -module ::DarrenTest |
14 | | - PLUGIN_NAME = "darren-test" |
15 | | -end |
| 3 | +# name: community-custom-fields |
| 4 | +# about: Adds custom fields for using Discourse as a support platform |
| 5 | +# url: |
| 6 | +# version: 0.1 |
| 7 | +# authors: Retool |
16 | 8 |
|
17 | | -require_relative "lib/darren_test/engine" |
| 9 | +enabled_site_setting :community_custom_fields_enabled |
18 | 10 |
|
19 | 11 | after_initialize do |
20 | | - # Code which should run after Rails has finished booting |
| 12 | + module ::CommunityCustomFields |
| 13 | + class Engine < ::Rails::Engine |
| 14 | + engine_name "community_custom_fields" |
| 15 | + isolate_namespace CommunityCustomFields |
| 16 | + config.autoload_paths << File.join(config.root, "lib") |
| 17 | + scheduled_job_dir = "#{config.root}/app/jobs/scheduled" |
| 18 | + config.to_prepare do |
| 19 | + Rails.autoloaders.main.eager_load_dir(scheduled_job_dir) if Dir.exist?(scheduled_job_dir) |
| 20 | + end |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + ############################################################################# |
| 25 | + |
| 26 | + CommunityCustomFields::Engine.routes.draw do |
| 27 | + put '/:topic_id' => 'custom_fields#update' |
| 28 | + end |
| 29 | + |
| 30 | + Discourse::Application.routes.append do |
| 31 | + mount ::CommunityCustomFields::Engine, at: '/admin/plugins/community-custom-fields' |
| 32 | + end |
| 33 | + |
| 34 | + ############################################################################# |
| 35 | + |
| 36 | + require_dependency 'application_controller' |
| 37 | + class CommunityCustomFields::CustomFieldsController < ::ApplicationController |
| 38 | + before_action :ensure_logged_in |
| 39 | + before_action :ensure_admin |
| 40 | + |
| 41 | + def update |
| 42 | + topic = Topic.find(params[:topic_id]) |
| 43 | + custom = params[:custom_field] || params.permit(:assignee_id, :status) |
| 44 | + topic.custom_fields["assignee_id"] = custom[:assignee_id].to_i if custom[:assignee_id].present? |
| 45 | + topic.custom_fields["status"] = custom[:status] if custom[:status].present? |
| 46 | + if topic.save_custom_fields |
| 47 | + render json: success_json |
| 48 | + else |
| 49 | + Rails.logger.error("Failed to save custom fields for topic #{topic.id}: #{topic.errors.full_messages}") |
| 50 | + render json: { error: topic.errors.full_messages }, status: 422 |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + ############################################################################# |
| 56 | + |
| 57 | + Topic.register_custom_field_type('assignee_id', :integer) |
| 58 | + Topic.register_custom_field_type('status', :string) |
| 59 | + |
| 60 | + TopicList.preloaded_custom_fields << 'assignee_id' << 'status' |
| 61 | + |
| 62 | + add_to_serializer(:topic_view, :custom_fields) do |
| 63 | + object.topic.custom_fields.slice('assignee_id', 'status') |
| 64 | + end |
21 | 65 | end |
0 commit comments