stores the heading where the user has voted

This commit is contained in:
rgarcia
2017-05-14 21:30:51 +02:00
committed by Javi Martín
parent e27f5903f2
commit 1eaa5cc77b
4 changed files with 24 additions and 0 deletions

View File

@@ -16,6 +16,7 @@ class Budget
scope :by_investment, ->(investment_id) { where(investment_id: investment_id) }
before_validation :set_denormalized_ids
after_save :store_user_heading
def check_sufficient_funds
errors.add(:money, "insufficient funds") if ballot.amount_available(investment.heading) < investment.price.to_i
@@ -37,6 +38,10 @@ class Budget
self.group_id ||= investment.try(:group_id)
self.budget_id ||= investment.try(:budget_id)
end
def store_user_heading
ballot.user.update(balloted_heading_id: heading.id)
end
end
end
end

View File

@@ -0,0 +1,5 @@
class AddBallotedHeadingIdToUsers < ActiveRecord::Migration
def change
add_column :users, :balloted_heading_id, :integer, default: nil
end
end

View File

@@ -1456,6 +1456,7 @@ ActiveRecord::Schema.define(version: 20190205131722) do
t.boolean "created_from_signature", default: false
t.integer "failed_email_digests_count", default: 0
t.text "former_users_data_log", default: ""
t.integer "balloted_heading_id"
t.boolean "public_interests", default: false
t.boolean "recommended_debates", default: true
t.boolean "recommended_proposals", default: true

View File

@@ -44,6 +44,19 @@ describe Budget::Ballot::Line do
end
describe "#store_user_heading" do
it "stores the heading where the user has voted" do
user = create(:user, :level_two)
investment = create(:budget_investment, :selected)
ballot = create(:budget_ballot, user: user, budget: investment.budget)
create(:budget_ballot_line, ballot: ballot, investment: investment)
expect(user.balloted_heading_id).to eq(investment.heading.id)
end
end
describe "scopes" do
describe "by_investment" do