Module: DataMapper::Has::Versions::InstanceMethods

Defined in:
lib/dm-has-versions/has/versions.rb

Instance Method Summary collapse

Instance Method Details

#pending_version_attributesHash

Returns a hash of original values to be stored in the versions table when a new version is created. It is cleared after a version model is created.

Returns:

  • (Hash)


64
65
66
# File 'lib/dm-has-versions/has/versions.rb', line 64

def pending_version_attributes
  @pending_version_attributes ||= {}
end

#revert_to(version) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dm-has-versions/has/versions.rb', line 85

def revert_to(version)
  if target = versions.first(:offset => version)
    transaction do
      self.properties.each do |property|
        next if property.key?
        name = property.name
        self.attribute_set(name, target.attribute_get(name))
      end
      pending_version_attributes.clear
      return false unless save
      versions.all(:id.gte => target.id).destroy!
    end
  end
  !!target
end

#versionObject



81
82
83
# File 'lib/dm-has-versions/has/versions.rb', line 81

def version
  versions.size
end

#versionsCollection

Returns a collection of other versions of this resource. The versions are related on the models keys, and ordered by the version field.

Returns:

  • (Collection)


75
76
77
78
79
# File 'lib/dm-has-versions/has/versions.rb', line 75

def versions
  version = self.class.const_get("Version")
  original_key = "#{self.class.storage_name.singular}_id".intern
  version.all(original_key => self.id, :order => [:id.asc])
end