diff --git a/lib/migrations/spending_proposal/budget_investment.rb b/lib/migrations/spending_proposal/budget_investment.rb index 1227d3e0343..8eb4bf27dd3 100644 --- a/lib/migrations/spending_proposal/budget_investment.rb +++ b/lib/migrations/spending_proposal/budget_investment.rb @@ -7,7 +7,7 @@ def initialize(spending_proposal) end def update - if budget_investment && budget_investment.update(budget_investment_attributes) + if updated? print "." else puts "Error updating budget investment from spending proposal: #{spending_proposal.id}\n" @@ -24,6 +24,17 @@ def find_budget_investment budget.investments.where(original_spending_proposal_id: spending_proposal.id).first end + def updated? + return unless budget_investment + + update_attributes && + create_valuation_comments + end + + def update_attributes + budget_investment.update(budget_investment_attributes) + end + def budget_investment_attributes { unfeasibility_explanation: field_with_unfeasibility_explanation } end @@ -38,4 +49,19 @@ def field_with_unfeasibility_explanation end end + def create_valuation_comments + if spending_proposal.internal_comments.present? + budget_investment.comments.create!(valuation_comment_attributes) + end + end + + def valuation_comment_attributes + { + body: spending_proposal.internal_comments, + user: spending_proposal.administrator.user, + commentable: budget_investment, + valuation: true + } + end + end diff --git a/spec/lib/migrations/spending_proposals/budget_investment_spec.rb b/spec/lib/migrations/spending_proposals/budget_investment_spec.rb index 9718b2f3b8e..48941a681c0 100644 --- a/spec/lib/migrations/spending_proposals/budget_investment_spec.rb +++ b/spec/lib/migrations/spending_proposals/budget_investment_spec.rb @@ -92,4 +92,30 @@ end + context "internal comments" do + + it "migrates internal_comments string to a comment object" do + internal_comment = "This project will last 2 years" + + spending_proposal.update(internal_comments: internal_comment) + spending_proposal.update(administrator: create(:administrator)) + + migration = Migrations::SpendingProposal::BudgetInvestment.new(spending_proposal) + migration.update + + comment = Comment.first + expect(Comment.count).to eq(1) + expect(comment.body).to eq(internal_comment) + expect(comment.author).to eq(spending_proposal.administrator.user) + expect(comment.commentable).to eq(budget_investment) + expect(comment.valuation).to eq(true) + end + + it "does not create a comment if internal_comments is blank" do + migration = Migrations::SpendingProposal::BudgetInvestment.new(spending_proposal) + migration.update + + expect(Comment.count).to eq(0) + end + end end