Skip to content

Instantly share code, notes, and snippets.

@forest
Created February 16, 2016 18:30
Show Gist options
  • Save forest/40d3244acb00bb7a0322 to your computer and use it in GitHub Desktop.
Save forest/40d3244acb00bb7a0322 to your computer and use it in GitHub Desktop.
diff --git i/lib/virtus/dirty_attribute.rb w/lib/virtus/dirty_attribute.rb
index 3854a36..13c20a3 100644
--- i/lib/virtus/dirty_attribute.rb
+++ w/lib/virtus/dirty_attribute.rb
@@ -16,7 +16,7 @@ module Virtus
def _create_writer_with_dirty_tracking(name)
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{name}=(new_regular_object)
- prev_object = #{name}
+ prev_object = instance_variable_get(:@#{name})
new_virtus_object = super
if attribute_dirty?(:#{name}) && original_attributes[:#{name}] == new_virtus_object
diff --git i/lib/virtus/dirty_attribute/session.rb w/lib/virtus/dirty_attribute/session.rb
index 29798ad..d00d1ae 100644
--- i/lib/virtus/dirty_attribute/session.rb
+++ w/lib/virtus/dirty_attribute/session.rb
@@ -8,7 +8,7 @@ module Virtus
end
def original_attributes
- @_original_attributes ||= subject.attributes.dup
+ @_original_attributes ||= get_original_attributes(subject).dup
end
def dirty_attributes
@@ -32,6 +32,15 @@ module Virtus
original_attributes.clear
dirty_attributes.clear
end
+
+ private
+
+ def get_original_attributes(subject)
+ subject.class.attribute_set.each_with_object({}) do |attribute, attributes|
+ name = attribute.name
+ attributes[name] = subject.instance_variable_get("@#{name}")
+ end
+ end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment