From 6e857d8e9f310a2248e8b0c3b3d30048eb327e17 Mon Sep 17 00:00:00 2001 From: Jared Beck Date: Fri, 27 Nov 2015 23:02:57 -0500 Subject: [PATCH] Extract method: pt_recordable_object_changes --- lib/paper_trail/has_paper_trail.rb | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/paper_trail/has_paper_trail.rb b/lib/paper_trail/has_paper_trail.rb index d5d99e96..3d893a93 100644 --- a/lib/paper_trail/has_paper_trail.rb +++ b/lib/paper_trail/has_paper_trail.rb @@ -370,8 +370,7 @@ module PaperTrail data[PaperTrail.timestamp_field] = updated_at end if paper_trail_options[:save_changes] && changed_notably? && self.class.paper_trail_version_class.column_names.include?('object_changes') - data[:object_changes] = self.class.paper_trail_version_class.object_changes_col_is_json? ? changes_for_paper_trail : - PaperTrail.serializer.dump(changes_for_paper_trail) + data[:object_changes] = pt_recordable_object_changes end if self.class.paper_trail_version_class.column_names.include?('transaction_id') data[:transaction_id] = PaperTrail.transaction_id @@ -393,8 +392,7 @@ module PaperTrail data[PaperTrail.timestamp_field] = updated_at end if paper_trail_options[:save_changes] && self.class.paper_trail_version_class.column_names.include?('object_changes') - data[:object_changes] = self.class.paper_trail_version_class.object_changes_col_is_json? ? changes_for_paper_trail : - PaperTrail.serializer.dump(changes_for_paper_trail) + data[:object_changes] = pt_recordable_object_changes end if self.class.paper_trail_version_class.column_names.include?('transaction_id') data[:transaction_id] = PaperTrail.transaction_id @@ -420,6 +418,20 @@ module PaperTrail end end + # Returns an object which can be assigned to the `object_changes` + # attribute of a nascent version record. If the `object_changes` column is + # a postgres `json` column, then a hash can be used in the assignment, + # otherwise the column is a `text` column, and we must perform the + # serialization here, using `PaperTrail.serializer`. + # @api private + def pt_recordable_object_changes + if self.class.paper_trail_version_class.object_changes_col_is_json? + changes_for_paper_trail + else + PaperTrail.serializer.dump(changes_for_paper_trail) + end + end + def changes_for_paper_trail _changes = changes.delete_if { |k,v| !notably_changed.include?(k) } if PaperTrail.serialized_attributes?