diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-05-18 02:11:43 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-05-18 02:11:43 +0000 |
commit | e48b062eed430eea43ca94630310e9a7d663cf03 (patch) | |
tree | 75070ecf9f9c8b7bdc6372ca1cd386fb8c228023 /activerecord/lib/active_record | |
parent | a56081d99ab056859679a92fb8f4a95e02bb6b5c (diff) | |
download | rails-e48b062eed430eea43ca94630310e9a7d663cf03.tar.gz rails-e48b062eed430eea43ca94630310e9a7d663cf03.tar.bz2 rails-e48b062eed430eea43ca94630310e9a7d663cf03.zip |
Sanitize Base#inspect. Closes #8392.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6761 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/associations/association_proxy.rb | 3 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 21 |
2 files changed, 22 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index 010e4e396b..96cbd3043a 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -73,7 +73,8 @@ module ActiveRecord end def inspect - loaded? ? @target.inspect : "<#{@reflection.name} not loaded yet>" + reload unless loaded? + @target.inspect end protected diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 31b25e5979..65f808815e 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -36,7 +36,7 @@ module ActiveRecord #:nodoc: end class Rollback < StandardError #:nodoc: end - + class AttributeAssignmentError < ActiveRecordError #:nodoc: attr_reader :exception, :attribute def initialize(message, exception, attribute) @@ -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 |