Skip to content

Commit

Permalink
fixes a test, and explains why AR::AttributeMethods checks defined?(@…
Browse files Browse the repository at this point in the history
…attributes) in some places
  • Loading branch information
fxn committed Apr 28, 2013
1 parent 31aab3e commit 607f136
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
3 changes: 3 additions & 0 deletions activerecord/lib/active_record/attribute_methods.rb
Expand Up @@ -172,6 +172,8 @@ def respond_to?(name, include_private = false)

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

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

Expand Down
4 changes: 3 additions & 1 deletion activerecord/lib/active_record/core.rb
Expand Up @@ -352,7 +352,9 @@ def connection_handler

# Returns the contents of the record as a nicely formatted string.
def inspect
inspection = if @attributes
# We check defined?(@attributes) not to issue warnings if the object is
# allocated but not initialized.
inspection = if defined?(@attributes) && @attributes
self.class.column_names.collect { |name|
if has_attribute?(name)
"#{name}: #{attribute_for_inspect(name)}"
Expand Down
7 changes: 2 additions & 5 deletions activerecord/test/cases/attribute_methods_test.rb
Expand Up @@ -141,13 +141,10 @@ def test_respond_to_with_allocated_object
assert_respond_to topic, :title
end

# IRB inspects the return value of "MyModel.allocate"
# by inspecting it.
# IRB inspects the return value of "MyModel.allocate".
def test_allocated_object_can_be_inspected
topic = Topic.allocate
topic.instance_eval { @attributes = nil }
assert_nothing_raised { topic.inspect }
assert topic.inspect, "#<Topic not initialized>"
assert_equal "#<Topic not initialized>", topic.inspect
end

def test_array_content
Expand Down

0 comments on commit 607f136

Please sign in to comment.