Adds snapshot based versioning to Neo4j Rails models To use versioning, include this module in your model.
Example:
class VersionableModel < Neo4j::Rails::Model include Neo4j::Rails::Versioning end
To find out the number of versions of an instance, you can use the #current_version method.
To retrieve a snapshot of an older version, use the version method.
snapshot = instance.version(1) #Retrieves version 1.
Note that the version numbers start from 1 onwards.
The snapshot retains all the properties and relationships at the point when a versioned model is saved. The snapshot also allows you to traverse incoming and outgoing relationships.
For example:
snapshot.incoming(:friends) would return a collection of nodes that are related via the friends relationship.
The snapshot node creates relationships with a model’s related nodes with a “version_” prefix in order to avoid name clashes. However, you can call the incoming and outgoing methods using your model’s relationship names.
To control the maximum number of versions created, you can use the max_versions property.
Example:
class MaxVersionableModel < Neo4j::Rails::Model include Neo4j::Rails::Versioning max_versions 10 end
Returns the current version of a model instance
# File lib/neo4j/rails/versioning/versioning.rb, line 74 def current_version self._version ||= 0 end
Reverts this instance to a specified version @param [ Integer ] version_number The version number to revert to. Reverting the instance will increment the current version number.
# File lib/neo4j/rails/versioning/versioning.rb, line 102 def revert_to(version_number) snapshot = version(version_number) self.props.each_pair{|k,v| self[k] = nil if !snapshot.props.has_key?(k)} snapshot.props.each_pair{|k,v| self[k] = v if self.props[k].nil?} Neo4j::Transaction.run do restore_relationships(snapshot) save end end
Overrides Rails’s save method to save snapshots.
# File lib/neo4j/rails/versioning/versioning.rb, line 90 def save if self.changed? || self.relationships_changed? self._version = current_version + 1 super revise end end
Returns the snapshot version for a given instance. @param [ Integer ] number The version number to retrieve. Returns nil in case a version is not found.
# File lib/neo4j/rails/versioning/versioning.rb, line 82 def version(number) snapshot = Version.find(:model_classname => _classname, :instance_id => neo_id, :number => number) {|query| query.first.nil? ? nil : query.first.end_node} snapshot.props.each_pair{|k,v| snapshot.assign(k,Neo4j::TypeConverters.to_ruby(self.class, k, v))} if !snapshot.nil? snapshot end