Permalink
Browse files

Relation#inspect handles doesn't perform a new query on an already-lo…

…aded relation
  • Loading branch information...
1 parent b76b9e2 commit c433adffa23785d3727379902859f57bc3f68b27 @jonleighton jonleighton committed Jul 7, 2012
Showing with 16 additions and 2 deletions.
  1. +4 −1 activerecord/lib/active_record/relation.rb
  2. +12 −1 activerecord/test/cases/relations_test.rb
@@ -515,7 +515,10 @@ def values
end
def inspect
- entries = limit([limit_value, 11].compact.min).map(&:inspect)
+ limit = [limit_value, 11].compact.min
+ entries = loaded? ? to_a.take(limit) : limit(limit)
+
+ entries.map!(&:inspect)
entries[10] = '...' if entries.size == 11
"#<#{self.class.name} [#{entries.join(', ')}]>"
@@ -1317,8 +1317,19 @@ def test_presence
assert_equal "#<ActiveRecord::Relation [#{Post.limit(2).map(&:inspect).join(', ')}]>", relation.inspect
end
- test "relations limits the records in #inspect at 10" do
+ test "relations limit the records in #inspect at 10" do
relation = Post.limit(11)
assert_equal "#<ActiveRecord::Relation [#{Post.limit(10).map(&:inspect).join(', ')}, ...]>", relation.inspect
end
+
+ test "already-loaded relations don't perform a new query in #inspect" do
+ relation = Post.limit(2)
+ relation.to_a
+
+ expected = "#<ActiveRecord::Relation [#{Post.limit(2).map(&:inspect).join(', ')}]>"
+
+ assert_no_queries do
+ assert_equal expected, relation.inspect
+ end
+ end
end

0 comments on commit c433adf

Please sign in to comment.