From 71e6c5713abc0009b8949440225b33c05ef437bf Mon Sep 17 00:00:00 2001 From: voodoorai2000 Date: Fri, 31 May 2019 20:04:40 +0200 Subject: [PATCH] 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. --- lib/tasks/budgets.rake | 10 ++++++++++ spec/lib/tasks/budgets_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 spec/lib/tasks/budgets_spec.rb diff --git a/lib/tasks/budgets.rake b/lib/tasks/budgets.rake index d9fc8f50f..2059bca4b 100644 --- a/lib/tasks/budgets.rake +++ b/lib/tasks/budgets.rake @@ -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 diff --git a/spec/lib/tasks/budgets_spec.rb b/spec/lib/tasks/budgets_spec.rb new file mode 100644 index 000000000..67ec3e63c --- /dev/null +++ b/spec/lib/tasks/budgets_spec.rb @@ -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