adds specs for proposal summary
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -43,7 +43,7 @@ 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).
|
||||
scope :grouped_by_categories, -> { where("lower(tags.name) IN (?)", Proposal.category_names).
|
||||
joins(:tags).select('proposals.*, tags.name').
|
||||
group_by(&:name) }
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
<% end %>
|
||||
|
||||
<% @proposals.each do |group_name, proposals| %>
|
||||
|
||||
<div id="<%= group_name.parameterize %>">
|
||||
<h2 class="margin-top"><%= group_name %></h2>
|
||||
|
||||
<% proposals[0..2].each do |proposal| %>
|
||||
@@ -42,6 +42,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user