diff --git a/CHANGELOG.md b/CHANGELOG.md index 101dd4c5af..0c1959f48f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Changelog +- Refactor Plan.deep_copy(plan) [#3469](https://github.com/DMPRoadmap/roadmap/pull/3469) - Fixed a bug in the deep copy of plans where the old identifier was being copied into the new plan. We now copy the generated id of the new plan to the identifier field. - Fixed bar chart click function in the Usage dashboard (GitHub issue #3443) diff --git a/app/models/plan.rb b/app/models/plan.rb index f2fb6a2901..83fac933e9 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -257,11 +257,9 @@ def self.deep_copy(plan) plan_copy.save! # Copy newly generated Id to the identifier plan_copy.identifier = plan_copy.id.to_s - plan.save! plan.answers.each do |answer| answer_copy = Answer.deep_copy(answer) - answer_copy.plan_id = plan_copy.id - answer_copy.save! + plan_copy.answers << answer_copy end plan.guidance_groups.each do |guidance_group| plan_copy.guidance_groups << guidance_group if guidance_group.present? diff --git a/spec/models/plan_spec.rb b/spec/models/plan_spec.rb index 08bcca6f5b..bbddb5b76e 100644 --- a/spec/models/plan_spec.rb +++ b/spec/models/plan_spec.rb @@ -398,6 +398,10 @@ expect(subject.title).to include(plan.title) end + it "copies the new plan's id to its identifer" do + expect(subject.identifier).to eql(subject.id.to_s) + end + it 'persists the record' do expect(subject).to be_persisted end