Skip to content

Commit

Permalink
Migrate spending proposals' internal comments
Browse files Browse the repository at this point in the history
SpendingProposals used a string for internal comments.
Budget::Investmenets use a Comment object instead.

This commits migrates the internal comments string attribute to a Comment object.
  • Loading branch information
voodoorai2000 committed Mar 5, 2019
1 parent 08e19e9 commit d379c73
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/migrations/spending_proposal/budget_investment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
26 changes: 26 additions & 0 deletions spec/lib/migrations/spending_proposals/budget_investment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d379c73

Please sign in to comment.