Add rake task to set the original_heading_id value

By default we want this attribute to be the current heading id for existing investments. If there have been reclassifications, this field should be updated accordingly.
This commit is contained in:
voodoorai2000
2019-05-31 20:04:40 +02:00
committed by Javi Martín
parent 488e19f8a0
commit 71e6c5713a
2 changed files with 32 additions and 0 deletions

View File

@@ -23,4 +23,14 @@ namespace :budgets do
end
desc "Update investments original_heading_id with current heading_id"
task set_original_heading_id: :environment do
puts "Starting"
Budget::Investment.find_each do |investment|
investment.update_column(:original_heading_id, investment.heading_id)
print "."
end
puts "Finished"
end
end

View File

@@ -0,0 +1,22 @@
require "rails_helper"
describe Budget do
let(:run_rake_task) do
Rake.application.invoke_task("budgets:set_original_heading_id")
end
it "sets attribute original_heading_id for existing investments" do
heading = create(:budget_heading)
investment = create(:budget_investment, heading: heading)
investment.update(original_heading_id: nil)
expect(investment.original_heading_id).to equal(nil)
run_rake_task
investment.reload
expect(investment.original_heading_id).to equal(heading.id)
end
end