Skip to content

Commit

Permalink
Create budget investment's votes
Browse files Browse the repository at this point in the history
Create budget investment's votes for all corresponding spending proposal's votes.

We already have budget investments for every spending proposal as of the partial migration done[1].

In method `create_budget_investment_vote` we are using 'yes' as the `vote` for consistency with the implementation in the `Investment`'s model[1].

With that acts_as_votable understands that it has to create a `vote_flag` of true before saving the vote to DB.

[1] #1059

[2] https://github.com/AyuntamientoMadrid/consul/blob/master/app/models/budget/investment.rb#L296
  • Loading branch information
voodoorai2000 committed Mar 5, 2019
1 parent 5cc5fd9 commit aa6a416
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/migrations/spending_proposal/vote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,25 @@ def represented_user_vote(represented_user, spending_proposal)
end
end

def create_budget_investment_votes
spending_proposal_votes.each do |vote|
create_budget_invesment_vote(vote)
end
end

private

def spending_proposal_votes
Vote.where(votable: SpendingProposal.all)
end

def create_budget_invesment_vote(vote)
budget_investment = find_budget_investment(vote.votable)
budget_investment.vote_by(voter: vote.voter, vote: "yes")
end

def find_budget_investment(spending_proposal)
Budget::Investment.where(original_spending_proposal_id: spending_proposal.id).first
end

end
1 change: 1 addition & 0 deletions lib/tasks/spending_proposals.rake
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace :spending_proposals do
task migrate_delegated_votes: :environment do
require "migrations/spending_proposal/vote"
Migrations::SpendingProposal::Vote.new.migrate_delegated_votes
Migrations::SpendingProposal::Vote.new.create_budget_investment_votes
end

desc "Migrates spending proposals attributes to corresponding budget investments attributes"
Expand Down
35 changes: 35 additions & 0 deletions spec/lib/migrations/spending_proposals/vote_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,39 @@
end

end

describe "#create_budget_investment_votes" do

it "creates a budget investment's vote for a corresponding spending proposal's vote" do
spending_proposal = create(:spending_proposal)
budget_investment = create(:budget_investment, original_spending_proposal_id: spending_proposal.id)

spending_proposal_vote = create(:vote, votable: spending_proposal)

Migrations::SpendingProposal::Vote.new.create_budget_investment_votes

budget_investment_vote = budget_investment.votes_for.first

expect(budget_investment.votes_for.count).to eq(1)
expect(budget_investment_vote.voter).to eq(spending_proposal_vote.voter)
expect(budget_investment_vote.votable).to eq(budget_investment)
expect(budget_investment_vote.vote_flag).to eq(true)
end

it "creates budget investment's votes for all correspoding spending proposal's votes" do
spending_proposal1 = create(:spending_proposal)
spending_proposal2 = create(:spending_proposal)
budget_investment1 = create(:budget_investment, original_spending_proposal_id: spending_proposal1.id)
budget_investment2 = create(:budget_investment, original_spending_proposal_id: spending_proposal2.id)

3.times { create(:vote, votable: spending_proposal1) }
5.times { create(:vote, votable: spending_proposal2) }

Migrations::SpendingProposal::Vote.new.create_budget_investment_votes

expect(budget_investment1.votes_for.count).to eq(3)
expect(budget_investment2.votes_for.count).to eq(5)
end

end
end

0 comments on commit aa6a416

Please sign in to comment.