From 6fde504e67ce3af553ba0d30ce8a92a299b9d6ed Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 19 Feb 2016 18:15:18 +0100 Subject: [PATCH] displays summary for districts --- app/models/geozone.rb | 4 ++++ app/models/proposal.rb | 7 +++++-- spec/features/proposals_spec.rb | 21 +++++++++++++++++++ spec/models/proposal_spec.rb | 37 ++++++++++++++++++++++++++------- 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/app/models/geozone.rb b/app/models/geozone.rb index 303671493..a38de3613 100644 --- a/app/models/geozone.rb +++ b/app/models/geozone.rb @@ -1,3 +1,7 @@ class Geozone < ActiveRecord::Base validates :name, presence: true + + def self.names + Geozone.all.map(&:name) + end end diff --git a/app/models/proposal.rb b/app/models/proposal.rb index b8d3a76be..41ff57d10 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -72,8 +72,11 @@ class Proposal < ActiveRecord::Base def self.for_summary summary = {} categories = ActsAsTaggableOn::Tag.category_names.sort - categories.each do |category| - summary[category] = search(category).last_week.sort_by_confidence_score.limit(3) + geozones = Geozone.names.sort + + groups = categories + geozones + groups.each do |group| + summary[group] = search(group).last_week.sort_by_confidence_score.limit(3) end summary end diff --git a/spec/features/proposals_spec.rb b/spec/features/proposals_spec.rb index 8af646c09..ad1449a24 100644 --- a/spec/features/proposals_spec.rb +++ b/spec/features/proposals_spec.rb @@ -1188,6 +1188,27 @@ feature 'Proposals' do end end + scenario "Displays proposals grouped by district" do + california = create(:geozone, name: 'California') + new_york = create(:geozone, name: 'New York') + + 3.times { create(:proposal, geozone: california) } + 3.times { create(:proposal, geozone: new_york) } + + visit proposals_path + click_link "The most supported proposals by category" + + within("#california") do + expect(page).to have_content("California") + expect(page).to have_css(".proposal", count: 3) + end + + within("#new-york") do + expect(page).to have_content("New York") + expect(page).to have_css(".proposal", count: 3) + end + end + scenario "Displays a maximum of 3 proposals per category" do create(:tag, kind: 'category', name: 'culture') 4.times { create(:proposal, tag_list: 'culture') } diff --git a/spec/models/proposal_spec.rb b/spec/models/proposal_spec.rb index 134f1fbaa..8138580a9 100644 --- a/spec/models/proposal_spec.rb +++ b/spec/models/proposal_spec.rb @@ -643,18 +643,39 @@ describe Proposal do end describe "for_summary" do - it "should return proposals tagged with a category" do - create(:tag, kind: 'category', name: 'culture') - proposal = create(:proposal, tag_list: 'culture') - expect(Proposal.for_summary.values.flatten).to include(proposal) + context "categories" do + + it "should return proposals tagged with a category" do + create(:tag, kind: 'category', name: 'culture') + proposal = create(:proposal, tag_list: 'culture') + + expect(Proposal.for_summary.values.flatten).to include(proposal) + end + + it "should not return proposals tagged without a category" do + create(:tag, kind: 'category', name: 'culture') + proposal = create(:proposal, tag_list: 'parks') + + expect(Proposal.for_summary.values.flatten).to_not include(proposal) + end end - it "should not return proposals tagged without a category" do - create(:tag, kind: 'category', name: 'culture') - proposal = create(:proposal, tag_list: 'parks') + context "districts" do - expect(Proposal.for_summary.values.flatten).to_not include(proposal) + it "should return proposals with a geozone" do + california = create(:geozone, name: 'california') + proposal = create(:proposal, geozone: california) + + expect(Proposal.for_summary.values.flatten).to include(proposal) + end + + it "should not return proposals without a geozone" do + create(:geozone, name: 'california') + proposal = create(:proposal) + + expect(Proposal.for_summary.values.flatten).to_not include(proposal) + end end it "should return proposals created this week" do