Add original_heading_id to investments

Investments can be reclassified to a different heading during the participatory budget process.

Whilst we are recording this change of heading in the `previous_heading_id` attribute, we are only keeping the _last_ heading. If there are multiple reclassifications we lose this chain of reclassifications.

In this commit we are adding an `original_heading_id` attribute, that will only be set once, when creating the investment, and will not get lost with multiple reclassificaitons of an investment.
This commit is contained in:
voodoorai2000
2019-05-31 20:00:54 +02:00
committed by Javi Martín
parent 8670cafa2e
commit 488e19f8a0
4 changed files with 34 additions and 0 deletions

View File

@@ -104,6 +104,7 @@ class Budget
scope :for_render, -> { includes(:heading) } scope :for_render, -> { includes(:heading) }
before_create :set_original_heading_id
before_save :calculate_confidence_score before_save :calculate_confidence_score
after_save :recalculate_heading_winners after_save :recalculate_heading_winners
before_validation :set_responsible_name before_validation :set_responsible_name
@@ -404,6 +405,10 @@ class Budget
self.budget_id ||= heading&.group&.budget_id self.budget_id ||= heading&.group&.budget_id
end end
def set_original_heading_id
self.original_heading_id = heading_id
end
def change_log def change_log
self.changed.each do |field| self.changed.each do |field|
unless field == "updated_at" unless field == "updated_at"

View File

@@ -0,0 +1,5 @@
class AddOriginalHeadingIdToInvestments < ActiveRecord::Migration[5.0]
def change
add_column :budget_investments, :original_heading_id, :integer
end
end

View File

@@ -294,6 +294,7 @@ ActiveRecord::Schema.define(version: 20190607160900) do
t.datetime "ignored_flag_at" t.datetime "ignored_flag_at"
t.integer "flags_count", default: 0 t.integer "flags_count", default: 0
t.integer "tracker_assignments_count" t.integer "tracker_assignments_count"
t.integer "original_heading_id"
t.index ["administrator_id"], name: "index_budget_investments_on_administrator_id", using: :btree t.index ["administrator_id"], name: "index_budget_investments_on_administrator_id", using: :btree
t.index ["author_id"], name: "index_budget_investments_on_author_id", using: :btree t.index ["author_id"], name: "index_budget_investments_on_author_id", using: :btree
t.index ["community_id"], name: "index_budget_investments_on_community_id", using: :btree t.index ["community_id"], name: "index_budget_investments_on_community_id", using: :btree

View File

@@ -56,6 +56,29 @@ describe Budget::Investment do
expect(investment.group_id).to eq group_2.id expect(investment.group_id).to eq group_2.id
end end
it "logs previous heading value if it is changed" do
budget = create(:budget, phase: "balloting")
group = create(:budget_group, budget: budget)
heading_1 = create(:budget_heading, group: group)
heading_2 = create(:budget_heading, group: group)
investment = create(:budget_investment, heading: heading_1)
expect(investment.previous_heading_id).to eq nil
investment.update(heading: heading_2)
expect(investment.previous_heading_id).to eq heading_1.id
end
it "stores original heading id" do
investment = create(:budget_investment)
expect(investment.original_heading_id).to eq investment.heading_id
end
describe "#unfeasibility_explanation blank" do describe "#unfeasibility_explanation blank" do
it "is valid if valuation not finished" do it "is valid if valuation not finished" do
investment.unfeasibility_explanation = "" investment.unfeasibility_explanation = ""