From e7e5c531a0babdcc7630606fdb1aabfa3867f5e0 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 27 Jan 2016 13:02:18 +0100 Subject: [PATCH 1/8] moves tag_cloud logic to a model --- app/controllers/concerns/commentable_actions.rb | 2 +- app/models/tag_cloud.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 app/models/tag_cloud.rb diff --git a/app/controllers/concerns/commentable_actions.rb b/app/controllers/concerns/commentable_actions.rb index e46ba7da4..3c62facd5 100644 --- a/app/controllers/concerns/commentable_actions.rb +++ b/app/controllers/concerns/commentable_actions.rb @@ -73,7 +73,7 @@ module CommentableActions end def tag_cloud - resource_model.last_week.tag_counts.order("#{resource_name.pluralize}_count": :desc, name: :asc).limit(5) + TagCloud.new(resource_model).tags end def load_geozones diff --git a/app/models/tag_cloud.rb b/app/models/tag_cloud.rb new file mode 100644 index 000000000..e4598971a --- /dev/null +++ b/app/models/tag_cloud.rb @@ -0,0 +1,13 @@ +class TagCloud + + attr_accessor :resource_model + + def initialize(resource_model) + @resource_model = resource_model + end + + def tags + resource_model.last_week.tag_counts.order("#{resource_model.to_s.downcase.pluralize}_count": :desc, name: :asc).limit(5) + end + +end \ No newline at end of file From 0175cd2c6817ae701e6c45f8ffc8c444726f4f1d Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 27 Jan 2016 13:48:43 +0100 Subject: [PATCH 2/8] does not display category tags nor geozone names --- app/models/tag_cloud.rb | 21 ++++++++- spec/models/tag_cloud_spec.rb | 81 ++++++++++++++++++++++++++++++++++ spec/support/common_actions.rb | 4 ++ 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 spec/models/tag_cloud_spec.rb 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 From 5d4b6fae92a3beb76d686c2a7d25ddaf9a2a19be Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 27 Jan 2016 14:15:12 +0100 Subject: [PATCH 3/8] scopes tags by category or geozone --- .../concerns/commentable_actions.rb | 2 +- app/models/tag_cloud.rb | 14 +++-- app/views/shared/_tag_cloud.html.erb | 2 +- spec/features/tags_spec.rb | 60 +++++++++++++++++++ spec/models/tag_cloud_spec.rb | 24 ++++++++ 5 files changed, 96 insertions(+), 6 deletions(-) diff --git a/app/controllers/concerns/commentable_actions.rb b/app/controllers/concerns/commentable_actions.rb index 3c62facd5..de03f4efb 100644 --- a/app/controllers/concerns/commentable_actions.rb +++ b/app/controllers/concerns/commentable_actions.rb @@ -73,7 +73,7 @@ module CommentableActions end def tag_cloud - TagCloud.new(resource_model).tags + TagCloud.new(resource_model, params[:search]).tags end def load_geozones diff --git a/app/models/tag_cloud.rb b/app/models/tag_cloud.rb index 020f44e49..fdb4c5f6c 100644 --- a/app/models/tag_cloud.rb +++ b/app/models/tag_cloud.rb @@ -1,14 +1,16 @@ class TagCloud - attr_accessor :resource_model + attr_accessor :resource_model, :scope - def initialize(resource_model) + def initialize(resource_model, scope=nil) @resource_model = resource_model + @scope = scope end def tags - resource_model.last_week.tag_counts. - where("lower(name) NOT IN (?)", category_names + geozone_names + default_blacklist) + resource_model_scoped. + 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 @@ -21,6 +23,10 @@ class TagCloud Geozone.all.map {|geozone| geozone.name.downcase } end + def resource_model_scoped + scope ? resource_model.search(scope) : resource_model + end + def default_blacklist [''] end diff --git a/app/views/shared/_tag_cloud.html.erb b/app/views/shared/_tag_cloud.html.erb index 06bf85c2b..1091ef6da 100644 --- a/app/views/shared/_tag_cloud.html.erb +++ b/app/views/shared/_tag_cloud.html.erb @@ -5,7 +5,7 @@ <% tag_cloud @tag_cloud, %w[s m l] do |tag, css_class| %> <%= link_to taggable_path(taggable, tag.name), class: css_class do %> - <%= tag.name %> + <%= tag.name %> (<%= tag.send(taggable_counter_field(taggable)) %>) <% end %> <% end %> diff --git a/spec/features/tags_spec.rb b/spec/features/tags_spec.rb index 3f57c6b91..2bd8db060 100644 --- a/spec/features/tags_spec.rb +++ b/spec/features/tags_spec.rb @@ -141,4 +141,64 @@ feature 'Tags' do expect(page).to_not have_content 'Economía' end + context 'Tag cloud' do + + scenario 'Proposals' do + earth = create(:proposal, tag_list: 'Medio Ambiente') + money = create(:proposal, tag_list: 'Economía') + + visit proposals_path + + within "#tag-cloud" do + expect(page).to have_content "Medio Ambiente" + expect(page).to have_content "Economía" + end + end + + scenario 'Debates' do + earth = create(:debate, tag_list: 'Medio Ambiente') + money = create(:debate, tag_list: 'Economía') + + visit debates_path + + within "#tag-cloud" do + expect(page).to have_content "Medio Ambiente" + expect(page).to have_content "Economía" + end + end + + scenario "scoped by category" do + create(:tag, kind: 'category', name: 'Medio Ambiente') + create(:tag, kind: 'category', name: 'Economía') + + earth = create(:proposal, tag_list: 'Medio Ambiente, Agua') + money = create(:proposal, tag_list: 'Economía, Corrupción') + + visit proposals_path(search: 'Economía') + + within "#tag-cloud" do + expect(page).to have_css(".tag", count: 1) + expect(page).to have_content "Corrupción" + expect(page).to_not have_content "Economía" + end + end + + scenario "scoped by district" do + create(:geozone, name: 'Madrid') + create(:geozone, name: 'Barcelona') + + earth = create(:proposal, tag_list: 'Madrid, Agua') + money = create(:proposal, tag_list: 'Barcelona, Playa') + + visit proposals_path(search: 'Barcelona') + + within "#tag-cloud" do + expect(page).to have_css(".tag", count: 1) + expect(page).to have_content "Playa" + expect(page).to_not have_content "Agua" + end + end + + end + end diff --git a/spec/models/tag_cloud_spec.rb b/spec/models/tag_cloud_spec.rb index bab902a18..7f3c0c85d 100644 --- a/spec/models/tag_cloud_spec.rb +++ b/spec/models/tag_cloud_spec.rb @@ -55,6 +55,30 @@ describe TagCloud do expect(tag_names(tag_cloud)).to contain_exactly('parks', 'water') end + it "returns tags scoped by category" 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, 'Education') + + expect(tag_names(tag_cloud)).to contain_exactly('parks') + end + + it "returns tags scoped by geozone" 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, 'California') + + expect(tag_names(tag_cloud)).to contain_exactly('parks') + end + it "orders tags by count" do 3.times { create(:proposal, tag_list: 'participation') } create(:proposal, tag_list: 'corruption') From 209b6407d3825255a62180010776f83a3b7aee81 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 27 Jan 2016 14:17:32 +0100 Subject: [PATCH 4/8] increases tag limit to 10 --- app/models/tag_cloud.rb | 2 +- spec/models/tag_cloud_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/models/tag_cloud.rb b/app/models/tag_cloud.rb index fdb4c5f6c..a8f53548a 100644 --- a/app/models/tag_cloud.rb +++ b/app/models/tag_cloud.rb @@ -12,7 +12,7 @@ class TagCloud last_week.tag_counts. where("lower(name) NOT IN (?)", category_names + geozone_names + default_blacklist). order("#{table_name}_count": :desc, name: :asc). - limit(5) + limit(10) end def category_names diff --git a/spec/models/tag_cloud_spec.rb b/spec/models/tag_cloud_spec.rb index 7f3c0c85d..5fb8fbb42 100644 --- a/spec/models/tag_cloud_spec.rb +++ b/spec/models/tag_cloud_spec.rb @@ -100,6 +100,14 @@ describe TagCloud do expect(tag_names(tag_cloud).second).to eq 'participation' expect(tag_names(tag_cloud).third).to eq 'corruption' end + + it "returns a maximum of 10 tags" do + 12.times { |i| create(:proposal, tag_list: "Tag #{i}") } + + tag_cloud = TagCloud.new(Proposal) + + expect(tag_names(tag_cloud).count).to eq(10) + end end end \ No newline at end of file From 5a3dc42dea3f5d4b05eb88632f09cf707c97e70f Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 27 Jan 2016 14:18:40 +0100 Subject: [PATCH 5/8] removes tag count --- app/views/shared/_tag_cloud.html.erb | 1 - spec/features/tags_spec.rb | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/app/views/shared/_tag_cloud.html.erb b/app/views/shared/_tag_cloud.html.erb index 1091ef6da..34e90c714 100644 --- a/app/views/shared/_tag_cloud.html.erb +++ b/app/views/shared/_tag_cloud.html.erb @@ -6,7 +6,6 @@ <% tag_cloud @tag_cloud, %w[s m l] do |tag, css_class| %> <%= link_to taggable_path(taggable, tag.name), class: css_class do %> <%= tag.name %> - (<%= tag.send(taggable_counter_field(taggable)) %>) <% end %> <% end %> diff --git a/spec/features/tags_spec.rb b/spec/features/tags_spec.rb index 2bd8db060..a6ecc739f 100644 --- a/spec/features/tags_spec.rb +++ b/spec/features/tags_spec.rb @@ -63,10 +63,10 @@ feature 'Tags' do visit debates_path within(:css, "#tag-cloud") do - expect(page.find("a:eq(1)")).to have_content("Economía (10)") - expect(page.find("a:eq(2)")).to have_content("Corrupción (5)") - expect(page.find("a:eq(3)")).to have_content("Educación (5)") - expect(page.find("a:eq(4)")).to have_content("Medio Ambiente (1)") + expect(page.find("a:eq(1)")).to have_content("Economía") + expect(page.find("a:eq(2)")).to have_content("Corrupción") + expect(page.find("a:eq(3)")).to have_content("Educación") + expect(page.find("a:eq(4)")).to have_content("Medio Ambiente") end end From eed36234668c7d55954f56a4b4ab3f660e3b1c23 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 27 Jan 2016 14:19:21 +0100 Subject: [PATCH 6/8] users full text search of tag --- app/helpers/tags_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/helpers/tags_helper.rb b/app/helpers/tags_helper.rb index 59bc56bce..f6363a864 100644 --- a/app/helpers/tags_helper.rb +++ b/app/helpers/tags_helper.rb @@ -3,9 +3,9 @@ module TagsHelper def taggable_path(taggable_type, tag_name) case taggable_type when 'debate' - debates_path(tag: tag_name) + debates_path(search: tag_name) when 'proposal' - proposals_path(tag: tag_name) + proposals_path(search: tag_name) else '#' end From 48b5aba1c550e40c54373711ed434b0d7da38f22 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 27 Jan 2016 14:33:38 +0100 Subject: [PATCH 7/8] removes unused helper --- .../concerns/commentable_actions.rb | 2 +- app/helpers/tags_helper.rb | 14 -------- app/views/shared/_tag_cloud.html.erb | 4 +-- spec/features/tags_spec.rb | 33 ++++++++++--------- 4 files changed, 20 insertions(+), 33 deletions(-) diff --git a/app/controllers/concerns/commentable_actions.rb b/app/controllers/concerns/commentable_actions.rb index de03f4efb..286d2a297 100644 --- a/app/controllers/concerns/commentable_actions.rb +++ b/app/controllers/concerns/commentable_actions.rb @@ -73,7 +73,7 @@ module CommentableActions end def tag_cloud - TagCloud.new(resource_model, params[:search]).tags + TagCloud.new(resource_model, params[:search]) end def load_geozones diff --git a/app/helpers/tags_helper.rb b/app/helpers/tags_helper.rb index f6363a864..da7824622 100644 --- a/app/helpers/tags_helper.rb +++ b/app/helpers/tags_helper.rb @@ -11,18 +11,4 @@ module TagsHelper end end - def taggable_counter_field(taggable_type) - "#{taggable_type.underscore.pluralize}_count" - end - - def tag_cloud(tags, classes, counter_field = :taggings_count) - return [] if tags.empty? - - max_count = tags.sort_by(&counter_field).last.send(counter_field).to_f - - tags.each do |tag| - index = ((tag.send(counter_field) / max_count) * (classes.size - 1)) - yield tag, classes[index.nan? ? 0 : index.round] - end - end end diff --git a/app/views/shared/_tag_cloud.html.erb b/app/views/shared/_tag_cloud.html.erb index 34e90c714..6b27de955 100644 --- a/app/views/shared/_tag_cloud.html.erb +++ b/app/views/shared/_tag_cloud.html.erb @@ -3,8 +3,8 @@
- <% tag_cloud @tag_cloud, %w[s m l] do |tag, css_class| %> - <%= link_to taggable_path(taggable, tag.name), class: css_class do %> + <% @tag_cloud.tags.each do |tag| %> + <%= link_to taggable_path(taggable, tag.name) do %> <%= tag.name %> <% end %> <% end %> diff --git a/spec/features/tags_spec.rb b/spec/features/tags_spec.rb index a6ecc739f..0b95e1de4 100644 --- a/spec/features/tags_spec.rb +++ b/spec/features/tags_spec.rb @@ -54,22 +54,6 @@ feature 'Tags' do expect(page).to have_content "Hacienda" end - scenario 'Tag Cloud' do - 1.times { create(:debate, tag_list: 'Medio Ambiente') } - 5.times { create(:debate, tag_list: 'Corrupción') } - 5.times { create(:debate, tag_list: 'Educación') } - 10.times { create(:debate, tag_list: 'Economía') } - - visit debates_path - - within(:css, "#tag-cloud") do - expect(page.find("a:eq(1)")).to have_content("Economía") - expect(page.find("a:eq(2)")).to have_content("Corrupción") - expect(page.find("a:eq(3)")).to have_content("Educación") - expect(page.find("a:eq(4)")).to have_content("Medio Ambiente") - end - end - scenario 'Create' do user = create(:user) login_as(user) @@ -199,6 +183,23 @@ feature 'Tags' do end end + scenario "tag links" do + proposal1 = create(:proposal, tag_list: 'Medio Ambiente') + proposal2 = create(:proposal, tag_list: 'Medio Ambiente') + proposal3 = create(:proposal, tag_list: 'Economía') + + visit proposals_path + + within "#tag-cloud" do + click_link "Medio Ambiente" + end + + expect(page).to have_css ".proposal", count: 2 + expect(page).to have_content proposal1.title + expect(page).to have_content proposal2.title + expect(page).to_not have_content proposal3.title + end + end end From 5bea145d712bd5c91bf011388d8af4723bc09020 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 27 Jan 2016 15:11:59 +0100 Subject: [PATCH 8/8] scopes tags only for Proposals --- app/models/tag_cloud.rb | 2 +- spec/models/tag_cloud_spec.rb | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/models/tag_cloud.rb b/app/models/tag_cloud.rb index a8f53548a..086d88c48 100644 --- a/app/models/tag_cloud.rb +++ b/app/models/tag_cloud.rb @@ -24,7 +24,7 @@ class TagCloud end def resource_model_scoped - scope ? resource_model.search(scope) : resource_model + scope && resource_model == Proposal ? resource_model.search(scope) : resource_model end def default_blacklist diff --git a/spec/models/tag_cloud_spec.rb b/spec/models/tag_cloud_spec.rb index 5fb8fbb42..d040c0527 100644 --- a/spec/models/tag_cloud_spec.rb +++ b/spec/models/tag_cloud_spec.rb @@ -79,6 +79,9 @@ describe TagCloud do expect(tag_names(tag_cloud)).to contain_exactly('parks') end + xit "returns tags scoped by category for debates" + xit "returns tags scoped by geozone for debates" + it "orders tags by count" do 3.times { create(:proposal, tag_list: 'participation') } create(:proposal, tag_list: 'corruption')