Skip to content

Commit

Permalink
Add support for BSON::Document sources to SafeCopyAttributesService (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulDoyle-DEFRA committed May 21, 2024
1 parent 5333109 commit 395c808
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def run(source_instance:, target_class:, embedded_documents: [], attributes_to_e
end

def source_attributes
source_instance.attributes.except(*attributes_to_exclude)
attributes = source_instance.is_a?(BSON::Document) ? source_instance : source_instance.attributes

attributes.except(*attributes_to_exclude)
end

def target_fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ module WasteCarriersEngine

# ensure all available attributes are populated on the source
before do
source_instance.class.fields.keys.excluding("_id").each do |attr|
next unless source_instance.send(attr).blank? && source_instance.respond_to?("#{attr}=")
unless source_instance.is_a?(BSON::Document)
source_instance.class.fields.keys.excluding("_id").each do |attr|
next unless source_instance.send(attr).blank? && source_instance.respond_to?("#{attr}=")

source_instance.send "#{attr}=", 0
source_instance.send "#{attr}=", 0
end
end
end

Expand Down Expand Up @@ -47,7 +49,7 @@ module WasteCarriersEngine
context "when the target is a Registration" do
let(:target_class) { Registration }
# include all embeds_many relationships
let(:embedded_documents) { %w[addresses finainceDetails metaData] }
let(:embedded_documents) { %w[addresses financeDetails metaData] }
let(:copyable_attributes) { %w[location contactEmail] }
let(:non_copyable_attributes) { %w[workflow_state temp_contact_postcode not_even_an_attribute] }
let(:exclusion_list) { %w[_id email_history] }
Expand All @@ -69,6 +71,12 @@ module WasteCarriersEngine

it_behaves_like "returns the correct attributes"
end

context "when the source is a BSON::Document" do
let(:source_instance) { BSON::Document.new(build(:new_registration, :has_required_data).attributes) }

it_behaves_like "returns the correct attributes"
end
end

context "when the source and target are Addresses" do
Expand Down

0 comments on commit 395c808

Please sign in to comment.