Improved public_for_api scopes and corresponding specs

This commit is contained in:
Alberto Miedes Garcés
2017-01-26 18:29:59 +01:00
parent 5b7cbccd19
commit 4b7cebf686
5 changed files with 98 additions and 5 deletions

View File

@@ -21,7 +21,7 @@ module ActsAsTaggableOn
Tag.class_eval do
include Graphqlable
def increment_custom_counter_for(taggable_type)
Tag.increment_counter(custom_counter_field_name_for(taggable_type), id)
end
@@ -45,7 +45,20 @@ module ActsAsTaggableOn
end
def self.public_for_api
where("kind IS NULL OR kind = 'category'")
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
private