From 42fdc52329b30e57a76e52d81f297d594537bfbb Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 16 Feb 2016 13:53:39 +0100 Subject: [PATCH] adds specs for proposal summary --- app/controllers/proposals_controller.rb | 2 +- app/models/proposal.rb | 6 +-- app/views/proposals/summary.html.erb | 51 +++++++++---------- spec/features/proposals_spec.rb | 65 +++++++++++++++++++++++++ spec/models/proposal_spec.rb | 33 +++++++++++-- 5 files changed, 125 insertions(+), 32 deletions(-) diff --git a/app/controllers/proposals_controller.rb b/app/controllers/proposals_controller.rb index ddb6a66b6..8c9808768 100644 --- a/app/controllers/proposals_controller.rb +++ b/app/controllers/proposals_controller.rb @@ -36,7 +36,7 @@ class ProposalsController < ApplicationController end def summary - @proposals = Proposal.last_week.sort_by_confidence_score.grouped_by_categories(Proposal.category_names) + @proposals = Proposal.last_week.sort_by_confidence_score.grouped_by_categories end private diff --git a/app/models/proposal.rb b/app/models/proposal.rb index f46a2e555..6d59b4ee9 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -43,9 +43,9 @@ class Proposal < ActiveRecord::Base scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) } scope :last_week, -> { where("proposals.created_at >= ?", 7.days.ago)} - scope :grouped_by_categories, -> (categories) { where("lower(tags.name) IN (?)", categories). - joins(:tags).select('proposals.*, tags.name'). - group_by(&:name) } + scope :grouped_by_categories, -> { where("lower(tags.name) IN (?)", Proposal.category_names). + joins(:tags).select('proposals.*, tags.name'). + group_by(&:name) } def searchable_values { title => 'A', diff --git a/app/views/proposals/summary.html.erb b/app/views/proposals/summary.html.erb index d5a7f7616..faa89eaea 100644 --- a/app/views/proposals/summary.html.erb +++ b/app/views/proposals/summary.html.erb @@ -8,40 +8,41 @@ <% end %> <% @proposals.each do |group_name, proposals| %> +
+

<%= group_name %>

-

<%= group_name %>

+ <% proposals[0..2].each do |proposal| %> +
+
+
+
+
+

<%= link_to proposal.title, namespaced_proposal_path(proposal) %>

- <% proposals[0..2].each do |proposal| %> -
-
-
-
-
-

<%= link_to proposal.title, namespaced_proposal_path(proposal) %>

- -

- <% if proposal.author.hidden? || proposal.author.erased? %> - <%= t("proposals.show.author_deleted") %> - <% else %> - <%= proposal.author.name %> - <% if proposal.author.official? %> - - <%= proposal.author.official_position %> - +

+ <% if proposal.author.hidden? || proposal.author.erased? %> + <%= t("proposals.show.author_deleted") %> + <% else %> + <%= proposal.author.name %> + <% if proposal.author.official? %> + + <%= proposal.author.official_position %> + + <% end %> <% end %> - <% end %> -

+

-
- <%= link_to proposal.description, namespaced_proposal_path(proposal) %> -
+
+ <%= link_to proposal.description, namespaced_proposal_path(proposal) %> +
+
-
- <% end %> + <% end %> +
<% end %>
diff --git a/spec/features/proposals_spec.rb b/spec/features/proposals_spec.rb index 4d1de0af1..ad746a5a9 100644 --- a/spec/features/proposals_spec.rb +++ b/spec/features/proposals_spec.rb @@ -1162,4 +1162,69 @@ feature 'Proposals' do end end end + + context "Summary" do + + scenario "Displays proposals grouped by category" do + 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') } + + create(:proposal, tag_list: 'Random') + + visit summary_proposals_path + + within("#culture") do + expect(page).to have_content("Culture") + expect(page).to have_css(".proposal", count: 3) + end + + within("#social-services") do + expect(page).to have_content("Social Services") + 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') } + + visit summary_proposals_path + + expect(page).to have_css(".proposal", count: 3) + end + + scenario "Orders proposals by votes" do + create(:tag, kind: 'category', name: 'Culture') + create(:proposal, title: 'Best', tag_list: 'Culture').update_column(:confidence_score, 10) + create(:proposal, title: 'Worst', tag_list: 'Culture').update_column(:confidence_score, 2) + create(:proposal, title: 'Medium', tag_list: 'Culture').update_column(:confidence_score, 5) + + visit summary_proposals_path + + expect('Best').to appear_before('Medium') + expect('Medium').to appear_before('Worst') + end + + scenario "Displays proposals from last week" do + create(:tag, kind: 'category', name: 'Culture') + proposal1 = create(:proposal, tag_list: 'Culture', created_at: 1.day.ago) + proposal2 = create(:proposal, tag_list: 'Culture', created_at: 5.days.ago) + proposal3 = create(:proposal, tag_list: 'Culture', created_at: 8.days.ago) + + visit summary_proposals_path + + within("#proposals") do + 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 + end diff --git a/spec/models/proposal_spec.rb b/spec/models/proposal_spec.rb index 8a61afcaf..45d139503 100644 --- a/spec/models/proposal_spec.rb +++ b/spec/models/proposal_spec.rb @@ -633,12 +633,39 @@ describe Proposal do describe "#last_week" do it "should return proposals created this week" do proposal = create(:proposal) - expect(Proposal.last_week.all).to include (proposal) + expect(Proposal.last_week).to include(proposal) end - it "should not show proposals created more than a week ago" do + it "should not return proposals created more than a week ago" do proposal = create(:proposal, created_at: 8.days.ago) - expect(Proposal.last_week.all).to_not include (proposal) + expect(Proposal.last_week).to_not include(proposal) + end + end + + describe "grouped_by_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.grouped_by_categories.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.grouped_by_categories.values.flatten).to_not include(proposal) + end + + it "should return proposals grouped by tag" do + create(:tag, kind: 'category', name: 'Culture') + create(:tag, kind: 'category', name: 'Health') + + proposal1 = create(:proposal, tag_list: 'Culture') + proposal2 = create(:proposal, tag_list: 'Culture') + proposal3 = create(:proposal, tag_list: 'Health') + + expect(Proposal.grouped_by_categories).to include('Culture' => [proposal2, proposal1], 'Health' => [proposal3]) end end