neo4j 1.2.5-java → 1.2.6-java
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +8 -0
- data/Gemfile +1 -1
- data/lib/generators/neo4j.rb +47 -39
- data/lib/generators/neo4j/model/model_generator.rb +45 -2
- data/lib/generators/neo4j/model/templates/model.erb +5 -1
- data/lib/neo4j.rb +2 -4
- data/lib/neo4j/index/indexer.rb +4 -3
- data/lib/neo4j/paginate.rb +25 -0
- data/lib/neo4j/rails/model.rb +1 -1
- data/lib/neo4j/rails/rails.rb +1 -0
- data/lib/neo4j/rails/relationship.rb +5 -1
- data/lib/neo4j/rails/relationships/node_dsl.rb +1 -0
- data/lib/neo4j/rails/relationships/relationships.rb +7 -1
- data/lib/neo4j/rails/relationships/rels_dsl.rb +1 -0
- data/lib/neo4j/rails/relationships/storage.rb +31 -14
- data/lib/neo4j/rails/timestamps.rb +8 -10
- data/lib/neo4j/rails/versioning/versioning.rb +131 -0
- data/lib/neo4j/traversal/traverser.rb +1 -17
- data/lib/neo4j/version.rb +1 -1
- metadata +4 -2
data/CHANGELOG
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
== 1.2.6 / 2011-11-02
|
2
|
+
* Generators can now generate relationships as well [#195]
|
3
|
+
* Better will_paginate support for Neo4j::Rails::Model [#194]
|
4
|
+
* Fixing updated_at to be set only if model has changed (pull #68, Deepak N)
|
5
|
+
* Bringing back changes removed during identiy map to fix bug [#190] (Deepak N)
|
6
|
+
* Fixing updated_at to be set only if model has changed, using callbacks instead of overriding method for stamping time (Deepak N)
|
7
|
+
* Added versioning support (pull #67) (Vivek Prahlad)
|
8
|
+
|
1
9
|
== 1.2.5 / 2011-10-21
|
2
10
|
* Faster traversals by avoiding loading Ruby wrappers (new method 'raw' on traversals) [#189]
|
3
11
|
* Support for IdentityMap [#188]
|
data/Gemfile
CHANGED
@@ -6,7 +6,7 @@ group 'test' do
|
|
6
6
|
gem "rake", ">= 0.8.7"
|
7
7
|
gem "rdoc", ">= 2.5.10"
|
8
8
|
gem "horo", ">= 1.0.2"
|
9
|
-
gem "rspec", "
|
9
|
+
gem "rspec", "= 2.6.0"
|
10
10
|
|
11
11
|
# use this version for rspec-rails-matchers which work with latest RSpec (Rspec => RSpec)
|
12
12
|
gem "rspec-rails-matchers", :git => 'git://github.com/afcapel/rspec-rails-matchers.git'
|
data/lib/generators/neo4j.rb
CHANGED
@@ -2,63 +2,71 @@ require 'rails/generators/named_base'
|
|
2
2
|
require 'rails/generators/active_model'
|
3
3
|
|
4
4
|
module Neo4j
|
5
|
-
|
6
|
-
|
5
|
+
module Generators #:nodoc:
|
6
|
+
end
|
7
7
|
end
|
8
8
|
|
9
9
|
class Neo4j::Generators::Base < Rails::Generators::NamedBase #:nodoc:
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
def self.source_root
|
11
|
+
@_neo4j_source_root ||= File.expand_path(File.join(File.dirname(__FILE__),
|
12
|
+
'neo4j', generator_name, 'templates'))
|
13
|
+
end
|
14
14
|
end
|
15
15
|
|
16
16
|
class Neo4j::Generators::ActiveModel < Rails::Generators::ActiveModel #:nodoc:
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
def self.all(klass)
|
18
|
+
"#{klass}.all"
|
19
|
+
end
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
def self.find(klass, params=nil)
|
22
|
+
"#{klass}.find(#{params})"
|
23
|
+
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
25
|
+
def self.build(klass, params=nil)
|
26
|
+
if params
|
27
|
+
"#{klass}.new(#{params})"
|
28
|
+
else
|
29
|
+
"#{klass}.new"
|
30
|
+
end
|
31
|
+
end
|
32
32
|
|
33
|
-
|
34
|
-
|
35
|
-
|
33
|
+
def save
|
34
|
+
"#{name}.save"
|
35
|
+
end
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
37
|
+
def update_attributes(params=nil)
|
38
|
+
"#{name}.update_attributes(#{params})"
|
39
|
+
end
|
40
40
|
|
41
|
-
|
42
|
-
|
43
|
-
|
41
|
+
def errors
|
42
|
+
"#{name}.errors"
|
43
|
+
end
|
44
44
|
|
45
|
-
|
46
|
-
|
47
|
-
|
45
|
+
def destroy
|
46
|
+
"#{name}.destroy"
|
47
|
+
end
|
48
48
|
end
|
49
49
|
|
50
50
|
module Rails
|
51
51
|
module Generators
|
52
52
|
class GeneratedAttribute #:nodoc:
|
53
53
|
def type_class
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
54
|
+
case type.to_s.downcase
|
55
|
+
when 'any' then
|
56
|
+
'any'
|
57
|
+
when 'datetime' then
|
58
|
+
'DateTime'
|
59
|
+
when 'date' then
|
60
|
+
'Date'
|
61
|
+
when 'text' then
|
62
|
+
'String'
|
63
|
+
when 'integer', 'number', 'fixnum' then
|
64
|
+
'Fixnum'
|
65
|
+
when 'float' then
|
66
|
+
'Float'
|
67
|
+
else
|
68
|
+
'String'
|
69
|
+
end
|
62
70
|
end
|
63
71
|
end
|
64
72
|
end
|
@@ -7,7 +7,10 @@ class Neo4j::Generators::ModelGenerator < Neo4j::Generators::Base #:nodoc:
|
|
7
7
|
|
8
8
|
class_option :timestamps, :type => :boolean
|
9
9
|
class_option :parent, :type => :string, :desc => "The parent class for the generated model"
|
10
|
-
|
10
|
+
class_option :indices, :type => :array, :desc => "The properties which should be indexed"
|
11
|
+
class_option :has_one, :type => :array, :desc => "A list of has_one relationships"
|
12
|
+
class_option :has_n, :type => :array, :desc => "A list of has_n relationships"
|
13
|
+
|
11
14
|
def create_model_file
|
12
15
|
template "model.erb", File.join('app/models', "#{singular_name}.rb")
|
13
16
|
end
|
@@ -20,7 +23,47 @@ class Neo4j::Generators::ModelGenerator < Neo4j::Generators::Base #:nodoc:
|
|
20
23
|
def timestamps?
|
21
24
|
options[:timestamps]
|
22
25
|
end
|
23
|
-
|
26
|
+
|
27
|
+
def has_n?
|
28
|
+
options[:has_n]
|
29
|
+
end
|
30
|
+
|
31
|
+
def has_n_statements
|
32
|
+
txt = ""
|
33
|
+
options[:has_n].each do |key|
|
34
|
+
to, from = key.split(':')
|
35
|
+
txt << (from ? "\n has_n(:#{to}).from(:#{from})\n" : "\n has_n :#{to}")
|
36
|
+
end
|
37
|
+
txt
|
38
|
+
end
|
39
|
+
|
40
|
+
def has_one?
|
41
|
+
options[:has_one]
|
42
|
+
end
|
43
|
+
|
44
|
+
def has_one_statements
|
45
|
+
txt = ""
|
46
|
+
options[:has_one].each do |key|
|
47
|
+
to, from = key.split(':')
|
48
|
+
txt << (from ? "\n has_one(:#{to}).from(:#{from})\n" : "\n has_one :#{to}")
|
49
|
+
end
|
50
|
+
txt
|
51
|
+
end
|
52
|
+
|
53
|
+
def indices?
|
54
|
+
options[:indices]
|
55
|
+
end
|
56
|
+
|
57
|
+
def indices_statements
|
58
|
+
puts "indices_statements #{options[:indices].inspect}"
|
59
|
+
txt = ""
|
60
|
+
options[:indices].each do |key|
|
61
|
+
txt << %Q{
|
62
|
+
index :#{key}}
|
63
|
+
end
|
64
|
+
txt
|
65
|
+
end
|
66
|
+
|
24
67
|
def parent?
|
25
68
|
options[:parent]
|
26
69
|
end
|
@@ -1,7 +1,11 @@
|
|
1
1
|
class <%= class_name %> < <%= parent? ? options[:parent].classify : "Neo4j::Rails::Model" %>
|
2
2
|
<% attributes.each do |attribute| -%>
|
3
|
-
property :<%= attribute.name %><%= ", :type => #{attribute.type_class}" %>
|
3
|
+
property :<%= attribute.name %><%= ", :type => #{attribute.type_class}" unless attribute.type_class == 'any' %>
|
4
4
|
<% end -%>
|
5
|
+
<%= indices_statements if indices? -%>
|
6
|
+
|
7
|
+
<%= has_n_statements if has_n? -%>
|
8
|
+
<%= has_one_statements if has_one? -%>
|
5
9
|
|
6
10
|
<%= timestamp_statements if timestamps? -%>
|
7
11
|
end
|
data/lib/neo4j.rb
CHANGED
@@ -70,6 +70,7 @@ end
|
|
70
70
|
|
71
71
|
require 'neo4j/version'
|
72
72
|
require 'neo4j/neo4j'
|
73
|
+
require 'neo4j/paginate'
|
73
74
|
require 'neo4j/node'
|
74
75
|
require 'neo4j/relationship'
|
75
76
|
require 'neo4j/relationship_set'
|
@@ -104,7 +105,4 @@ require 'neo4j/batch/batch'
|
|
104
105
|
|
105
106
|
require 'orm_adapter/adapters/neo4j'
|
106
107
|
|
107
|
-
require 'neo4j/identity_map'
|
108
|
-
|
109
|
-
|
110
|
-
|
108
|
+
require 'neo4j/identity_map'
|
data/lib/neo4j/index/indexer.rb
CHANGED
@@ -330,9 +330,10 @@ module Neo4j
|
|
330
330
|
def index_prefix
|
331
331
|
return "" unless Neo4j.running?
|
332
332
|
return "" unless @indexer_for.respond_to?(:ref_node_for_class)
|
333
|
-
ref_node = @indexer_for.ref_node_for_class
|
334
|
-
|
335
|
-
|
333
|
+
ref_node = @indexer_for.ref_node_for_class.wrapper
|
334
|
+
prefix = ref_node.send(:_index_prefix) if ref_node.respond_to?(:_index_prefix)
|
335
|
+
prefix ||= ref_node[:name] # To maintain backward compatiblity
|
336
|
+
prefix.blank? ? "" : prefix + "_"
|
336
337
|
end
|
337
338
|
end
|
338
339
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Neo4j
|
2
|
+
module Paginate
|
3
|
+
def self.included(base)
|
4
|
+
base.send(:include, WillPaginate::Finders::Base)
|
5
|
+
end
|
6
|
+
|
7
|
+
|
8
|
+
def wp_query(options, pager, args, &block) #:nodoc:
|
9
|
+
page = pager.current_page || 1
|
10
|
+
per_page = pager.per_page
|
11
|
+
to = per_page * page
|
12
|
+
from = to - per_page
|
13
|
+
i = 0
|
14
|
+
res = []
|
15
|
+
each do |node|
|
16
|
+
res << node.wrapper if i >= from
|
17
|
+
i += 1
|
18
|
+
break if i >= to
|
19
|
+
end
|
20
|
+
pager.replace res
|
21
|
+
pager.total_entries ||= count
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
data/lib/neo4j/rails/model.rb
CHANGED
@@ -262,9 +262,9 @@ module Neo4j
|
|
262
262
|
include Attributes # handles how to save and retrieve attributes
|
263
263
|
include Mapping::Property # allows some additional options on the #property class method
|
264
264
|
include Serialization # enable to_xml and to_json
|
265
|
-
include Timestamps # handle created_at, updated_at timestamp properties
|
266
265
|
include Validations # enable validations
|
267
266
|
include Callbacks # enable callbacks
|
267
|
+
include Timestamps # handle created_at, updated_at timestamp properties
|
268
268
|
include ActiveModel::Observing # enable observers
|
269
269
|
include Finders # ActiveRecord style find
|
270
270
|
include Relationships # for none persisted relationships
|
data/lib/neo4j/rails/rails.rb
CHANGED
@@ -88,6 +88,10 @@ module Neo4j
|
|
88
88
|
@properties = {}
|
89
89
|
end
|
90
90
|
|
91
|
+
def wrapper
|
92
|
+
self
|
93
|
+
end
|
94
|
+
|
91
95
|
# --------------------------------------
|
92
96
|
# Public Class Methods
|
93
97
|
# --------------------------------------
|
@@ -128,9 +132,9 @@ module Neo4j
|
|
128
132
|
include Attributes # handles how to save and retrieve attributes
|
129
133
|
include Mapping::Property # allows some additional options on the #property class method
|
130
134
|
include Serialization # enable to_xml and to_json
|
131
|
-
include Timestamps # handle created_at, updated_at timestamp properties
|
132
135
|
include Validations # enable validations
|
133
136
|
include Callbacks # enable callbacks
|
137
|
+
include Timestamps # handle created_at, updated_at timestamp properties
|
134
138
|
include Finders # ActiveRecord style find
|
135
139
|
include Compositions
|
136
140
|
end
|
@@ -2,13 +2,19 @@ module Neo4j
|
|
2
2
|
module Rails
|
3
3
|
module Relationships
|
4
4
|
|
5
|
-
|
6
5
|
def write_changed_relationships #:nodoc:
|
7
6
|
@_relationships.each_value do |storage|
|
8
7
|
storage.persist
|
9
8
|
end
|
10
9
|
end
|
11
10
|
|
11
|
+
def relationships_changed?
|
12
|
+
@_relationships.each_value do |storage|
|
13
|
+
return true if !storage.persisted?
|
14
|
+
end
|
15
|
+
false
|
16
|
+
end
|
17
|
+
|
12
18
|
def clear_relationships #:nodoc:
|
13
19
|
@_relationships && @_relationships.each_value{|storage| storage.remove_from_identity_map}
|
14
20
|
@_relationships = {}
|
@@ -14,13 +14,15 @@ module Neo4j
|
|
14
14
|
@target_class = (dsl && dsl.target_class) || Neo4j::Rails::Model
|
15
15
|
@outgoing_rels = []
|
16
16
|
@incoming_rels = []
|
17
|
+
@persisted_related_nodes = {}
|
18
|
+
@persisted_relationships = {}
|
19
|
+
@persisted_node_to_relationships = {}
|
17
20
|
end
|
18
21
|
|
19
22
|
def to_s #:nodoc:
|
20
23
|
"Storage #{object_id} node: #{@node.id} rel_type: #{@rel_type} outgoing #{@outgoing_rels.size} incoming #{@incoming_rels.size}"
|
21
24
|
end
|
22
25
|
|
23
|
-
|
24
26
|
def remove_from_identity_map
|
25
27
|
@outgoing_rels.each {|r| Neo4j::IdentityMap.remove(r._java_rel)}
|
26
28
|
@incoming_rels.each {|r| Neo4j::IdentityMap.remove(r._java_rel)}
|
@@ -35,7 +37,6 @@ module Neo4j
|
|
35
37
|
counter
|
36
38
|
end
|
37
39
|
|
38
|
-
|
39
40
|
def build(attrs)
|
40
41
|
@target_class.new(attrs)
|
41
42
|
end
|
@@ -60,14 +61,34 @@ module Neo4j
|
|
60
61
|
end
|
61
62
|
|
62
63
|
def each_rel(dir, &block) #:nodoc:
|
63
|
-
relationships(dir).each { |rel| block.call rel }
|
64
|
+
relationships(dir).each { |rel| block.call rel }
|
64
65
|
if @node.persisted?
|
65
|
-
|
66
|
-
|
67
|
-
|
66
|
+
cache_relationships(dir) if @persisted_relationships[dir].nil?
|
67
|
+
@persisted_relationships[dir].each {|rel| block.call rel unless !rel.exist?}
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def cache_relationships(dir)
|
72
|
+
@persisted_relationships[dir] ||= []
|
73
|
+
node._java_node.getRelationships(java_rel_type, dir_to_java(dir)).each do |rel|
|
74
|
+
@persisted_relationships[dir] << rel.wrapper
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def cache_persisted_nodes_and_relationships(dir)
|
79
|
+
@persisted_related_nodes[dir] ||= []
|
80
|
+
@persisted_node_to_relationships[dir] ||= {}
|
81
|
+
@node._java_node.getRelationships(java_rel_type, dir_to_java(dir)).each do |rel|
|
82
|
+
end_node = rel.getOtherNode(@node._java_node).wrapper
|
83
|
+
@persisted_related_nodes[dir] << end_node
|
84
|
+
@persisted_node_to_relationships[dir][end_node]=rel
|
68
85
|
end
|
69
86
|
end
|
70
|
-
|
87
|
+
|
88
|
+
def relationship_deleted?(dir,node)
|
89
|
+
@persisted_node_to_relationships[dir][node].nil? || !@persisted_node_to_relationships[dir][node].exist?
|
90
|
+
end
|
91
|
+
|
71
92
|
def each_node(dir, &block)
|
72
93
|
relationships(dir).each do |rel|
|
73
94
|
if rel.start_node == @node
|
@@ -76,18 +97,14 @@ module Neo4j
|
|
76
97
|
block.call rel.start_node
|
77
98
|
end
|
78
99
|
end
|
79
|
-
|
80
100
|
if @node.persisted?
|
81
|
-
|
82
|
-
|
83
|
-
block.call(end_node)
|
84
|
-
end
|
101
|
+
cache_persisted_nodes_and_relationships(dir) if @persisted_related_nodes[dir].nil?
|
102
|
+
@persisted_related_nodes[dir].each {|node| block.call node unless relationship_deleted?(dir,node)}
|
85
103
|
end
|
86
104
|
end
|
87
105
|
|
88
106
|
def single_relationship(dir, raw = false)
|
89
107
|
rel = relationships(dir).first
|
90
|
-
# puts "single_relationship #{dir} for #{self}, got #{rel && rel._java_rel} @node.persisted?=#{@node.persisted?}, #{@node._java_node}"
|
91
108
|
if rel.nil? && @node.persisted?
|
92
109
|
java_rel = @node._java_node.getSingleRelationship(java_rel_type, dir_to_java(dir))
|
93
110
|
raw ? java_rel : java_rel && java_rel.wrapper
|
@@ -145,7 +162,7 @@ module Neo4j
|
|
145
162
|
out_rels = @outgoing_rels.clone
|
146
163
|
in_rels = @incoming_rels.clone
|
147
164
|
|
148
|
-
[@outgoing_rels, @incoming_rels].each{|c| c.clear}
|
165
|
+
[@outgoing_rels, @incoming_rels, @persisted_related_nodes, @persisted_node_to_relationships, @persisted_relationships].each{|c| c.clear}
|
149
166
|
|
150
167
|
out_rels.each do |rel|
|
151
168
|
rel.end_node.rm_incoming_rel(@rel_type.to_sym, rel) if rel.end_node
|
@@ -6,16 +6,10 @@ module Neo4j
|
|
6
6
|
|
7
7
|
TIMESTAMP_PROPERTIES = [ :created_at, :created_on, :updated_at, :updated_on ]
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
def init_on_create(*args)
|
15
|
-
create_timestamp
|
16
|
-
super
|
17
|
-
end
|
18
|
-
|
9
|
+
included do
|
10
|
+
before_create :create_timestamp
|
11
|
+
before_save :update_timestamp, :if => :new_or_changed?
|
12
|
+
end
|
19
13
|
# Set the timestamps for this model if timestamps is set to true in the config
|
20
14
|
# and the model is set up with the correct property name, e.g.:
|
21
15
|
#
|
@@ -50,6 +44,10 @@ module Neo4j
|
|
50
44
|
send("#{attribute}=", value)
|
51
45
|
end
|
52
46
|
|
47
|
+
def new_or_changed?
|
48
|
+
self.new? or self.changed?
|
49
|
+
end
|
50
|
+
|
53
51
|
module ClassMethods
|
54
52
|
def property_setup(property, options)
|
55
53
|
super
|
@@ -0,0 +1,131 @@
|
|
1
|
+
module Neo4j
|
2
|
+
module Rails
|
3
|
+
# Adds snapshot based versioning to Neo4j Rails models
|
4
|
+
# To use versioning, include this module in your model.
|
5
|
+
#
|
6
|
+
# Example:
|
7
|
+
# class VersionableModel < Neo4j::Rails::Model
|
8
|
+
# include Neo4j::Rails::Versioning
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
# To find out the number of versions of an instance, you can use the current_version method.
|
12
|
+
#
|
13
|
+
# To retrieve a snapshot of an older version, use the version method.
|
14
|
+
# snapshot = instance.version(1) #Retrieves version 1.
|
15
|
+
#
|
16
|
+
# Note that the version numbers start from 1 onwards.
|
17
|
+
#
|
18
|
+
# The snapshot retains all the properties and relationships at the point when a versioned model is saved.
|
19
|
+
# The snapshot also allows you to traverse incoming and outgoing relationships.
|
20
|
+
#
|
21
|
+
# For example:
|
22
|
+
# snapshot.incoming(:friends) would return a collection of nodes that are related via the friends relationship.
|
23
|
+
#
|
24
|
+
# The snapshot node creates relationships with a model's related nodes with a "version_" prefix in order to
|
25
|
+
# avoid name clashes. However, you can call the incoming and outgoing methods using your model's relationship names.
|
26
|
+
#
|
27
|
+
# To control the maximum number of versions created, you can use the max_versions property.
|
28
|
+
#
|
29
|
+
# Example:
|
30
|
+
# class MaxVersionableModel < Neo4j::Rails::Model
|
31
|
+
# include Neo4j::Rails::Versioning
|
32
|
+
# max_versions 10
|
33
|
+
# end
|
34
|
+
module Versioning
|
35
|
+
extend ActiveSupport::Concern
|
36
|
+
|
37
|
+
class Version
|
38
|
+
include Neo4j::RelationshipMixin
|
39
|
+
property :number, :type => Fixnum
|
40
|
+
property :instance_id, :type => Fixnum
|
41
|
+
property :model_classname
|
42
|
+
index :number, :instance_id, :model_classname
|
43
|
+
end
|
44
|
+
|
45
|
+
class Snapshot
|
46
|
+
include Neo4j::NodeMixin
|
47
|
+
|
48
|
+
def incoming(rel_type)
|
49
|
+
super "version_#{rel_type.to_s}".to_sym
|
50
|
+
end
|
51
|
+
|
52
|
+
def outgoing(rel_type)
|
53
|
+
super "version_#{rel_type.to_s}".to_sym
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
included do
|
58
|
+
class_attribute :version_max
|
59
|
+
property :_version, :type => Fixnum
|
60
|
+
end
|
61
|
+
|
62
|
+
##
|
63
|
+
# Returns the current version of a model instance
|
64
|
+
def current_version
|
65
|
+
self._version ||= 0
|
66
|
+
end
|
67
|
+
|
68
|
+
##
|
69
|
+
# Returns the snapshot version for a given instance.
|
70
|
+
# @param [ Integer ] number The version number to retrieve.
|
71
|
+
# Returns nil in case a version is not found.
|
72
|
+
def version(number)
|
73
|
+
Version.find(:model_classname => _classname, :instance_id => neo_id, :number => number) {|query| query.first.nil? ? nil : query.first.end_node}
|
74
|
+
end
|
75
|
+
|
76
|
+
##
|
77
|
+
# Overrides Rails's save method to save snapshots.
|
78
|
+
def save
|
79
|
+
if self.changed? || self.relationships_changed?
|
80
|
+
self._version = current_version + 1
|
81
|
+
super
|
82
|
+
revise
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
private
|
87
|
+
def revise
|
88
|
+
Neo4j::Transaction.run do
|
89
|
+
snapshot = Snapshot.new(self.props.reject{|key, value| key.to_sym == :_classname})
|
90
|
+
version_relationships(snapshot)
|
91
|
+
delete_old_version if version_max.present? && number_of_versions >= version_max
|
92
|
+
Version.new(:version, self, snapshot, :model_classname => _classname, :instance_id => neo_id, :number => current_version)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def number_of_versions
|
97
|
+
Version.find(:model_classname => _classname, :instance_id => neo_id) {|query| query.size}
|
98
|
+
end
|
99
|
+
|
100
|
+
def version_relationships(snapshot)
|
101
|
+
self._java_node.getRelationships().each do |rel|
|
102
|
+
if (self._java_node == rel.getStartNode())
|
103
|
+
snapshot._java_node.createRelationshipTo(rel.getEndNode(), relationship_type(rel.getType()))
|
104
|
+
else
|
105
|
+
rel.getStartNode().createRelationshipTo(snapshot._java_node, relationship_type(rel.getType()))
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def relationship_type(rel_type)
|
111
|
+
org.neo4j.graphdb.DynamicRelationshipType.withName( "version_#{rel_type.name}" )
|
112
|
+
end
|
113
|
+
|
114
|
+
def delete_old_version
|
115
|
+
versions = Version.find(:model_classname => _classname).asc(:number)
|
116
|
+
versions.first.del
|
117
|
+
versions.close
|
118
|
+
end
|
119
|
+
|
120
|
+
module ClassMethods #:nodoc:
|
121
|
+
|
122
|
+
# Sets the maximum number of versions to store.
|
123
|
+
#
|
124
|
+
# @param [ Integer ] number The maximum number of versions to store.
|
125
|
+
def max_versions(number)
|
126
|
+
self.version_max = number.to_i
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
@@ -31,7 +31,7 @@ module Neo4j
|
|
31
31
|
class Traverser
|
32
32
|
include Enumerable
|
33
33
|
include ToJava
|
34
|
-
include
|
34
|
+
include Neo4j::Paginate
|
35
35
|
|
36
36
|
|
37
37
|
def initialize(from, type = nil, dir=nil)
|
@@ -85,22 +85,6 @@ module Neo4j
|
|
85
85
|
end
|
86
86
|
|
87
87
|
|
88
|
-
def wp_query(options, pager, args, &block) #:nodoc:
|
89
|
-
page = pager.current_page || 1
|
90
|
-
per_page = pager.per_page
|
91
|
-
to = per_page * page
|
92
|
-
from = to - per_page
|
93
|
-
i = 0
|
94
|
-
res = []
|
95
|
-
iterator.each do |node|
|
96
|
-
res << node.wrapper if i >= from
|
97
|
-
i += 1
|
98
|
-
break if i >= to
|
99
|
-
end
|
100
|
-
pager.replace res
|
101
|
-
pager.total_entries ||= count
|
102
|
-
end
|
103
|
-
|
104
88
|
def <<(other_node)
|
105
89
|
new(other_node)
|
106
90
|
self
|
data/lib/neo4j/version.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: neo4j
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.2.
|
5
|
+
version: 1.2.6
|
6
6
|
platform: java
|
7
7
|
authors:
|
8
8
|
- Andreas Ronge
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-11-03 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -92,6 +92,7 @@ files:
|
|
92
92
|
- lib/neo4j/neo4j.rb
|
93
93
|
- lib/neo4j/node.rb
|
94
94
|
- lib/neo4j/load.rb
|
95
|
+
- lib/neo4j/paginate.rb
|
95
96
|
- lib/neo4j/database.rb
|
96
97
|
- lib/neo4j/relationship_set.rb
|
97
98
|
- lib/neo4j/version.rb
|
@@ -143,6 +144,7 @@ files:
|
|
143
144
|
- lib/neo4j/rails/relationships/node_dsl.rb
|
144
145
|
- lib/neo4j/rails/relationships/relationships.rb
|
145
146
|
- lib/neo4j/rails/relationships/storage.rb
|
147
|
+
- lib/neo4j/rails/versioning/versioning.rb
|
146
148
|
- lib/neo4j/rails/mapping/property.rb
|
147
149
|
- lib/neo4j/rails/validations/associated.rb
|
148
150
|
- lib/neo4j/rails/validations/uniqueness.rb
|