extracts reclassification to a module
This commit is contained in:
@@ -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
|
||||||
|
|||||||
47
app/models/budget/reclassification.rb
Normal file
47
app/models/budget/reclassification.rb
Normal 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
|
||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user