1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Don't attempt to add a string to a lambda

DATE_FORMATS can be either a format string or a lambda we don't want to attempt to concat them with a string if we are in the lambda case
This commit is contained in:
Adam Hess 2020-05-06 11:39:58 -07:00
parent 342f2f0b51
commit 3812134049
2 changed files with 16 additions and 1 deletions

View file

@ -402,7 +402,11 @@ module ActiveRecord
if value.is_a?(String) && value.length > 50
"#{value[0, 50]}...".inspect
elsif value.is_a?(Time)
%("#{value.strftime(Time::DATE_FORMATS[:db] + '.%9N')}")
if Time::DATE_FORMATS[:db].respond_to?(:call)
Time::DATE_FORMATS[:db].call(value)
else
%("#{value.strftime(Time::DATE_FORMATS[:db] + '.%9N')}")
end
elsif value.is_a?(Date)
%("#{value.to_s(:db)}")
else

View file

@ -21,6 +21,17 @@ class CoreTest < ActiveRecord::TestCase
assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "david@loudthinking.com", written_on: "#{topic.written_on.to_s(:db)}.#{topic.written_on.strftime('%9N')}", bonus_time: "#{topic.bonus_time.to_s(:db)}.#{topic.bonus_time.strftime('%9N')}", last_read: "#{topic.last_read.to_s(:db)}", content: "Have a nice day", important: nil, approved: false, replies_count: 1, unique_replies_count: 0, parent_id: nil, parent_title: nil, type: nil, group: nil, created_at: "#{topic.created_at.to_s(:db)}.#{topic.created_at.strftime('%9N')}", updated_at: "#{topic.updated_at.to_s(:db)}.#{topic.updated_at.strftime('%9N')}">), topic.inspect
end
def test_inspect_instance_with_lambda_date_formatter
before = Time::DATE_FORMATS[:db]
Time::DATE_FORMATS[:db] = ->(date) { "my_format" }
topic = topics(:first)
assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "david@loudthinking.com", written_on: my_format, bonus_time: my_format, last_read: "2004-04-15", content: "Have a nice day", important: nil, approved: false, replies_count: 1, unique_replies_count: 0, parent_id: nil, parent_title: nil, type: nil, group: nil, created_at: my_format, updated_at: my_format>), topic.inspect
ensure
Time::DATE_FORMATS[:db] = before
end
def test_inspect_new_instance
assert_match(/Topic id: nil/, Topic.new.inspect)
end