Skip to content

Commit

Permalink
AP-4380: Add cloner service and copy proceedings
Browse files Browse the repository at this point in the history
Use deep_clonable. This is one option, amoeba being
another.
  • Loading branch information
jsugarman committed Oct 23, 2023
1 parent 48f3bdc commit ef53de6
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ruby "3.2.2"

gem "aasm", "~> 5.5.0"
gem "active_model_serializers", "~> 0.10.14"
gem "deep_cloneable", "~> 3.2.0"
gem "discard", "~> 1.3"
gem "geckoboard-ruby"
gem "google-apis-sheets_v4"
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ GEM
date (3.3.3)
debug_inspector (1.1.0)
declarative (0.0.20)
deep_cloneable (3.2.0)
activerecord (>= 3.1.0, < 8)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
devise (4.9.3)
Expand Down Expand Up @@ -732,6 +734,7 @@ DEPENDENCIES
cucumber
cucumber-rails (>= 2.4.0)
database_cleaner
deep_cloneable (~> 3.2.0)
devise
devise_saml_authenticatable (>= 1.7.0)
dibber
Expand Down
8 changes: 8 additions & 0 deletions app/forms/copy_case/confirmation_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ class ConfirmationForm < BaseForm
validates :copy_case_id, presence: true, unless: proc { draft? }
validates :copy_case_confirmation, presence: true, unless: proc { draft? || copy_case_confirmation.present? }

def save
return false unless valid?

cloner = CopyCase::ClonerService.new(legal_aid_application, copy_case_id)
cloner.call
end
alias_method :save!, :save

def exclude_from_model
%i[copy_case_id copy_case_confirmation]
end
Expand Down
29 changes: 29 additions & 0 deletions app/services/copy_case/cloner_service.rb
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
32 changes: 32 additions & 0 deletions spec/services/copy_case/cloner_service_spec.rb
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

0 comments on commit ef53de6

Please sign in to comment.