From bec3c8ea85df15520fbe1c401972f26fb2211d6c Mon Sep 17 00:00:00 2001 From: Guillaume Dott Date: Tue, 26 Jul 2016 14:55:14 +0200 Subject: [PATCH] Add recipients and documents to hash only when changed --- lib/docurest/envelope.rb | 11 +++++++---- lib/docurest/field.rb | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/docurest/envelope.rb b/lib/docurest/envelope.rb index 06ab72a..d74b6e1 100644 --- a/lib/docurest/envelope.rb +++ b/lib/docurest/envelope.rb @@ -94,10 +94,13 @@ module Docurest emailSubject: emailSubject, emailBlurb: emailBlurb, emailSettings: emailSettings.to_h, - documents: documents.map(&:to_h), - recipients: Docurest::Base.hash_by_type(recipients), - files: files, - } + }.tap do |hash| + if documents_changed? + hash[:documents] = documents.map(&:to_h) + hash[:files] = files + end + hash[:recipients] = Docurest::Base.hash_by_type(recipients) if recipients_changed? + end end private diff --git a/lib/docurest/field.rb b/lib/docurest/field.rb index 6e9423b..00307ec 100644 --- a/lib/docurest/field.rb +++ b/lib/docurest/field.rb @@ -45,8 +45,14 @@ module Docurest value.send("#{parent_field}=", guid) if parent_field value.id = values.length + 1 unless value.id + + instance_variable_set(:"@#{klass}_changed", true) + values << value end + define_method("#{klass}_changed?") do + !!instance_variable_get(:"@#{klass}_changed") + end end end end