From 47f97e342de4fbfaf0cb1bec1bdcac47b8858acb Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 17 May 2017 15:18:39 +0200 Subject: [PATCH] extracts reclassification to a module --- app/models/budget/investment.rb | 39 +------------------ app/models/budget/reclassification.rb | 47 +++++++++++++++++++++++ spec/features/budgets/investments_spec.rb | 2 +- 3 files changed, 49 insertions(+), 39 deletions(-) create mode 100644 app/models/budget/reclassification.rb diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 1d4230e0c..ea9276d91 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -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,43 +243,6 @@ class Budget investments 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 def set_denormalized_ids diff --git a/app/models/budget/reclassification.rb b/app/models/budget/reclassification.rb new file mode 100644 index 000000000..5265912ef --- /dev/null +++ b/app/models/budget/reclassification.rb @@ -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 \ No newline at end of file diff --git a/spec/features/budgets/investments_spec.rb b/spec/features/budgets/investments_spec.rb index a6f988e56..ea7cf6c99 100644 --- a/spec/features/budgets/investments_spec.rb +++ b/spec/features/budgets/investments_spec.rb @@ -670,7 +670,7 @@ feature 'Budget Investments' do expect(page).to_not have_link("Vote") end - scenario "Reclassification" do + scenario "Reclassification", :focus do user = create(:user, :level_two) investment = create(:budget_investment, :selected, heading: heading) heading2 = create(:budget_heading, group: group)