8000 Refactor `Plan.deep_copy(plan)` by aaronskiba · Pull Request #3469 · DMPRoadmap/roadmap · GitHub
[go: up one dir, main page]

Skip to content

Refactor Plan.deep_copy(plan) #3469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 16, 2024
Merged

Conversation

aaronskiba
Copy link
Contributor

Changes proposed in this PR:

  • Remove redundant plan.save! (line 260) 4b25e4a

    • Considering that the removed line occurred immediately after plan_copy.identifier = plan_copy.id.to_s, I believe this was meant to be plan_copy.save!. However, rather than correcting the variable, I think the line can be removed completely .
      The earlier execution of plan_copy.save! (line 257) is necessary because we need to generate a pk for plan_copy. However, the duplicate action within PlansController appears to be the only thing calling Plan.deep_copy, and the action is performing its own save on the copied plan returned from deep_copy (see below):
    def duplicate
    plan = Plan.find(params[:id])
    authorize plan
    @plan = Plan.deep_copy(plan)
    respond_to do |format|
    if @plan.save
    ...
  • Refactor / use ActiveRecord association operator d8f8978

    • Using << here replaces manual foreign key assignment as well as the explicit save call.
  • Add test for plan identifier copying 521ba8e

aaronskiba 8000 added 3 commits December 11, 2024 14:12
Considering that the removed line occurred immediately after  `plan_copy.identifier = plan_copy.id.to_s`, I believe this  was meant to be `plan_copy.save!`. However, rather than correcting the variable, I think the line can be removed completely (explanation below).

The earlier execution of `plan_copy.save!` (line 257) is necessary because we need to generate a pk for `plan_copy`. However, the `duplicate` action within PlansController appears to be the only thing calling Plan.deep_copy, and the action is performing its own save on the copied plan returned from deep_copy:

  def duplicate
    plan = Plan.find(params[:id])
    authorize plan
    @plan = Plan.deep_copy(plan)
    respond_to do |format|
      if @plan.save
       ...
Using << here replaces manual foreign key assignment as well as the explicit save call.
Copy link
Contributor
@johnpinto1 johnpinto1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aaronskiba I have reviewed this and it works as suggested. Attached is spreadsheet of my checks.
I copied an existing plan by making a copy through the UI and a deep copy in the Rails console.
In both cases: the plan_id (was new as expected), the plan identifier matched the plan id and plan answers matched with plan_id changed as expected.
TestingPlanCopy.xlsx

@aaronskiba
Copy link
Contributor Author

@aaronskiba I have reviewed this and it works as suggested. Attached is spreadsheet of my checks. I copied an existing plan by making a copy through the UI and a deep copy in the Rails console. In both cases: the plan_id (was new as expected), the plan identifier matched the plan id and plan answers matched with plan_id changed as expected. TestingPlanCopy.xlsx

Excellent! Thank you for the approval and the testing @johnpinto1!

@aaronskiba aaronskiba merged commit 2d707d5 into development Dec 16, 2024
8 checks passed
@aaronskiba aaronskiba deleted the aaron/refactor-plan-deep-copy branch December 16, 2024 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0