does not display category tags nor geozone names

This commit is contained in:
rgarcia
2016-01-27 13:48:43 +01:00
parent e7e5c531a0
commit 0175cd2c68
3 changed files with 105 additions and 1 deletions

View File

@@ -7,7 +7,26 @@ class TagCloud
end end
def tags def tags
resource_model.last_week.tag_counts.order("#{resource_model.to_s.downcase.pluralize}_count": :desc, name: :asc).limit(5) resource_model.last_week.tag_counts.
where("lower(name) NOT IN (?)", category_names + geozone_names + default_blacklist)
order("#{table_name}_count": :desc, name: :asc).
limit(5)
end
def category_names
ActsAsTaggableOn::Tag.where("kind = 'category'").map {|tag| tag.name.downcase }
end
def geozone_names
Geozone.all.map {|geozone| geozone.name.downcase }
end
def default_blacklist
['']
end
def table_name
resource_model.to_s.downcase.pluralize
end end
end end

View File

@@ -0,0 +1,81 @@
require 'rails_helper'
describe TagCloud do
describe "#tags" do
it "returns proposal tags" do
create(:proposal, tag_list: 'participation')
create(:debate, tag_list: 'world hunger')
tag_cloud = TagCloud.new(Proposal)
expect(tag_names(tag_cloud)).to contain_exactly('participation')
end
it "returns debate tags" do
create(:proposal, tag_list: 'participation')
create(:debate, tag_list: 'world hunger')
tag_cloud = TagCloud.new(Debate)
expect(tag_names(tag_cloud)).to contain_exactly('world hunger')
end
it "returns tags from last week" do
create(:proposal, tag_list: 'participation', created_at: 1.day.ago)
create(:proposal, tag_list: 'corruption', created_at: 2.weeks.ago)
tag_cloud = TagCloud.new(Proposal)
expect(tag_names(tag_cloud)).to contain_exactly('participation')
end
it "does not return category tags" do
create(:tag, kind: 'category', name: 'Education')
create(:tag, kind: 'category', name: 'Participation')
create(:proposal, tag_list: 'education, parks')
create(:proposal, tag_list: 'participation, water')
tag_cloud = TagCloud.new(Proposal)
expect(tag_names(tag_cloud)).to contain_exactly('parks', 'water')
end
it "does not return geozone names" do
create(:geozone, name: 'California')
create(:geozone, name: 'New York')
create(:proposal, tag_list: 'parks, California')
create(:proposal, tag_list: 'water, New York')
tag_cloud = TagCloud.new(Proposal)
expect(tag_names(tag_cloud)).to contain_exactly('parks', 'water')
end
it "orders tags by count" do
3.times { create(:proposal, tag_list: 'participation') }
create(:proposal, tag_list: 'corruption')
tag_cloud = TagCloud.new(Proposal)
expect(tag_names(tag_cloud).first).to eq 'participation'
expect(tag_names(tag_cloud).second).to eq 'corruption'
end
it "orders tags by count and then by name" do
3.times { create(:proposal, tag_list: 'participation') }
3.times { create(:proposal, tag_list: 'health') }
create(:proposal, tag_list: 'corruption')
tag_cloud = TagCloud.new(Proposal)
expect(tag_names(tag_cloud).first).to eq 'health'
expect(tag_names(tag_cloud).second).to eq 'participation'
expect(tag_names(tag_cloud).third).to eq 'corruption'
end
end
end

View File

@@ -175,4 +175,8 @@ module CommonActions
create(:debate, :with_confidence_score, cached_votes_up: 80)] create(:debate, :with_confidence_score, cached_votes_up: 80)]
end end
def tag_names(tag_cloud)
tag_cloud.tags.map(&:name)
end
end end