forked from consuldemocracy/consuldemocracy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request consuldemocracy#1776 from AyuntamientoMadrid/spend…
…ing_proposals Migrate spending proposal
- Loading branch information
Showing
38 changed files
with
1,771 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddDelegatedToVotes < ActiveRecord::Migration | ||
def change | ||
add_column :votes, :delegated, :boolean, default: false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Migrations::Log | ||
|
||
def log(message) | ||
print message unless Rails.env.test? | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
require_dependency "budget" | ||
require_dependency "budget/ballot" | ||
|
||
class Migrations::SpendingProposal::Ballot | ||
include Migrations::Log | ||
|
||
attr_accessor :spending_proposal_ballot, :budget_investment_ballot, :represented_user | ||
|
||
def initialize(spending_proposal_ballot, represented_user=nil) | ||
@represented_user = represented_user | ||
@spending_proposal_ballot = spending_proposal_ballot | ||
@budget_investment_ballot = find_or_initialize_budget_investment_ballot | ||
end | ||
|
||
def migrate_ballot | ||
return if user_already_voted? | ||
|
||
if budget_investment_ballot.save | ||
log(".") | ||
migrate_ballot_lines | ||
else | ||
log("\nError creating budget investment ballot from spending proposal ballot #{spending_proposal_ballot.id}\n") | ||
end | ||
end | ||
|
||
def migrate_ballot_lines | ||
spending_proposal_ballot.spending_proposals.each do |spending_proposal| | ||
budget_investment = find_budget_investment(spending_proposal) | ||
|
||
if budget_investment.blank? | ||
log("Budget investment not found for spending proposal #{spending_proposal.id}") | ||
next | ||
end | ||
|
||
ballot_line = find_or_initialize_ballot_line(budget_investment) | ||
if ballot_line_saved?(ballot_line) | ||
log(".") | ||
else | ||
log("Error adding spending proposal: #{spending_proposal.id} to ballot: #{budget_investment_ballot.id}\n") | ||
log(ballot_line.errors.messages) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def budget | ||
Budget.where(slug: "2016").first | ||
end | ||
|
||
def find_budget_investment(spending_proposal) | ||
budget.investments.where(original_spending_proposal_id: spending_proposal.id).first | ||
end | ||
|
||
def find_or_initialize_budget_investment_ballot | ||
Budget::Ballot.find_or_initialize_by(budget_investment_ballot_attributes) | ||
end | ||
|
||
def find_or_initialize_ballot_line(investment) | ||
return nil if investment.blank? | ||
|
||
attributes = { ballot: budget_investment_ballot, investment: investment } | ||
budget_investment_ballot.lines.where(attributes).first_or_initialize | ||
end | ||
|
||
def user_already_voted? | ||
budget_investment_ballot.ballot_lines_count > 0 && represented_user | ||
end | ||
|
||
def ballot_line_saved?(ballot_line) | ||
return true if ballot_line_exists?(ballot_line) | ||
|
||
ballot_line.set_denormalized_ids | ||
ballot_line.save(validate: false) | ||
end | ||
|
||
def ballot_line_exists?(ballot_line) | ||
budget_investment_ballot.investments.include?(ballot_line.investment) | ||
end | ||
|
||
def budget_investment_ballot_attributes | ||
{ | ||
budget_id: budget.id, | ||
user_id: user_id | ||
} | ||
end | ||
|
||
def user_id | ||
represented_user.try(:id) || spending_proposal_ballot.user_id | ||
end | ||
|
||
end |
Oops, something went wrong.