neo4j 1.0.0.beta.5 → 1.0.0.beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,21 +24,11 @@ class Neo4j::Model
24
24
  end
25
25
 
26
26
  def init_on_create(*args) # :nodoc:
27
- if Neo4j::Rails::Transaction.running?
28
- super()
29
- init_on_create_in_tx(*args)
30
- else
31
- Neo4j::Rails::Transaction.run { super(); init_on_create_in_tx(*args) }
32
- end
27
+ super()
28
+ self.attributes=args[0] if args[0].respond_to?(:each_pair)
29
+ @_created_record = true
33
30
  end
34
31
 
35
- def init_on_create_in_tx(*args)
36
- _run_save_callbacks do
37
- _run_create_callbacks do
38
- self.attributes=args[0] if args[0].respond_to?(:each_pair)
39
- end
40
- end
41
- end
42
32
  # --------------------------------------
43
33
  # Identity
44
34
  # --------------------------------------
@@ -48,13 +38,13 @@ class Neo4j::Model
48
38
  end
49
39
 
50
40
  def to_param
51
- persisted? ? neo_id.to_s : nil
41
+ persisted? ? neo_id.to_s : nil
52
42
  end
53
43
 
54
44
  # Returns an Enumerable of all (primary) key attributes
55
45
  # or nil if model.persisted? is false
56
46
  def to_key
57
- persisted? ? [:id] : nil
47
+ persisted? ? [:id] : nil
58
48
  end
59
49
 
60
50
 
@@ -73,9 +63,8 @@ class Neo4j::Model
73
63
  unless key[0] == ?_
74
64
  old_value = self.send(:[], key)
75
65
  attribute_will_change!(key) unless old_value == new_value
76
- #changed_attributes[key] = new_value unless old_value == new_value
77
66
  end
78
- super
67
+ Neo4j::Rails::Transaction.running? ? super : Neo4j::Rails::Transaction.run { super }
79
68
  end
80
69
 
81
70
  def attribute_will_change!(attr)
@@ -89,7 +78,7 @@ class Neo4j::Model
89
78
 
90
79
 
91
80
  def read_attribute_for_validation(key)
92
- self[key]
81
+ respond_to?(key)? send(key) : self[key]
93
82
  end
94
83
 
95
84
  def attributes=(values)
@@ -107,7 +96,7 @@ class Neo4j::Model
107
96
  # If the saving fails because of a connection or remote service error, an exception will be raised.
108
97
  # If saving fails because the resource is invalid then false will be returned.
109
98
  def update_attributes(attributes)
110
- Neo4j::Rails::Transaction.running? ? update_attributes_in_tx(attributes): Neo4j::Rails::Transaction.run { update_attributes_in_tx(attributes) }
99
+ Neo4j::Rails::Transaction.running? ? update_attributes_in_tx(attributes) : Neo4j::Rails::Transaction.run { update_attributes_in_tx(attributes) }
111
100
  end
112
101
 
113
102
  def update_attributes_in_tx(attributes)
@@ -116,7 +105,7 @@ class Neo4j::Model
116
105
  end
117
106
 
118
107
  def update_attributes!(attributes)
119
- Neo4j::Rails::Transaction.running? ? update_attributes_in_tx!(attributes): Neo4j::Rails::Transaction.run { update_attributes_in_tx!(attributes) }
108
+ Neo4j::Rails::Transaction.running? ? update_attributes_in_tx!(attributes) : Neo4j::Rails::Transaction.run { update_attributes_in_tx!(attributes) }
120
109
  end
121
110
 
122
111
  def update_attributes_in_tx!(attributes)
@@ -127,57 +116,64 @@ class Neo4j::Model
127
116
  def delete
128
117
  super
129
118
  @_deleted = true
119
+ @_persisted = false
130
120
  end
131
121
 
132
122
  def save
133
123
  if valid?
134
124
  # if we are trying to save a value then we should create a real node
135
- Neo4j::Rails::Transaction.running? ? save_in_tx : Neo4j::Rails::Transaction.run { save_in_tx }
125
+ if Neo4j::Rails::Transaction.running?
126
+ _run_save_callbacks { save_in_tx }
127
+ else
128
+ Neo4j::Rails::Transaction.run { _run_save_callbacks { save_in_tx } }
129
+ end
130
+ @_created_record = false
136
131
  true
137
132
  else
138
- # if not valid we should rollback the transaction if there is one
139
- # so that the changes does not take place.
140
- # no point failing the transaction if we have not already persisted it since it will then
141
- # not be persisted
142
- Neo4j::Rails::Transaction.fail if Neo4j::Rails::Transaction.running? && persisted?
133
+ # if not valid we should rollback the transaction so that the changes does not take place.
134
+ # no point failing the transaction if we have created a model with 'new'
135
+ Neo4j::Rails::Transaction.fail if Neo4j::Rails::Transaction.running? && !_java_node.kind_of?(Neo4j::Value)
143
136
  false
