Skip to content
This repository
Browse code

Work around a strange piece of Syck behaviour where it checks Model#r…

…espond_to? before initializing the object.

Things like YAML.load(YAML.dump(@post)) won't work without this.
  • Loading branch information...
commit 96eec090dfd50326146b2f690408fefec50c5111 1 parent 0afebd5
Michael Koziarski authored December 02, 2010 tenderlove committed December 02, 2010
2  activerecord/lib/active_record/attribute_methods.rb
@@ -54,7 +54,7 @@ def respond_to?(*args)
54 54
 
55 55
     protected
56 56
       def attribute_method?(attr_name)
57  
-        attr_name == 'id' || @attributes.include?(attr_name)
  57
+        attr_name == 'id' || (defined?(@attributes) && @attributes.include?(attr_name))
58 58
       end
59 59
   end
60 60
 end
11  activerecord/test/cases/attribute_methods_test.rb
@@ -85,6 +85,17 @@ def test_respond_to?
85 85
     assert !topic.respond_to?("nothingness")
86 86
     assert !topic.respond_to?(:nothingness)
87 87
   end
  88
+  
  89
+  
  90
+  # Syck calls respond_to? before actually calling initialize
  91
+  def test_respond_to_with_allocated_object
  92
+    topic = Topic.allocate
  93
+    assert !topic.respond_to?("nothingness")
  94
+    assert !topic.respond_to?(:nothingness)
  95
+    assert_respond_to topic, "title"
  96
+    assert_respond_to topic, :title
  97
+  end
  98
+  
88 99
 
89 100
   def test_array_content
90 101
     topic = Topic.new

0 notes on commit 96eec09

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