Skip to content

Commit

Permalink
Backport fix for #428 (avoids invoking #dup on model instances); fixes
Browse files Browse the repository at this point in the history
…#404

Conflicts:
	lib/paper_trail/has_paper_trail.rb
  • Loading branch information
batter committed Mar 2, 2015
1 parent 2bb66b1 commit 38ca845
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
17 changes: 6 additions & 11 deletions lib/paper_trail/has_paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,24 +338,19 @@ def merge_metadata(data)
end

def item_before_change
previous = self.dup
# `dup` clears timestamps so we add them back.
all_timestamp_attributes.each do |column|
previous[column] = send(column) if self.class.column_names.include?(column.to_s) and not send(column).nil?
end
enums = previous.respond_to?(:defined_enums) ? previous.defined_enums : {}
previous.tap do |prev|
prev.id = id # `dup` clears the `id` so we add that back
attributes.tap do |prev|
enums = self.respond_to?(:defined_enums) ? self.defined_enums : {}
changed_attributes.select { |k,v| self.class.column_names.include?(k) }.each do |attr, before|
before = enums[attr][before] if enums[attr]
prev[attr] = before
end
end
end

# returns hash of object attributes (with appropriate attributes serialized), ommitting attributes to be skipped
def object_attrs_for_paper_trail(object)
_attrs = object.attributes.except(*self.paper_trail_options[:skip]).tap do |attributes|
# returns hash of attributes (with appropriate attributes serialized),
# ommitting attributes to be skipped
def object_attrs_for_paper_trail(attributes_hash)
attributes_hash.except(*self.paper_trail_options[:skip]).tap do |attributes|
self.class.serialize_attributes_for_paper_trail(attributes)
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/unit/serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SerializerTest < ActiveSupport::TestCase
END

@fluxor = Fluxor.create :name => 'Some text.'
@original_fluxor_attributes = @fluxor.send(:item_before_change).attributes # this is exactly what PaperTrail serializes
@original_fluxor_attributes = @fluxor.send(:item_before_change) # this is exactly what PaperTrail serializes
@fluxor.update_attributes :name => 'Some more text.'
end

Expand Down Expand Up @@ -41,7 +41,7 @@ class SerializerTest < ActiveSupport::TestCase
END

@fluxor = Fluxor.create :name => 'Some text.'
@original_fluxor_attributes = @fluxor.send(:item_before_change).attributes # this is exactly what PaperTrail serializes
@original_fluxor_attributes = @fluxor.send(:item_before_change) # this is exactly what PaperTrail serializes
@fluxor.update_attributes :name => 'Some more text.'
end

Expand Down Expand Up @@ -83,7 +83,7 @@ class SerializerTest < ActiveSupport::TestCase
END

@fluxor = Fluxor.create
@original_fluxor_attributes = @fluxor.send(:item_before_change).attributes.reject { |k,v| v.nil? } # this is exactly what PaperTrail serializes
@original_fluxor_attributes = @fluxor.send(:item_before_change).reject { |k,v| v.nil? } # this is exactly what PaperTrail serializes
@fluxor.update_attributes :name => 'Some more text.'
end

Expand Down

0 comments on commit 38ca845

Please sign in to comment.