Fix edge case

The user was able to vote as many investments as wanted in the first
heading voted. However in the second heading voted, only one investment
could be voted

This was due to the previous implementation, where you could only vote
in one heading. Note the `first` call in method
`heading_voted_by_user?(user)`

This commits simplifies the logic and allows voting for any investment
in any heading that the user has previously voted in
This commit is contained in:
rgarcia
2018-03-22 23:08:41 +01:00
parent 5c6eaa76ff
commit 073cf74818
2 changed files with 36 additions and 8 deletions

View File

@@ -231,7 +231,7 @@ class Budget
end end
def valid_heading?(user) def valid_heading?(user)
voted_in?([heading.id], user) || voted_in?(heading, user) ||
can_vote_in_another_heading?(user) can_vote_in_another_heading?(user)
end end
@@ -243,13 +243,8 @@ class Budget
user.votes.for_budget_investments(budget.investments.where(group: group)).votables.map(&:heading_id).uniq user.votes.for_budget_investments(budget.investments.where(group: group)).votables.map(&:heading_id).uniq
end end
def voted_in?(heading_ids, user) def voted_in?(heading, user)
heading_ids.include? heading_voted_by_user?(user) headings_voted_by_user(user).include?(heading.id)
end
def heading_voted_by_user?(user)
user.votes.for_budget_investments(budget.investments.where(group: group))
.votables.map(&:heading_id).first
end end
def ballotable_by?(user) def ballotable_by?(user)

View File

@@ -603,6 +603,22 @@ describe Budget::Investment do
expect(salamanca_investment.valid_heading?(user)).to eq(true) expect(salamanca_investment.valid_heading?(user)).to eq(true)
end end
it "accepts votes in any heading previously voted in" do
group.update(max_votable_headings: 2)
carabanchel = create(:budget_heading, group: group)
salamanca = create(:budget_heading, group: group)
carabanchel_investment = create(:budget_investment, heading: carabanchel)
salamanca_investment = create(:budget_investment, heading: salamanca)
create(:vote, votable: carabanchel_investment, voter: user)
create(:vote, votable: salamanca_investment, voter: user)
expect(carabanchel_investment.valid_heading?(user)).to eq(true)
expect(salamanca_investment.valid_heading?(user)).to eq(true)
end
it "allows votes in a group with a single heading" do it "allows votes in a group with a single heading" do
all_city_investment = create(:budget_investment, heading: heading) all_city_investment = create(:budget_investment, heading: heading)
expect(all_city_investment.valid_heading?(user)).to eq(true) expect(all_city_investment.valid_heading?(user)).to eq(true)
@@ -702,6 +718,23 @@ describe Budget::Investment do
end end
end end
describe "#voted_in?" do
let(:user) { create(:user) }
let(:investment) { create(:budget_investment) }
it "returns true if the user has voted in this heading" do
create(:vote, votable: investment, voter: user)
expect(investment.voted_in?(investment.heading, user)).to eq(true)
end
it "returns false if the user has not voted in this heading" do
expect(investment.voted_in?(investment.heading, user)).to eq(false)
end
end
describe "Order" do describe "Order" do
describe "#sort_by_confidence_score" do describe "#sort_by_confidence_score" do