144
137
  end
145
138
  end
146
139
 
147
140
  def save_in_tx
148
- if persisted?
149
- # already existing node - so we are updating it
150
- _run_update_callbacks do
151
- @previously_changed = changes
152
- @changed_attributes.clear
153
- end
154
- else
155
- # we are creating a new node
141
+ # _run_save_callbacks do
142
+ if _java_node.kind_of?(Neo4j::Value)
156
143
  node = Neo4j::Node.new(props)
157
144
  init_on_load(node)
158
145
  init_on_create
159
- @previously_changed = changes
160
- @changed_attributes.clear
161
146
  end
162
147
 
148
+ if new_record?
149
+ _run_create_callbacks { clear_changes }
150
+ else
151
+ _run_update_callbacks { clear_changes }
152
+ end
153
+ end
154
+
155
+ def clear_changes
156
+ @previously_changed = changes
157
+ @changed_attributes.clear
163
158
  end
164
159
 
165
160
  def save!
166
- raise RecordInvalidError.new(self) unless save
161
+ raise RecordInvalidError.new(self) unless save
167
162
  end
168
163
 
169
- # In neo4j all object are automatically persisted in the database when created (but the Transaction might get rollback)
170
- # Only the Neo4j::Value object will never exist in the database
164
+ # Returns if the record is persisted, i.e. it’s not a new record and it was not destroyed
171
165
  def persisted?
172
- !_java_node.kind_of?(Neo4j::Value)
166
+ !new_record? && !destroyed?
173
167
  end
174
168
 
175
169
  def to_model
176
170
  self
177
171
  end
178
172
 
173
+ # Returns true if this object hasn’t been saved yet — that is, a record for the object doesn’t exist yet; otherwise, returns false.
179
174
  def new_record?()
180
- _java_node.kind_of?(Neo4j::Value)
175
+ # it is new if the model has been created with either the new or create method
176
+ _java_node.kind_of?(Neo4j::Value) || @_created_record == true
181
177
  end
182
178
 
183
179
  def del
@@ -186,7 +182,7 @@ class Neo4j::Model
186
182
  end
187
183
 
188
184
  def destroy
189
- Neo4j::Rails::Transaction.running? ? _run_update_callbacks { del } : Neo4j::Rails::Transaction.run { _run_update_callbacks { del } }
185
+ Neo4j::Rails::Transaction.running? ? _run_update_callbacks { del } : Neo4j::Rails::Transaction.run { _run_update_callbacks { del } }
190
186
  end
191
187
 
192
188
  def destroyed?()
@@ -231,13 +227,24 @@ class Neo4j::Model
231
227
  end
232
228
  end
233
229
 
230
+
231
+ alias_method :_orig_create, :create
232
+
234
233
  def create(*)
235
- Neo4j::Rails::Transaction.running? ? super : Neo4j::Rails::Transaction.run { super }
234
+ Neo4j::Rails::Transaction.running? ? create_in_tx(super) : Neo4j::Rails::Transaction.run { create_in_tx(super) }
236
235
  end
237
236
 
238
237
  def create!(*args)
239
- model = create(*args)
240
- raise RecordInvalidError.new(model) unless model.valid?
238
+ Neo4j::Rails::Transaction.running? ? create_in_tx!(_orig_create(*args)) : Neo4j::Rails::Transaction.run { create_in_tx!(_orig_create(*args)) }
239
+ end
240
+
241
+ def create_in_tx(model)
242
+ model.save
243
+ model
244
+ end
245
+
246
+ def create_in_tx!(model)
247
+ model.save!
241
248
  model
242
249
  end
243
250
 
@@ -39,7 +39,7 @@ module Neo4j
39
39
 
40
40
  def finish
41
41
  tx = Thread.current[:neo4j_transaction]
42
- tx.success unless Thread.current[:neo4j_transaction_fail]
42
+ tx.success unless fail?
43
43
  tx.finish
44
44
  Thread.current[:neo4j_transaction] = nil
45
45
  Thread.current[:neo4j_transaction_fail] = nil
data/lib/neo4j/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Neo4j
2
- VERSION = "1.0.0.beta.5"
2
+ VERSION = "1.0.0.beta.6"
3
3
  end
metadata CHANGED
@@ -7,8 +7,8 @@ version: !ruby/object:Gem::Version
7
7
  - 0
8
8
  - 0
9
9
  - beta
10
- - 5
11
- version: 1.0.0.beta.5
10
+ - 6
11
+ version: 1.0.0.beta.6
12
12
  platform: ruby
13
13
  authors:
14
14
  - Andreas Ronge