From c2e12e01911113368a47da9f88901918ba12c97d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 24 Nov 2021 22:41:33 +0000 Subject: [PATCH] Use `to_formatted_s(:db)` instead of `to_s(:db)` internally Ruby 3.1 introduced an optimization to string interpolation for some core classes in https://github.com/ruby/ruby/commit/b08dacfea39ad8da3f1fd7fdd0e4538cc892ec44. But since we override `to_s` in some of those core classes to add behavior like `to_s(:db)`, all Rails applications will not be able to take advantage of that improvement. Since we can use the `to_formatted_s` alias for the Rails specific behavior it is best for us to deprecate the `to_s` core extension and allow Rails applications to get the proformace improvement. This commit starts removing all the `to_s(:db)` calls inside the framework so we can deprecate the core extension in the next commit. --- actionview/test/fixtures/replies.yml | 4 ++-- actionview/test/fixtures/topics.yml | 6 ++--- activemodel/lib/active_model/type/date.rb | 2 +- .../active_model/type/helpers/time_value.rb | 2 +- .../connection_adapters/abstract/quoting.rb | 2 +- activerecord/lib/active_record/fixtures.rb | 2 +- .../cases/adapters/sqlite3/quoting_test.rb | 4 ++-- .../cases/multiparameter_attributes_test.rb | 4 ++-- activerecord/test/cases/quoting_test.rb | 18 +++++++-------- activerecord/test/fixtures/memberships.yml | 12 +++++----- activerecord/test/fixtures/pirates.yml | 4 ++-- .../core_ext/date/conversions.rb | 2 +- .../core_ext/date_time/conversions.rb | 2 +- .../core_ext/numeric/conversions.rb | 1 + .../core_ext/range/conversions.rb | 6 ++--- activesupport/lib/active_support/xml_mini.rb | 2 +- .../test/core_ext/array/conversions_test.rb | 4 ++-- activesupport/test/core_ext/date_ext_test.rb | 4 ++-- .../test/core_ext/date_time_ext_test.rb | 2 +- activesupport/test/core_ext/range_ext_test.rb | 8 +++---- activesupport/test/core_ext/time_ext_test.rb | 2 +- .../test/core_ext/time_with_zone_test.rb | 2 +- activesupport/test/time_travel_test.rb | 22 +++++++++---------- guides/source/security.md | 4 ++-- .../rails/generators/generated_attribute.rb | 4 ++-- .../generators/generated_attribute_test.rb | 4 ++-- 26 files changed, 65 insertions(+), 64 deletions(-) diff --git a/actionview/test/fixtures/replies.yml b/actionview/test/fixtures/replies.yml index 2a3454b8bf..977513ae63 100644 --- a/actionview/test/fixtures/replies.yml +++ b/actionview/test/fixtures/replies.yml @@ -3,7 +3,7 @@ witty_retort: topic_id: 1 developer_id: 1 content: Birdman is better! - created_at: <%= 6.hours.ago.to_s(:db) %> + created_at: <%= 6.hours.ago.to_formatted_s(:db) %> updated_at: nil another: @@ -11,5 +11,5 @@ another: topic_id: 2 developer_id: 1 content: Nuh uh! - created_at: <%= 1.hour.ago.to_s(:db) %> + created_at: <%= 1.hour.ago.to_formatted_s(:db) %> updated_at: nil diff --git a/actionview/test/fixtures/topics.yml b/actionview/test/fixtures/topics.yml index 41bd3849bc..aac6802d60 100644 --- a/actionview/test/fixtures/topics.yml +++ b/actionview/test/fixtures/topics.yml @@ -3,7 +3,7 @@ futurama: title: Isn't futurama awesome? subtitle: It really is, isn't it. content: I like futurama - created_at: <%= 1.day.ago.to_s(:db) %> + created_at: <%= 1.day.ago.to_formatted_s(:db) %> updated_at: harvey_birdman: @@ -11,7 +11,7 @@ harvey_birdman: title: Harvey Birdman is the king of all men subtitle: yup content: It really is - created_at: <%= 2.hours.ago.to_s(:db) %> + created_at: <%= 2.hours.ago.to_formatted_s(:db) %> updated_at: rails: @@ -19,4 +19,4 @@ rails: title: Rails is nice subtitle: It makes me happy content: except when I have to hack internals to fix pagination. even then really. - created_at: <%= 20.minutes.ago.to_s(:db) %> + created_at: <%= 20.minutes.ago.to_formatted_s(:db) %> diff --git a/activemodel/lib/active_model/type/date.rb b/activemodel/lib/active_model/type/date.rb index 0e96d2c8a4..4b0403161d 100644 --- a/activemodel/lib/active_model/type/date.rb +++ b/activemodel/lib/active_model/type/date.rb @@ -11,7 +11,7 @@ module ActiveModel end def type_cast_for_schema(value) - value.to_s(:db).inspect + value.to_formatted_s(:db).inspect end private diff --git a/activemodel/lib/active_model/type/helpers/time_value.rb b/activemodel/lib/active_model/type/helpers/time_value.rb index f357a1f72f..fe2ac7f241 100644 --- a/activemodel/lib/active_model/type/helpers/time_value.rb +++ b/activemodel/lib/active_model/type/helpers/time_value.rb @@ -36,7 +36,7 @@ module ActiveModel end def type_cast_for_schema(value) - value.to_s(:db).inspect + value.to_formatted_s(:db).inspect end def user_input_in_time_zone(value) diff --git a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb index 304338cce6..928a72181c 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb @@ -128,7 +128,7 @@ module ActiveRecord end end - result = value.to_s(:db) + result = value.to_formatted_s(:db) if value.respond_to?(:usec) && value.usec > 0 result << "." << sprintf("%06d", value.usec) else diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index b22ac2e980..386d6e8ce8 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -407,7 +407,7 @@ module ActiveRecord # defaults: # # DEFAULTS: &DEFAULTS - # created_on: <%= 3.weeks.ago.to_s(:db) %> + # created_on: <%= 3.weeks.ago.to_formatted_s(:db) %> # # first: # name: Smurf diff --git a/activerecord/test/cases/adapters/sqlite3/quoting_test.rb b/activerecord/test/cases/adapters/sqlite3/quoting_test.rb index 9d26f32102..c13e8232cf 100644 --- a/activerecord/test/cases/adapters/sqlite3/quoting_test.rb +++ b/activerecord/test/cases/adapters/sqlite3/quoting_test.rb @@ -57,7 +57,7 @@ class SQLite3QuotingTest < ActiveRecord::SQLite3TestCase t = Time.new(2000, 7, 1, 0, 0, 0, "+04:30") expected = t.change(year: 2000, month: 1, day: 1) - expected = expected.getutc.to_s(:db).sub(/\A\d\d\d\d-\d\d-\d\d /, "2000-01-01 ") + expected = expected.getutc.to_formatted_s(:db).sub(/\A\d\d\d\d-\d\d-\d\d /, "2000-01-01 ") assert_equal expected, @conn.quoted_time(t) end @@ -70,7 +70,7 @@ class SQLite3QuotingTest < ActiveRecord::SQLite3TestCase t = Time.new(2000, 7, 1, 0, 0, 0, "+04:30") expected = t.change(year: 2000, month: 1, day: 1) - expected = expected.getlocal.to_s(:db).sub(/\A\d\d\d\d-\d\d-\d\d /, "2000-01-01 ") + expected = expected.getlocal.to_formatted_s(:db).sub(/\A\d\d\d\d-\d\d-\d\d /, "2000-01-01 ") assert_equal expected, @conn.quoted_time(t) end diff --git a/activerecord/test/cases/multiparameter_attributes_test.rb b/activerecord/test/cases/multiparameter_attributes_test.rb index 6f3903eed4..1ec448f791 100644 --- a/activerecord/test/cases/multiparameter_attributes_test.rb +++ b/activerecord/test/cases/multiparameter_attributes_test.rb @@ -107,8 +107,8 @@ class MultiParameterAttributeTest < ActiveRecord::TestCase } topic = Topic.find(1) topic.attributes = attributes - # testing against to_s(:db) representation because either a Time or a DateTime might be returned, depending on platform - assert_equal "1850-06-24 16:24:00", topic.written_on.to_s(:db) + # testing against to_formatted_s(:db) representation because either a Time or a DateTime might be returned, depending on platform + assert_equal "1850-06-24 16:24:00", topic.written_on.to_formatted_s(:db) end def test_multiparameter_attributes_on_time_will_raise_on_big_time_if_missing_date_parts diff --git a/activerecord/test/cases/quoting_test.rb b/activerecord/test/cases/quoting_test.rb index ed77ccf138..cea079bb6b 100644 --- a/activerecord/test/cases/quoting_test.rb +++ b/activerecord/test/cases/quoting_test.rb @@ -43,20 +43,20 @@ module ActiveRecord def test_quoted_date t = Date.today - assert_equal t.to_s(:db), @quoter.quoted_date(t) + assert_equal t.to_formatted_s(:db), @quoter.quoted_date(t) end def test_quoted_timestamp_utc with_timezone_config default: :utc do t = Time.now.change(usec: 0) - assert_equal t.getutc.to_s(:db), @quoter.quoted_date(t) + assert_equal t.getutc.to_formatted_s(:db), @quoter.quoted_date(t) end end def test_quoted_timestamp_local with_timezone_config default: :local do t = Time.now.change(usec: 0) - assert_equal t.getlocal.to_s(:db), @quoter.quoted_date(t) + assert_equal t.getlocal.to_formatted_s(:db), @quoter.quoted_date(t) end end @@ -65,7 +65,7 @@ module ActiveRecord t = Time.now.change(usec: 0) expected = t.change(year: 2000, month: 1, day: 1) - expected = expected.getutc.to_s(:db).slice(11..-1) + expected = expected.getutc.to_formatted_s(:db).slice(11..-1) assert_equal expected, @quoter.quoted_time(t) end @@ -76,7 +76,7 @@ module ActiveRecord t = Time.now.change(usec: 0) expected = t.change(year: 2000, month: 1, day: 1) - expected = expected.getlocal.to_s(:db).sub("2000-01-01 ", "") + expected = expected.getlocal.to_formatted_s(:db).sub("2000-01-01 ", "") assert_equal expected, @quoter.quoted_time(t) end @@ -88,7 +88,7 @@ module ActiveRecord t = Time.new(2000, 7, 1, 0, 0, 0, "+04:30") expected = t.change(year: 2000, month: 1, day: 1) - expected = expected.getutc.to_s(:db).slice(11..-1) + expected = expected.getutc.to_formatted_s(:db).slice(11..-1) assert_equal expected, @quoter.quoted_time(t) end @@ -101,7 +101,7 @@ module ActiveRecord t = Time.new(2000, 7, 1, 0, 0, 0, "+04:30") expected = t.change(year: 2000, month: 1, day: 1) - expected = expected.getlocal.to_s(:db).slice(11..-1) + expected = expected.getlocal.to_formatted_s(:db).slice(11..-1) assert_equal expected, @quoter.quoted_time(t) end @@ -111,7 +111,7 @@ module ActiveRecord def test_quoted_datetime_utc with_timezone_config default: :utc do t = Time.now.change(usec: 0).to_datetime - assert_equal t.getutc.to_s(:db), @quoter.quoted_date(t) + assert_equal t.getutc.to_formatted_s(:db), @quoter.quoted_date(t) end end @@ -120,7 +120,7 @@ module ActiveRecord def test_quoted_datetime_local with_timezone_config default: :local do t = Time.now.change(usec: 0).to_datetime - assert_equal t.to_s(:db), @quoter.quoted_date(t) + assert_equal t.to_formatted_s(:db), @quoter.quoted_date(t) end end diff --git a/activerecord/test/fixtures/memberships.yml b/activerecord/test/fixtures/memberships.yml index bc20734231..9fc8828720 100644 --- a/activerecord/test/fixtures/memberships.yml +++ b/activerecord/test/fixtures/memberships.yml @@ -1,40 +1,40 @@ membership_of_boring_club: - joined_on: <%= 3.weeks.ago.to_s(:db) %> + joined_on: <%= 3.weeks.ago.to_formatted_s(:db) %> club: boring_club member_id: 1 favorite: false type: CurrentMembership membership_of_favorite_club: - joined_on: <%= 3.weeks.ago.to_s(:db) %> + joined_on: <%= 3.weeks.ago.to_formatted_s(:db) %> club: moustache_club member_id: 1 favorite: true type: Membership other_guys_membership: - joined_on: <%= 4.weeks.ago.to_s(:db) %> + joined_on: <%= 4.weeks.ago.to_formatted_s(:db) %> club: boring_club member_id: 2 favorite: false type: CurrentMembership blarpy_winkup_outrageous_club: - joined_on: <%= 4.weeks.ago.to_s(:db) %> + joined_on: <%= 4.weeks.ago.to_formatted_s(:db) %> club: outrageous_club member_id: 3 favorite: false type: CurrentMembership super_membership_of_boring_club: - joined_on: <%= 3.weeks.ago.to_s(:db) %> + joined_on: <%= 3.weeks.ago.to_formatted_s(:db) %> club: boring_club member_id: 1 favorite: false type: SuperMembership selected_membership_of_boring_club: - joined_on: <%= 3.weeks.ago.to_s(:db) %> + joined_on: <%= 3.weeks.ago.to_formatted_s(:db) %> club: boring_club member_id: 1 favorite: false diff --git a/activerecord/test/fixtures/pirates.yml b/activerecord/test/fixtures/pirates.yml index 0b1a785853..ba6f50ce33 100644 --- a/activerecord/test/fixtures/pirates.yml +++ b/activerecord/test/fixtures/pirates.yml @@ -5,8 +5,8 @@ blackbeard: redbeard: catchphrase: "Avast!" parrot: louis - created_on: "<%= 2.weeks.ago.to_s(:db) %>" - updated_on: "<%= 2.weeks.ago.to_s(:db) %>" + created_on: "<%= 2.weeks.ago.to_formatted_s(:db) %>" + updated_on: "<%= 2.weeks.ago.to_formatted_s(:db) %>" mark: catchphrase: "X $LABELs the spot!" diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb index 050a62bb31..5131ed5d85 100644 --- a/activesupport/lib/active_support/core_ext/date/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date/conversions.rb @@ -27,7 +27,7 @@ class Date # date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007 # # date.to_formatted_s(:db) # => "2007-11-10" - # date.to_s(:db) # => "2007-11-10" + # date.to_formatted_s(:db) # => "2007-11-10" # # date.to_formatted_s(:short) # => "10 Nov" # date.to_formatted_s(:number) # => "20071110" diff --git a/activesupport/lib/active_support/core_ext/date_time/conversions.rb b/activesupport/lib/active_support/core_ext/date_time/conversions.rb index 231bf870a2..8fe2c1228c 100644 --- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb @@ -15,7 +15,7 @@ class DateTime # datetime = DateTime.civil(2007, 12, 4, 0, 0, 0, 0) # => Tue, 04 Dec 2007 00:00:00 +0000 # # datetime.to_formatted_s(:db) # => "2007-12-04 00:00:00" - # datetime.to_s(:db) # => "2007-12-04 00:00:00" + # datetime.to_formatted_s(:db) # => "2007-12-04 00:00:00" # datetime.to_s(:number) # => "20071204000000" # datetime.to_formatted_s(:short) # => "04 Dec 00:00" # datetime.to_formatted_s(:long) # => "December 04, 2007 00:00" diff --git a/activesupport/lib/active_support/core_ext/numeric/conversions.rb b/activesupport/lib/active_support/core_ext/numeric/conversions.rb index 1e67e7c7e7..00a1209218 100644 --- a/activesupport/lib/active_support/core_ext/numeric/conversions.rb +++ b/activesupport/lib/active_support/core_ext/numeric/conversions.rb @@ -132,6 +132,7 @@ module ActiveSupport super(format) end end + alias_method :to_formatted_s, :to_s end end diff --git a/activesupport/lib/active_support/core_ext/range/conversions.rb b/activesupport/lib/active_support/core_ext/range/conversions.rb index 024e32db40..b2d039e043 100644 --- a/activesupport/lib/active_support/core_ext/range/conversions.rb +++ b/activesupport/lib/active_support/core_ext/range/conversions.rb @@ -7,7 +7,7 @@ module ActiveSupport case start when String then "BETWEEN '#{start}' AND '#{stop}'" else - "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'" + "BETWEEN '#{start.to_formatted_s(:db)}' AND '#{stop.to_formatted_s(:db)}'" end end } @@ -17,14 +17,14 @@ module ActiveSupport # range = (1..100) # => 1..100 # # range.to_s # => "1..100" - # range.to_s(:db) # => "BETWEEN '1' AND '100'" + # range.to_formatted_s(:db) # => "BETWEEN '1' AND '100'" # # == Adding your own range formats to to_s # You can add your own formats to the Range::RANGE_FORMATS hash. # Use the format name as the hash key and a Proc instance. # # # config/initializers/range_formats.rb - # Range::RANGE_FORMATS[:short] = ->(start, stop) { "Between #{start.to_s(:db)} and #{stop.to_s(:db)}" } + # Range::RANGE_FORMATS[:short] = ->(start, stop) { "Between #{start.to_formatted_s(:db)} and #{stop.to_formatted_s(:db)}" } def to_s(format = :default) if formatter = RANGE_FORMATS[format] formatter.call(first, last) diff --git a/activesupport/lib/active_support/xml_mini.rb b/activesupport/lib/active_support/xml_mini.rb index e2b52daa65..3a4efb106f 100644 --- a/activesupport/lib/active_support/xml_mini.rb +++ b/activesupport/lib/active_support/xml_mini.rb @@ -54,7 +54,7 @@ module ActiveSupport FORMATTING = { "symbol" => Proc.new { |symbol| symbol.to_s }, - "date" => Proc.new { |date| date.to_s(:db) }, + "date" => Proc.new { |date| date.to_formatted_s(:db) }, "dateTime" => Proc.new { |time| time.xmlschema }, "binary" => Proc.new { |binary| ::Base64.encode64(binary) }, "yaml" => Proc.new { |yaml| yaml.to_yaml } diff --git a/activesupport/test/core_ext/array/conversions_test.rb b/activesupport/test/core_ext/array/conversions_test.rb index 7312fa009b..4b61c0e2b7 100644 --- a/activesupport/test/core_ext/array/conversions_test.rb +++ b/activesupport/test/core_ext/array/conversions_test.rb @@ -88,8 +88,8 @@ class ToSTest < ActiveSupport::TestCase def test_to_s_db collection = [TestDB.new, TestDB.new, TestDB.new] - assert_equal "null", [].to_s(:db) - assert_equal "1,2,3", collection.to_s(:db) + assert_equal "null", [].to_formatted_s(:db) + assert_equal "1,2,3", collection.to_formatted_s(:db) end end diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 0612e67079..4935fa9484 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -27,7 +27,7 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal "21 Feb", date.to_s(:short) assert_equal "February 21, 2005", date.to_s(:long) assert_equal "February 21st, 2005", date.to_s(:long_ordinal) - assert_equal "2005-02-21", date.to_s(:db) + assert_equal "2005-02-21", date.to_formatted_s(:db) assert_equal "2005-02-21", date.to_s(:inspect) assert_equal "21 Feb 2005", date.to_s(:rfc822) assert_equal "2005-02-21", date.to_s(:iso8601) @@ -39,7 +39,7 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal "01 Feb", date.to_s(:short) assert_equal "February 01, 2005", date.to_s(:long) assert_equal "February 1st, 2005", date.to_s(:long_ordinal) - assert_equal "2005-02-01", date.to_s(:db) + assert_equal "2005-02-01", date.to_formatted_s(:db) assert_equal "2005-02-01", date.to_s(:inspect) assert_equal "01 Feb 2005", date.to_s(:rfc822) assert_equal "2005-02-01", date.to_s(:iso8601) diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 2c6ad8d866..021687b69c 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -15,7 +15,7 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase def test_to_s datetime = DateTime.new(2005, 2, 21, 14, 30, 0, 0) - assert_equal "2005-02-21 14:30:00", datetime.to_s(:db) + assert_equal "2005-02-21 14:30:00", datetime.to_formatted_s(:db) assert_equal "2005-02-21 14:30:00.000000000 +0000", datetime.to_s(:inspect) assert_equal "14:30", datetime.to_s(:time) assert_equal "21 Feb 14:30", datetime.to_s(:short) diff --git a/activesupport/test/core_ext/range_ext_test.rb b/activesupport/test/core_ext/range_ext_test.rb index f9c1da3840..733bea3346 100644 --- a/activesupport/test/core_ext/range_ext_test.rb +++ b/activesupport/test/core_ext/range_ext_test.rb @@ -8,22 +8,22 @@ require "active_support/core_ext/range" class RangeTest < ActiveSupport::TestCase def test_to_s_from_dates date_range = Date.new(2005, 12, 10)..Date.new(2005, 12, 12) - assert_equal "BETWEEN '2005-12-10' AND '2005-12-12'", date_range.to_s(:db) + assert_equal "BETWEEN '2005-12-10' AND '2005-12-12'", date_range.to_formatted_s(:db) end def test_to_s_from_times date_range = Time.utc(2005, 12, 10, 15, 30)..Time.utc(2005, 12, 10, 17, 30) - assert_equal "BETWEEN '2005-12-10 15:30:00' AND '2005-12-10 17:30:00'", date_range.to_s(:db) + assert_equal "BETWEEN '2005-12-10 15:30:00' AND '2005-12-10 17:30:00'", date_range.to_formatted_s(:db) end def test_to_s_with_alphabets alphabet_range = ("a".."z") - assert_equal "BETWEEN 'a' AND 'z'", alphabet_range.to_s(:db) + assert_equal "BETWEEN 'a' AND 'z'", alphabet_range.to_formatted_s(:db) end def test_to_s_with_numeric number_range = (1..100) - assert_equal "BETWEEN '1' AND '100'", number_range.to_s(:db) + assert_equal "BETWEEN '1' AND '100'", number_range.to_formatted_s(:db) end def test_date_range diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 3c28310940..452f38362b 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -583,7 +583,7 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase time = Time.utc(2005, 2, 21, 17, 44, 30.12345678901) assert_equal time.to_default_s, time.to_s assert_equal time.to_default_s, time.to_s(:doesnt_exist) - assert_equal "2005-02-21 17:44:30", time.to_s(:db) + assert_equal "2005-02-21 17:44:30", time.to_formatted_s(:db) assert_equal "21 Feb 17:44", time.to_s(:short) assert_equal "17:44", time.to_s(:time) assert_equal "20050221174430", time.to_s(:number) diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index c328234a39..013aad6b03 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -138,7 +138,7 @@ class TimeWithZoneTest < ActiveSupport::TestCase end def test_to_s_db - assert_equal "2000-01-01 00:00:00", @twz.to_s(:db) + assert_equal "2000-01-01 00:00:00", @twz.to_formatted_s(:db) end def test_to_s_inspect diff --git a/activesupport/test/time_travel_test.rb b/activesupport/test/time_travel_test.rb index c9486a2afc..f9673516dd 100644 --- a/activesupport/test/time_travel_test.rb +++ b/activesupport/test/time_travel_test.rb @@ -18,9 +18,9 @@ class TimeTravelTest < ActiveSupport::TestCase expected_time = Time.now + 1.day travel 1.day - assert_equal expected_time.to_s(:db), Time.now.to_s(:db) + assert_equal expected_time.to_formatted_s(:db), Time.now.to_formatted_s(:db) assert_equal expected_time.to_date, Date.today - assert_equal expected_time.to_datetime.to_s(:db), DateTime.now.to_s(:db) + assert_equal expected_time.to_datetime.to_formatted_s(:db), DateTime.now.to_formatted_s(:db) ensure travel_back end @@ -31,14 +31,14 @@ class TimeTravelTest < ActiveSupport::TestCase expected_time = Time.now + 1.day travel 1.day do - assert_equal expected_time.to_s(:db), Time.now.to_s(:db) + assert_equal expected_time.to_formatted_s(:db), Time.now.to_formatted_s(:db) assert_equal expected_time.to_date, Date.today - assert_equal expected_time.to_datetime.to_s(:db), DateTime.now.to_s(:db) + assert_equal expected_time.to_datetime.to_formatted_s(:db), DateTime.now.to_formatted_s(:db) end - assert_not_equal expected_time.to_s(:db), Time.now.to_s(:db) + assert_not_equal expected_time.to_formatted_s(:db), Time.now.to_formatted_s(:db) assert_not_equal expected_time.to_date, Date.today - assert_not_equal expected_time.to_datetime.to_s(:db), DateTime.now.to_s(:db) + assert_not_equal expected_time.to_datetime.to_formatted_s(:db), DateTime.now.to_formatted_s(:db) end end @@ -78,7 +78,7 @@ class TimeTravelTest < ActiveSupport::TestCase expected_time = 5.minutes.ago travel_to 5.minutes.ago do - assert_equal expected_time.to_s(:db), Time.zone.now.to_s(:db) + assert_equal expected_time.to_formatted_s(:db), Time.zone.now.to_formatted_s(:db) end end end @@ -92,7 +92,7 @@ class TimeTravelTest < ActiveSupport::TestCase expected_time = Time.new(2004, 11, 24, 1, 4, 44) travel_to "2004-11-24 01:04:44" do - assert_equal expected_time.to_s(:db), Time.zone.now.to_s(:db) + assert_equal expected_time.to_formatted_s(:db), Time.zone.now.to_formatted_s(:db) end end end @@ -237,7 +237,7 @@ class TimeTravelTest < ActiveSupport::TestCase freeze_time sleep(1) - assert_equal expected_time.to_s(:db), Time.now.to_s(:db) + assert_equal expected_time.to_formatted_s(:db), Time.now.to_formatted_s(:db) ensure travel_back end @@ -248,10 +248,10 @@ class TimeTravelTest < ActiveSupport::TestCase freeze_time do sleep(1) - assert_equal expected_time.to_s(:db), Time.now.to_s(:db) + assert_equal expected_time.to_formatted_s(:db), Time.now.to_formatted_s(:db) end - assert_operator expected_time.to_s(:db), :<, Time.now.to_s(:db) + assert_operator expected_time.to_formatted_s(:db), :<, Time.now.to_formatted_s(:db) end def test_time_helper_unfreeze_time diff --git a/guides/source/security.md b/guides/source/security.md index 322354731e..5d5660db33 100644 --- a/guides/source/security.md +++ b/guides/source/security.md @@ -216,7 +216,7 @@ One possibility is to set the expiry time-stamp of the cookie with the session I ```ruby class Session < ApplicationRecord def self.sweep(time = 1.hour) - where("updated_at < ?", time.ago.to_s(:db)).delete_all + where("updated_at < ?", time.ago.to_formatted_s(:db)).delete_all end end ``` @@ -224,7 +224,7 @@ end The section about session fixation introduced the problem of maintained sessions. An attacker maintaining a session every five minutes can keep the session alive forever, although you are expiring sessions. A simple solution for this would be to add a `created_at` column to the sessions table. Now you can delete sessions that were created a long time ago. Use this line in the sweep method above: ```ruby -where("updated_at < ? OR created_at < ?", time.ago.to_s(:db), 2.days.ago.to_s(:db)).delete_all +where("updated_at < ? OR created_at < ?", time.ago.to_formatted_s(:db), 2.days.ago.to_formatted_s(:db)).delete_all ``` Cross-Site Request Forgery (CSRF) diff --git a/railties/lib/rails/generators/generated_attribute.rb b/railties/lib/rails/generators/generated_attribute.rb index 0bea88f511..cd37be8111 100644 --- a/railties/lib/rails/generators/generated_attribute.rb +++ b/railties/lib/rails/generators/generated_attribute.rb @@ -123,8 +123,8 @@ module Rails when :integer then 1 when :float then 1.5 when :decimal then "9.99" - when :datetime, :timestamp, :time then Time.now.to_s(:db) - when :date then Date.today.to_s(:db) + when :datetime, :timestamp, :time then Time.now.to_formatted_s(:db) + when :date then Date.today.to_formatted_s(:db) when :string then name == "type" ? "" : "MyString" when :text then "MyText" when :boolean then false diff --git a/railties/test/generators/generated_attribute_test.rb b/railties/test/generators/generated_attribute_test.rb index 122d243cba..7e945128bc 100644 --- a/railties/test/generators/generated_attribute_test.rb +++ b/railties/test/generators/generated_attribute_test.rb @@ -91,12 +91,12 @@ class GeneratedAttributeTest < Rails::Generators::TestCase def test_default_value_is_datetime %w(datetime timestamp time).each do |attribute_type| - assert_field_default_value attribute_type, Time.now.to_s(:db) + assert_field_default_value attribute_type, Time.now.to_formatted_s(:db) end end def test_default_value_is_date - assert_field_default_value :date, Date.today.to_s(:db) + assert_field_default_value :date, Date.today.to_formatted_s(:db) end def test_default_value_is_string