-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
acts_as_taggable_on.rb
72 lines (56 loc) · 1.7 KB
/
acts_as_taggable_on.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
65
66
67
68
69
70
71
72
module ActsAsTaggableOn
Tagging.class_eval do
after_create :increment_tag_custom_counter
after_destroy :touch_taggable, :decrement_tag_custom_counter
scope :public_for_api, -> do
where(
tag: Tag.where(kind: [nil, "category"]),
taggable: [Debate.public_for_api, Proposal.public_for_api]
)
end
def touch_taggable
taggable.touch if taggable.present?
end
def increment_tag_custom_counter
tag.increment_custom_counter_for(taggable_type)
end
def decrement_tag_custom_counter
tag.decrement_custom_counter_for(taggable_type)
end
end
Tag.class_eval do
scope :category, -> { where(kind: "category") }
def category?
kind == "category"
end
include Graphqlable
scope :public_for_api, -> do
where(
kind: [nil, "category"],
id: Tagging.public_for_api.distinct.pluck(:tag_id)
)
end
include PgSearch::Model
pg_search_scope :pg_search, against: :name,
using: {
tsearch: { prefix: true }
},
ignoring: :accents
def self.search(term)
pg_search(term)
end
def increment_custom_counter_for(taggable_type)
Tag.increment_counter(custom_counter_field_name_for(taggable_type), id)
end
def decrement_custom_counter_for(taggable_type)
Tag.decrement_counter(custom_counter_field_name_for(taggable_type), id)
end
def self.category_names
Tag.category.pluck(:name)
end
private
def custom_counter_field_name_for(taggable_type)
"#{taggable_type.tableize.tr("/", "_")}_count"
end
end
end