Permalink
Browse files

Allow attributes to be ignored when deciding whether to create a version

  • Loading branch information...
1 parent 8c001a3 commit bb7acb7004e7e9dca7271f2e43c49a60e19b2fb9 @devrandom devrandom committed Jan 13, 2012
Showing with 22 additions and 3 deletions.
  1. +22 −3 lib/dm-is-versioned/is/versioned.rb
@@ -47,22 +47,41 @@ module Is
module Versioned
def is_versioned(options = {})
@is_versioned_on = options[:on]
+ @is_versioned_ignore = options[:ignore]
extend(Migration) if respond_to?(:auto_migrate!)
before :save do
if dirty? && !new?
@pending_version_attributes = {}
- original_attributes.each { |k,v| @pending_version_attributes[k.name] = v }
+ original_attributes.each do |k,v|
+ # Skip associations
+ unless k.is_a? DataMapper::Associations::Relationship
+ @pending_version_attributes[k.name] = v
+ end
+ end
else
@pending_version_attributes = nil
end
end
after :save do
- debugger
if clean? && @pending_version_attributes
- model::Version.create!(attributes.merge(@pending_version_attributes))
+ version_attributes = attributes.merge(@pending_version_attributes)
+
+ # Check if we were asked to ignore certain attributes when checking for changes
+ ignore = self.class.instance_variable_get(:@is_versioned_ignore)
+
+ if ignore
+ wanted_keys = attributes.keys - ignore
+ # Compare old and new attributes while ignoring some attributes
+ if version_attributes.values_at(*wanted_keys) != attributes.values_at(*wanted_keys)
+ model::Version.create!(version_attributes)
+ end
+ else
+ # Assume DataMapper already figured out whether anything changed
+ model::Version.create!(version_attributes)
+ end
end
@pending_version_attributes = nil
end

0 comments on commit bb7acb7

Please sign in to comment.