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')