Add headings_voted_by_user
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
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user