mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Sanitize Base#inspect. Closes #8392.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6761 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
a56081d99a
commit
e48b062eed
5 changed files with 39 additions and 35 deletions
|
@ -1,5 +1,7 @@
|
|||
*SVN*
|
||||
|
||||
* Sanitize Base#inspect. #8392 [Nik Wakelin]
|
||||
|
||||
* Replace the transaction {|transaction|..} semantics with a new Exception ActiveRecord::Rollback. [Koz]
|
||||
|
||||
* Oracle: extract column length for CHAR also. #7866 [ymendel]
|
||||
|
|
|
@ -73,7 +73,8 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def inspect
|
||||
loaded? ? @target.inspect : "<#{@reflection.name} not loaded yet>"
|
||||
reload unless loaded?
|
||||
@target.inspect
|
||||
end
|
||||
|
||||
protected
|
||||
|
|
|
@ -1812,6 +1812,20 @@ module ActiveRecord #:nodoc:
|
|||
clone_attributes :read_attribute_before_type_cast
|
||||
end
|
||||
|
||||
# Format attributes nicely for inspect.
|
||||
def attribute_for_inspect(attr_name)
|
||||
raise "Attribute not present #{attr_name}" unless has_attribute?(attr_name)
|
||||
value = read_attribute(attr_name)
|
||||
|
||||
if (value.is_a?(String) && value.length > 50)
|
||||
'"' + value[0..50] + '..."'
|
||||
elsif (value.is_a?(Date) || value.is_a?(Time))
|
||||
'"' + value.to_s(:db) + '"'
|
||||
else
|
||||
value.inspect
|
||||
end
|
||||
end
|
||||
|
||||
# Returns true if the specified +attribute+ has been set by the user or by a database load and is neither
|
||||
# nil nor empty? (the latter only applies to objects that respond to empty?, most notably Strings).
|
||||
def attribute_present?(attribute)
|
||||
|
@ -1892,6 +1906,11 @@ module ActiveRecord #:nodoc:
|
|||
@readonly = true
|
||||
end
|
||||
|
||||
# Nice pretty inspect.
|
||||
def inspect
|
||||
attributes_as_nice_string = self.class.column_names.collect {|attr_name| "#{attr_name}: #{attribute_for_inspect(attr_name)}"}.join(", ")
|
||||
"#<#{self.class.name} #{attributes_as_nice_string}>"
|
||||
end
|
||||
|
||||
private
|
||||
def create_or_update
|
||||
|
|
|
@ -93,43 +93,12 @@ class AssociationProxyTest < Test::Unit::TestCase
|
|||
|
||||
def test_push_does_not_load_target
|
||||
david = authors(:david)
|
||||
not_loaded_string = '<categories not loaded yet>'
|
||||
not_loaded_re = Regexp.new(not_loaded_string)
|
||||
|
||||
david.categories << categories(:technology)
|
||||
assert_match not_loaded_re, david.inspect
|
||||
assert_equal not_loaded_string, david.categories.inspect
|
||||
assert !david.categories.loaded?
|
||||
assert david.categories.include?(categories(:technology))
|
||||
end
|
||||
|
||||
def test_inspect_does_not_load_target
|
||||
david = authors(:david)
|
||||
not_loaded_string = '<posts not loaded yet>'
|
||||
not_loaded_re = Regexp.new(not_loaded_string)
|
||||
|
||||
2.times do
|
||||
assert !david.posts.loaded?, "Posts should not be loaded yet"
|
||||
assert_match not_loaded_re, david.inspect
|
||||
assert_equal not_loaded_string, david.posts.inspect
|
||||
|
||||
assert !david.posts.empty?, "There should be more than one post"
|
||||
assert !david.posts.loaded?, "Posts should still not be loaded yet"
|
||||
assert_match not_loaded_re, david.inspect
|
||||
assert_equal not_loaded_string, david.posts.inspect
|
||||
|
||||
assert !david.posts.find(:all).empty?, "There should be more than one post"
|
||||
assert !david.posts.loaded?, "Posts should still not be loaded yet"
|
||||
assert_match not_loaded_re, david.inspect
|
||||
assert_equal not_loaded_string, david.posts.inspect
|
||||
|
||||
assert !david.posts(true).empty?, "There should be more than one post"
|
||||
assert david.posts.loaded?, "Posts should be loaded now"
|
||||
assert_no_match not_loaded_re, david.inspect
|
||||
assert_not_equal not_loaded_string, david.posts.inspect
|
||||
|
||||
david.reload
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class HasOneAssociationsTest < Test::Unit::TestCase
|
||||
|
|
|
@ -1646,6 +1646,19 @@ class BasicsTest < Test::Unit::TestCase
|
|||
# "expected last count (#{counts.last}) to be <= first count (#{counts.first})"
|
||||
#end
|
||||
|
||||
def test_inspect
|
||||
topic = topics(:first)
|
||||
assert_equal topic.inspect, '#<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "david@loudthinking.com", written_on: "2003-07-16 07:28:11", bonus_time: "2000-01-01 06:28:00", last_read: "2004-04-15", content: "Have a nice day", approved: false, replies_count: 1, parent_id: nil, type: nil>'
|
||||
end
|
||||
|
||||
def test_attribute_for_inspect
|
||||
t = topics(:first)
|
||||
t.content = %(This is some really long content, longer than 50 characters, so I can test that text is truncated correctly by the new ActiveRecord::Base#inspect method! Yay! BOOM!)
|
||||
|
||||
assert_equal t.attribute_for_inspect(:written_on), '"' + t.written_on.to_s(:db) + '"'
|
||||
assert_equal t.attribute_for_inspect(:content), '"This is some really long content, longer than 50 ch..."'
|
||||
end
|
||||
|
||||
private
|
||||
def assert_readers(model, exceptions)
|
||||
expected_readers = Set.new(model.column_names - ['id'])
|
||||
|
|
Loading…
Reference in a new issue