-
Notifications
You must be signed in to change notification settings - Fork 14
/
plugin.rb
64 lines (52 loc) · 2.47 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# name: discourse-tickets
# about: Tickets system for Discourse
# version: 0.2
# authors: Pavilion
# url: https://github.com/paviliondev/discourse-tickets
# contact_emails: development@pavilion.tech
enabled_site_setting :tickets_enabled
register_asset 'stylesheets/tickets.scss'
load File.expand_path('../lib/tickets/validator.rb', __FILE__)
after_initialize do
load File.expand_path('../lib/tickets/engine.rb', __FILE__)
load File.expand_path("../lib/tickets/guardian.rb", __FILE__)
load File.expand_path('../lib/tickets/routes.rb', __FILE__)
load File.expand_path("../lib/tickets/topic.rb", __FILE__)
load File.expand_path("../lib/tickets/ticket.rb", __FILE__)
load File.expand_path("../lib/tickets/tag.rb", __FILE__)
load File.expand_path("../controllers/tickets/tickets_controller.rb", __FILE__)
load File.expand_path("../serializers/tickets/ticket_serializer.rb", __FILE__)
register_seedfu_fixtures(Rails.root.join("plugins", "discourse-tickets", "db", "fixtures").to_s)
add_class_method(:site, :ticket_tags) do
Tag.joins('JOIN tag_group_memberships ON tags.id = tag_group_memberships.tag_id')
.joins('JOIN tag_groups ON tag_group_memberships.tag_group_id = tag_groups.id')
.where('tag_groups.name in (?)', Tickets::Tag::GROUPS)
.group('tag_groups.name, tags.name', 'tag_group_memberships.created_at')
.order('tag_group_memberships.created_at')
.pluck('tag_groups.name, tags.name')
.each_with_object({}) do |arr, result|
type = arr[0].split("_").last
result[type] = [] if result[type].blank?
result[type].push(arr[1])
end
end
module DiscourseTaggingExtension
def filter_allowed_tags(guardian, opts = {})
result = super(guardian, opts)
if opts[:for_input]
ticket_tag_ids = Tag.joins('JOIN tag_group_memberships ON tags.id = tag_group_memberships.tag_id')
.joins('JOIN tag_groups ON tag_group_memberships.tag_group_id = tag_groups.id')
.where('tag_groups.name in (?)', Tickets::Tag::GROUPS).pluck(:id)
tags_with_counts = opts[:with_context] ? result.first : result
tags_with_counts = tags_with_counts.select { |tag| ticket_tag_ids.exclude? tag.id }
result = opts[:with_context] ? [tags_with_counts, result.second] : tags_with_counts
end
result
end
end
require_dependency 'discourse_tagging'
class << DiscourseTagging
prepend DiscourseTaggingExtension
end
add_to_serializer(:site, :ticket_tags) { ::Site.ticket_tags }
end