From 5c6eaa76ff95c07f52d525e971cc45d1a8569374 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Thu, 22 Mar 2018 22:24:38 +0100 Subject: [PATCH] Add headings_voted_by_user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This method was used only in Madrid’s fork, but it is now needed to complete the backport for voting in multiple headings There wasn’t a test in Madrid, so here goes one too. Even though, the responsibility should probably be moved soon to the `Budget::Heading`. For consistency with the related methods and tests it has been left in the investment_spec --- app/models/budget/investment.rb | 4 ++++ spec/models/budget/investment_spec.rb | 29 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 28cc5bded..6e4a9f477 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -239,6 +239,10 @@ class Budget headings_voted_by_user(user).count < group.max_votable_headings end + def headings_voted_by_user(user) + user.votes.for_budget_investments(budget.investments.where(group: group)).votables.map(&:heading_id).uniq + end + def voted_in?(heading_ids, user) heading_ids.include? heading_voted_by_user?(user) end diff --git a/spec/models/budget/investment_spec.rb b/spec/models/budget/investment_spec.rb index 73bd46c5d..d277647de 100644 --- a/spec/models/budget/investment_spec.rb +++ b/spec/models/budget/investment_spec.rb @@ -673,6 +673,35 @@ describe Budget::Investment do end end + describe "#headings_voted_by_user" do + it "returns the headings voted by a user" do + user1 = create(:user) + user2 = create(:user) + + budget = create(:budget) + group = create(:budget_group, budget: budget) + + new_york = create(:budget_heading, group: group) + san_franciso = create(:budget_heading, group: group) + another_heading = create(:budget_heading, group: group) + + new_york_investment = create(:budget_investment, heading: new_york) + san_franciso_investment = create(:budget_investment, heading: san_franciso) + another_investment = create(:budget_investment, heading: san_franciso) + + create(:vote, votable: new_york_investment, voter: user1) + create(:vote, votable: san_franciso_investment, voter: user1) + + expect(another_investment.headings_voted_by_user(user1)).to include(new_york.id) + expect(another_investment.headings_voted_by_user(user1)).to include(san_franciso.id) + expect(another_investment.headings_voted_by_user(user1)).to_not include(another_heading.id) + + expect(another_investment.headings_voted_by_user(user2)).to_not include(new_york.id) + expect(another_investment.headings_voted_by_user(user2)).to_not include(san_franciso.id) + expect(another_investment.headings_voted_by_user(user2)).to_not include(another_heading.id) + end + end + describe "Order" do describe "#sort_by_confidence_score" do