diff options
author | Yves Senn <yves.senn@gmail.com> | 2013-10-30 00:50:36 -0700 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2013-10-30 00:50:36 -0700 |
commit | 609bc40904638663f92ca44ad05e8c95f87aa16b (patch) | |
tree | 8e9b9c7fcce9add3c5a35e451a26031e5f2466b7 /activerecord | |
parent | a5e2800221b29eb5b2a3f3b831e6bc8ce4e7d534 (diff) | |
parent | 510601ce8e6f95b47646e61d6e366245fbb5a329 (diff) | |
download | rails-609bc40904638663f92ca44ad05e8c95f87aa16b.tar.gz rails-609bc40904638663f92ca44ad05e8c95f87aa16b.tar.bz2 rails-609bc40904638663f92ca44ad05e8c95f87aa16b.zip |
Merge pull request #12653 from releu/short_arrays_in_inspect
Short arrays in record.inspect
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_methods.rb | 13 | ||||
-rw-r--r-- | activerecord/test/cases/adapters/postgresql/array_test.rb | 5 |
3 files changed, 19 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index e15756e834..ff8e0beb04 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* ActiveRecord::Base#attribute_for_inspect now truncates long arrays (more than 10 elements) + + *Jan Bernacki* + * Allow for the name of the schema_migrations table to be configured. *Jerad Phelps* diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 43419efc75..3924eec872 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -245,9 +245,10 @@ module ActiveRecord # Returns an <tt>#inspect</tt>-like string for the value of the # attribute +attr_name+. String attributes are truncated upto 50 - # characters, and Date and Time attributes are returned in the - # <tt>:db</tt> format. Other attributes return the value of - # <tt>#inspect</tt> without modification. + # characters, Date and Time attributes are returned in the + # <tt>:db</tt> format, Array attributes are truncated upto 10 values. + # Other attributes return the value of <tt>#inspect</tt> without + # modification. # # person = Person.create!(name: 'David Heinemeier Hansson ' * 3) # @@ -256,6 +257,9 @@ module ActiveRecord # # person.attribute_for_inspect(:created_at) # # => "\"2012-10-22 00:15:07\"" + # + # person.attribute_for_inspect(:tag_ids) + # # => "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]" def attribute_for_inspect(attr_name) value = read_attribute(attr_name) @@ -263,6 +267,9 @@ module ActiveRecord "#{value[0, 50]}...".inspect elsif value.is_a?(Date) || value.is_a?(Time) %("#{value.to_s(:db)}") + elsif value.is_a?(Array) && value.size > 10 + inspected = value.first(10).inspect + %(#{inspected[0...-1]}, ...]) else value.inspect end diff --git a/activerecord/test/cases/adapters/postgresql/array_test.rb b/activerecord/test/cases/adapters/postgresql/array_test.rb index ecdbefcd03..9536cceb1d 100644 --- a/activerecord/test/cases/adapters/postgresql/array_test.rb +++ b/activerecord/test/cases/adapters/postgresql/array_test.rb @@ -113,6 +113,11 @@ class PostgresqlArrayTest < ActiveRecord::TestCase assert_equal(PgArray.last.tags, tag_values) end + def test_attribute_for_inspect_for_array_field + record = PgArray.new { |a| a.ratings = (1..11).to_a } + assert_equal("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]", record.attribute_for_inspect(:ratings)) + end + private def assert_cycle field, array # test creation |