Skip to content
This repository
Browse code

Make sure freeze does not have side effects, #235

Needed by carrierwave-neo4j
  • Loading branch information...
commit 2ba75012b736324c54c00c754cdea67e6fa143f7 1 parent a1a2243
Andreas Ronge authored January 05, 2013
3  lib/neo4j/rails/attributes.rb
@@ -231,9 +231,6 @@ def read_attribute(key)
231 231
         if @_properties.has_key?(key)
232 232
           @_properties[key]
233 233
         else
234  
-          #puts "@_properties #{@_properties}"
235  
-          #puts "attribute_defaults #{attribute_defaults.inspect}"
236  
-          #puts "Key #{key}, self #{self}"
237 234
           @_properties[key] = (!new_record? && _java_entity.has_property?(key)) ? read_property_from_db(key) : attribute_defaults[key]
238 235
         end
239 236
       end
9  lib/neo4j/rails/node_persistance.rb
@@ -41,6 +41,15 @@ def reload(options = nil)
41 41
         self
42 42
       end
43 43
 
  44
+      def freeze_if_deleted
  45
+        unless new_record?
  46
+          Neo4j::IdentityMap.remove_node_by_id(neo_id)
  47
+          unless self.class.load_entity(neo_id)
  48
+            set_deleted_properties
  49
+            freeze
  50
+          end
  51
+        end
  52
+      end
44 53
 
45 54
       def reload_from_database
46 55
         Neo4j::IdentityMap.remove_node_by_id(neo_id)
2  lib/neo4j/rails/persistence.rb
@@ -74,7 +74,7 @@ def freeze
74 74
 
75 75
       # Returns +true+ if the properties hash has been frozen.
76 76
       def frozen?
77  
-        reload unless new_record?
  77
+        freeze_if_deleted
78 78
         @_properties.frozen?
79 79
       end
80 80
 
9  lib/neo4j/rails/relationship_persistence.rb
@@ -73,6 +73,15 @@ def reload(options = nil)
73 73
         self
74 74
       end
75 75
 
  76
+      def freeze_if_deleted
  77
+        unless new_record?
  78
+          Neo4j::IdentityMap.remove_rel_by_id(neo_id)
  79
+          unless self.class.load_entity(neo_id)
  80
+            set_deleted_properties
  81
+            freeze
  82
+          end
  83
+        end
  84
+      end
76 85
 
77 86
       def reload_from_database
78 87
         Neo4j::IdentityMap.remove_rel_by_id(id) if persisted?
33  spec/regressions/issue_235_spec.rb
... ...
@@ -0,0 +1,33 @@
  1
+module Regressions
  2
+  require 'spec_helper'
  3
+
  4
+  class Issue235 < Neo4j::Rails::Model
  5
+    property :thing
  6
+  end
  7
+
  8
+  class IssueRel235 < Neo4j::Rails::Relationship
  9
+    property :thing
  10
+  end
  11
+
  12
+  describe "Issue 235, make sure frozen? does not have side effects" do
  13
+    it "works for Neo4j::Rails::Model" do
  14
+      a = Issue235.create(:thing => 2)
  15
+      a.reload
  16
+      a.thing = 42
  17
+      a.frozen?
  18
+      a.thing.should == 42
  19
+    end
  20
+
  21
+    it "works for Neo4j::Rails::Relationship" do
  22
+      a = Issue235.create!
  23
+      b = Issue235.create!
  24
+      r = IssueRel235.create(:friends, a, b, :things => 2)
  25
+
  26
+      r.reload
  27
+      r.thing = 42
  28
+      r.frozen?
  29
+      r.thing.should == 42
  30
+    end
  31
+
  32
+  end
  33
+end
2  spec/unit/rails_relationship_spec.rb
@@ -56,7 +56,7 @@
56 56
     its(:new_record?) { should be_false }
57 57
     describe "frozen?" do
58 58
       it "reloads the node and returns false" do
59  
-        subject.should_receive(:reload).and_return(true)
  59
+        subject.should_receive(:freeze_if_deleted).and_return(true)
60 60
         subject.frozen?.should be_false
61 61
       end
62 62
     end

0 notes on commit 2ba7501

Please sign in to comment.
Something went wrong with that request. Please try again.