Merge pull request #1550 from consul/budgets-unfeasible

Investments reclassified as unfeasible
This commit is contained in:
Juanjo Bazán
2017-05-18 14:37:32 +02:00
committed by GitHub
11 changed files with 236 additions and 47 deletions

View File

@@ -13,6 +13,8 @@ class Budget
validate :check_sufficient_funds
validate :check_valid_heading
scope :by_investment, -> (investment_id) { where(investment_id: investment_id) }
before_validation :set_denormalized_ids
def check_sufficient_funds

View File

@@ -5,6 +5,7 @@ class Budget
include Sanitizable
include Taggable
include Searchable
include Reclassification
acts_as_votable
acts_as_paranoid column: :hidden_at
@@ -61,7 +62,6 @@ class Budget
before_save :calculate_confidence_score
before_validation :set_responsible_name
before_validation :set_denormalized_ids
after_save :check_for_reclassification
def self.filter_params(params)
params.select{|x,_| %w{heading_id group_id administrator_id tag_name valuator_id}.include? x.to_s }
@@ -243,25 +243,6 @@ class Budget
investments
end
def check_for_reclassification
if reclassified?
log_reclassification
remove_reclassified_votes
end
end
def reclassified?
budget.balloting? && heading_id_changed?
end
def log_reclassification
update_column(:previous_heading_id, heading_id_was)
end
def remove_reclassified_votes
Budget::Ballot::Line.where(investment: self).destroy_all
end
private
def set_denormalized_ids

View File

@@ -0,0 +1,50 @@
class Budget
module Reclassification
extend ActiveSupport::Concern
included do
after_save :check_for_reclassification
end
def check_for_reclassification
if heading_changed?
log_heading_change
store_reclassified_votes("heading_changed")
remove_reclassified_votes
elsif marked_as_unfeasible?
store_reclassified_votes("unfeasible")
remove_reclassified_votes
end
end
def heading_changed?
budget.balloting? && heading_id_changed?
end
def marked_as_unfeasible?
budget.balloting? && feasibility_changed? && unfeasible?
end
def log_heading_change
update_column(:previous_heading_id, heading_id_was)
end
def store_reclassified_votes(reason)
ballot_lines_for_investment.each do |line|
attrs = { user: line.ballot.user,
investment: self,
reason: reason }
Budget::ReclassifiedVote.create!(attrs)
end
end
def remove_reclassified_votes
ballot_lines_for_investment.destroy_all
end
def ballot_lines_for_investment
Budget::Ballot::Line.by_investment(self.id)
end
end
end

View File

@@ -0,0 +1,12 @@
class Budget
class ReclassifiedVote < ActiveRecord::Base
REASONS = %w(heading_changed unfeasible)
belongs_to :user
belongs_to :investment
validates :user, presence: true
validates :investment, presence: true
validates :reason, inclusion: {in: REASONS, allow_nil: false}
end
end