Skip to content

Commit 607f136

Browse files
committed
fixes a test, and explains why AR::AttributeMethods checks defined?(@attributes) in some places
1 parent 31aab3e commit 607f136

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

activerecord/lib/active_record/attribute_methods.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ def respond_to?(name, include_private = false)
172172

173173
# If the result is true then check for the select case.
174174
# For queries selecting a subset of columns, return false for unselected columns.
175+
# We check defined?(@attributes) not to issue warnings if called on objects that
176+
# have been allocated but not yet initialized.
175177
if defined?(@attributes) && @attributes.present? && self.class.column_names.include?(name)
176178
return has_attribute?(name)
177179
end
@@ -340,6 +342,7 @@ def arel_attributes_with_values_for_update(attribute_names) # :nodoc:
340342
end
341343

342344
def attribute_method?(attr_name) # :nodoc:
345+
# We check defined? because Syck calls respond_to? before actually calling initialize.
343346
defined?(@attributes) && @attributes.include?(attr_name)
344347
end
345348

activerecord/lib/active_record/core.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,9 @@ def connection_handler
352352

353353
# Returns the contents of the record as a nicely formatted string.
354354
def inspect
355-
inspection = if @attributes
355+
# We check defined?(@attributes) not to issue warnings if the object is
356+
# allocated but not initialized.
357+
inspection = if defined?(@attributes) && @attributes
356358
self.class.column_names.collect { |name|
357359
if has_attribute?(name)
358360
"#{name}: #{attribute_for_inspect(name)}"

activerecord/test/cases/attribute_methods_test.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,10 @@ def test_respond_to_with_allocated_object
141141
assert_respond_to topic, :title
142142
end
143143

144-
# IRB inspects the return value of "MyModel.allocate"
145-
# by inspecting it.
144+
# IRB inspects the return value of "MyModel.allocate".
146145
def test_allocated_object_can_be_inspected
147146
topic = Topic.allocate
148-
topic.instance_eval { @attributes = nil }
149-
assert_nothing_raised { topic.inspect }
150-
assert topic.inspect, "#<Topic not initialized>"
147+
assert_equal "#<Topic not initialized>", topic.inspect
151148
end
152149

153150
def test_array_content

0 commit comments

Comments
 (0)