diff --git a/app/models/geozone.rb b/app/models/geozone.rb
index 303671493..ee612f355 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.pluck(:name)
+ end
end
diff --git a/app/models/proposal.rb b/app/models/proposal.rb
index 3bd1d9a72..41ff57d10 100644
--- a/app/models/proposal.rb
+++ b/app/models/proposal.rb
@@ -71,9 +71,12 @@ class Proposal < ActiveRecord::Base
def self.for_summary
summary = {}
- categories = ActsAsTaggableOn::Tag.category_names
- categories.each do |category|
- summary[category] = search(category).last_week.sort_by_confidence_score.limit(3)
+ categories = ActsAsTaggableOn::Tag.category_names.sort
+ 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/app/models/tag_cloud.rb b/app/models/tag_cloud.rb
index 54c745ce8..f3ea655f0 100644
--- a/app/models/tag_cloud.rb
+++ b/app/models/tag_cloud.rb
@@ -16,7 +16,7 @@ class TagCloud
end
def category_names
- ActsAsTaggableOn::Tag.category_names
+ ActsAsTaggableOn::Tag.category_names.map(&:downcase)
end
def geozone_names
diff --git a/app/views/proposals/summary.html.erb b/app/views/proposals/summary.html.erb
index 679d821a8..c576a827a 100644
--- a/app/views/proposals/summary.html.erb
+++ b/app/views/proposals/summary.html.erb
@@ -9,7 +9,7 @@
<% @proposals.each do |group_name, proposals| %>
-
<%= group_name.capitalize %>
+
<%= group_name %>
<% proposals[0..2].each do |proposal| %>
diff --git a/config/initializers/acts_as_taggable_on.rb b/config/initializers/acts_as_taggable_on.rb
index c9e38683c..40a0a5949 100644
--- a/config/initializers/acts_as_taggable_on.rb
+++ b/config/initializers/acts_as_taggable_on.rb
@@ -35,7 +35,7 @@ module ActsAsTaggableOn
end
def self.category_names
- Tag.where("kind = 'category'").map {|tag| tag.name.downcase }
+ Tag.where("kind = 'category'").pluck(:name)
end
private
diff --git a/spec/features/proposals_spec.rb b/spec/features/proposals_spec.rb
index 8af646c09..06aa49101 100644
--- a/spec/features/proposals_spec.rb
+++ b/spec/features/proposals_spec.rb
@@ -1166,11 +1166,11 @@ feature 'Proposals' do
context "Summary" do
scenario "Displays proposals grouped by category" do
- create(:tag, kind: 'category', name: 'culture')
- create(:tag, kind: 'category', name: 'social services')
+ create(:tag, kind: 'category', name: 'Culture')
+ create(:tag, kind: 'category', name: 'Social Services')
- 3.times { create(:proposal, tag_list: 'culture') }
- 3.times { create(:proposal, tag_list: 'social services') }
+ 3.times { create(:proposal, tag_list: 'Culture') }
+ 3.times { create(:proposal, tag_list: 'Social Services') }
create(:proposal, tag_list: 'Random')
@@ -1183,7 +1183,28 @@ feature 'Proposals' do
end
within("#social-services") do
- expect(page).to have_content("Social services")
+ expect(page).to have_content("Social Services")
+ expect(page).to have_css(".proposal", count: 3)
+ 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
diff --git a/spec/models/proposal_spec.rb b/spec/models/proposal_spec.rb
index b96246fdc..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
@@ -669,7 +690,7 @@ describe Proposal do
expect(Proposal.for_summary.values.flatten).to_not include(proposal)
end
- it "should order by votes" do
+ it "should order proposals by votes" do
create(:tag, kind: 'category', name: 'culture')
create(:proposal, tag_list: 'culture').update_column(:confidence_score, 2)
create(:proposal, tag_list: 'culture').update_column(:confidence_score, 10)
@@ -682,6 +703,22 @@ describe Proposal do
expect(results.third.confidence_score).to be(2)
end
+ it "should order groups alphabetically" do
+ create(:tag, kind: 'category', name: 'health')
+ create(:tag, kind: 'category', name: 'culture')
+ create(:tag, kind: 'category', name: 'social services')
+
+ health_proposal = create(:proposal, tag_list: 'health')
+ culture_proposal = create(:proposal, tag_list: 'culture')
+ social_proposal = create(:proposal, tag_list: 'social services')
+
+ results = Proposal.for_summary.values.flatten
+
+ expect(results.first).to eq(culture_proposal)
+ expect(results.second).to eq(health_proposal)
+ expect(results.third).to eq(social_proposal)
+ end
+
it "should return proposals grouped by tag" do
create(:tag, kind: 'category', name: 'culture')
create(:tag, kind: 'category', name: 'health')