-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AP-4380: Add cloner service and copy proceedings
Use deep_clonable. This is one option, amoeba being another.
- Loading branch information
Showing
5 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module CopyCase | ||
class ClonerService | ||
attr_accessor :copy, :original | ||
|
||
def self.call(copy, original) | ||
new(copy, original).call | ||
end | ||
|
||
def initialize(copy, original) | ||
@copy = copy | ||
@original = original | ||
end | ||
|
||
def call | ||
clone_proceedings | ||
end | ||
|
||
private | ||
|
||
def clone_proceedings | ||
new_proceedings = original.proceedings.each_with_object([]) do |proceeding, memo| | ||
memo << proceeding.deep_clone(except: %i[legal_aid_application_id proceeding_case_id]) | ||
end | ||
|
||
copy.proceedings = new_proceedings | ||
copy.save! | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe CopyCase::ClonerService do | ||
subject(:instance) { described_class.new(copy, original) } | ||
|
||
describe "#call" do | ||
subject(:call) { instance.call } | ||
|
||
let(:copy) { create(:legal_aid_application, :with_applicant) } | ||
let(:original) { create(:legal_aid_application, :with_proceedings) } | ||
|
||
it "copies proceedings" do | ||
expect { call } | ||
.to change { copy.reload.proceedings.count } | ||
.from(0) | ||
.to(1) | ||
|
||
original_proceeding = original.proceedings.first | ||
copy_of_proceeding = copy.proceedings.first | ||
expected_attributes = original_proceeding | ||
.attributes | ||
.except("id", | ||
"legal_aid_application_id", | ||
"proceeding_case_id", | ||
"created_at", | ||
"updated_at") | ||
|
||
expect(copy_of_proceeding) | ||
.to have_attributes(**expected_attributes) | ||
end | ||
end | ||
end |