Module: Neo4j::Rails::Versioning

Extended by:
ActiveSupport::Concern
Defined in:
lib/neo4j/rails/versioning/versioning.rb

Overview

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

Defined Under Namespace

Modules: ClassMethods Classes: Snapshot, Version

Instance Method Summary (collapse)

Instance Method Details

- (Object) current_version

Returns the current version of a model instance



74
75
76
# File 'lib/neo4j/rails/versioning/versioning.rb', line 74

def current_version
  self._version ||= 0
end

- (Object) revert_to(version_number)

Reverts this instance to a specified version Reverting the instance will increment the current version number.

Parameters:

  • version_number (Integer)

    The version number to revert to.



102
103
104
105
106
107
108
109
110
# 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? && k != '_neo_id'}
  Neo4j::Transaction.run do
    restore_relationships(snapshot)
    save
  end
end

- (Object) save

Overrides Rails's save method to save snapshots.



90
91
92
93
94
95
96
# 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

- (Object) version(number)

Returns the snapshot version for a given instance. Returns nil in case a version is not found.

Parameters:

  • number (Integer)

    The version number to retrieve.



82
83
84
85
86
# 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.wrapper}
  snapshot.props.each_pair{|k,v| snapshot.assign(k,Neo4j::TypeConverters.to_ruby(self.class, k, v))} if !snapshot.nil?
  snapshot
end