extracts reclassification to a module

This commit is contained in:
rgarcia
2017-05-17 15:18:39 +02:00
parent 42f62e5c36
commit 47f97e342d
3 changed files with 49 additions and 39 deletions

View File

@@ -5,6 +5,7 @@ class Budget
include Sanitizable include Sanitizable
include Taggable include Taggable
include Searchable include Searchable
include Reclassification
acts_as_votable acts_as_votable
acts_as_paranoid column: :hidden_at acts_as_paranoid column: :hidden_at
@@ -61,7 +62,6 @@ class Budget
before_save :calculate_confidence_score before_save :calculate_confidence_score
before_validation :set_responsible_name before_validation :set_responsible_name
before_validation :set_denormalized_ids before_validation :set_denormalized_ids
after_save :check_for_reclassification
def self.filter_params(params) def self.filter_params(params)
params.select{|x,_| %w{heading_id group_id administrator_id tag_name valuator_id}.include? x.to_s } params.select{|x,_| %w{heading_id group_id administrator_id tag_name valuator_id}.include? x.to_s }
@@ -243,43 +243,6 @@ class Budget
investments investments
end 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 log_heading_change
update_column(:previous_heading_id, heading_id_was)
end
def marked_as_unfeasible?
budget.balloting? && feasibility_changed? && unfeasible?
end
def store_reclassified_votes(reason)
ballot_lines_for_investment.each do |line|
Budget::ReclassifiedVote.create!(user: line.ballot.user, investment: self, reason: reason)
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
private private
def set_denormalized_ids def set_denormalized_ids

View File

@@ -0,0 +1,47 @@
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 log_heading_change
update_column(:previous_heading_id, heading_id_was)
end
def marked_as_unfeasible?
budget.balloting? && feasibility_changed? && unfeasible?
end
def store_reclassified_votes(reason)
ballot_lines_for_investment.each do |line|
Budget::ReclassifiedVote.create!(user: line.ballot.user, investment: self, reason: reason)
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

@@ -670,7 +670,7 @@ feature 'Budget Investments' do
expect(page).to_not have_link("Vote") expect(page).to_not have_link("Vote")
end end
scenario "Reclassification" do scenario "Reclassification", :focus do
user = create(:user, :level_two) user = create(:user, :level_two)
investment = create(:budget_investment, :selected, heading: heading) investment = create(:budget_investment, :selected, heading: heading)
heading2 = create(:budget_heading, group: group) heading2 = create(:budget_heading, group: group)