refactor public_for_api acts_as_taggable scopes

This commit is contained in:
kikito
2017-06-13 16:14:24 +02:00
parent 92633195d9
commit a580e60786

View File

@@ -5,6 +5,15 @@ module ActsAsTaggableOn
after_create :increment_tag_custom_counter
after_destroy :touch_taggable, :decrement_tag_custom_counter
scope :public_for_api, -> do
where(%{taggings.tag_id in (?) and
(taggings.taggable_type = 'Debate' and taggings.taggable_id in (?)) or
(taggings.taggable_type = 'Proposal' and taggings.taggable_id in (?))},
Tag.where('kind IS NULL or kind = ?', 'category').pluck(:id),
Debate.public_for_api.pluck(:id),
Proposal.public_for_api.pluck(:id))
end
def touch_taggable
taggable.touch if taggable.present?
end
@@ -22,6 +31,12 @@ module ActsAsTaggableOn
include Graphqlable
scope :public_for_api, -> do
where('(tags.kind IS NULL or tags.kind = ?) and tags.id in (?)',
'category',
Tagging.public_for_api.pluck('DISTINCT taggings.tag_id'))
end
def increment_custom_counter_for(taggable_type)
Tag.increment_counter(custom_counter_field_name_for(taggable_type), id)
end
@@ -44,23 +59,6 @@ module ActsAsTaggableOn
ActsAsTaggableOn::Tag.where('taggings.taggable_type' => 'SpendingProposal').includes(:taggings).order(:name).uniq
end
scope :public_for_api, -> do
find_by_sql(%|
SELECT *
FROM tags
WHERE (tags.kind IS NULL OR tags.kind = 'category') AND tags.id IN (
SELECT tag_id
FROM (
SELECT COUNT(taggings.id) AS taggings_count, tag_id
FROM ((taggings FULL OUTER JOIN proposals ON taggable_type = 'Proposal' AND taggable_id = proposals.id) FULL OUTER JOIN debates ON taggable_type = 'Debate' AND taggable_id = debates.id)
WHERE (taggable_type = 'Proposal' AND proposals.hidden_at IS NULL) OR (taggable_type = 'Debate' AND debates.hidden_at IS NULL)
GROUP BY tag_id
) AS tag_taggings_count_relation
WHERE taggings_count > 0
)
|)
end
def self.graphql_field_name
:tag
end