Extract "supported headings" logic to User method

In preparation to use this method from views where
it doesn't make sense for it to be associated with
a specific investment.
This commit is contained in:
Marko Lovic
2018-07-09 17:06:12 +02:00
committed by decabeza
parent e76f483850
commit e47cbe2a10
3 changed files with 36 additions and 2 deletions

View File

@@ -257,7 +257,7 @@ class Budget
end end
def can_vote_in_another_heading?(user) def can_vote_in_another_heading?(user)
headings_voted_by_user(user).count < group.max_votable_headings user.headings_voted_within_group(group).count < group.max_votable_headings
end end
def headings_voted_by_user(user) def headings_voted_by_user(user)
@@ -265,7 +265,7 @@ class Budget
end end
def voted_in?(heading, user) def voted_in?(heading, user)
headings_voted_by_user(user).include?(heading.id) user.headings_voted_within_group(group).where(id: heading.id).exists?
end end
def ballotable_by?(user) def ballotable_by?(user)

View File

@@ -127,6 +127,11 @@ class User < ActiveRecord::Base
votes.for_budget_investments(Budget::Investment.where(group: group)).exists? votes.for_budget_investments(Budget::Investment.where(group: group)).exists?
end end
def headings_voted_within_group(group)
voted_investments = votes.for_budget_investments(Budget::Investment.by_group(group.id)).votables
Budget::Heading.where(id: voted_investments.map(&:heading_id).uniq)
end
def administrator? def administrator?
administrator.present? administrator.present?
end end

View File

@@ -2,6 +2,35 @@ require 'rails_helper'
describe User do describe User do
describe '#headings_voted_within_group' 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(user1.headings_voted_within_group(group)).to include(new_york)
expect(user1.headings_voted_within_group(group)).to include(san_franciso)
expect(user1.headings_voted_within_group(group)).to_not include(another_heading)
expect(user2.headings_voted_within_group(group)).to_not include(new_york)
expect(user2.headings_voted_within_group(group)).to_not include(san_franciso)
expect(user2.headings_voted_within_group(group)).to_not include(another_heading)
end
end
describe "#debate_votes" do describe "#debate_votes" do
let(:user) { create(:user) } let(:user) { create(:user) }