diff --git a/app/models/tag_cloud.rb b/app/models/tag_cloud.rb index e4598971a..020f44e49 100644 --- a/app/models/tag_cloud.rb +++ b/app/models/tag_cloud.rb @@ -7,7 +7,26 @@ class TagCloud end 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 \ No newline at end of file diff --git a/spec/models/tag_cloud_spec.rb b/spec/models/tag_cloud_spec.rb new file mode 100644 index 000000000..bab902a18 --- /dev/null +++ b/spec/models/tag_cloud_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/support/common_actions.rb b/spec/support/common_actions.rb index 437a3c7ca..f9331d328 100644 --- a/spec/support/common_actions.rb +++ b/spec/support/common_actions.rb @@ -175,4 +175,8 @@ module CommonActions create(:debate, :with_confidence_score, cached_votes_up: 80)] end + def tag_names(tag_cloud) + tag_cloud.tags.map(&:name) + end + end