diff options
Diffstat (limited to 'activesupport')
43 files changed, 534 insertions, 934 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index e4497e1756..812e18a253 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,761 +1,53 @@ -* Time zones: Ensure that the UTC offset reflects DST changes that occurred - since the app started. Removes UTC offset caching, reducing performance, - but this is still relatively quick and isn't in any hot paths. +* Introduce Module#delegate_missing_to. - *Alexey Shein* + When building a decorator, a common pattern emerges: -* Make `getlocal` and `getutc` always return instances of `Time` for - `ActiveSupport::TimeWithZone` and `DateTime`. This eliminates a possible - stack level too deep error in `to_time` where `ActiveSupport::TimeWithZone` - was wrapping a `DateTime` instance. As a consequence of this the internal - time value in `ActiveSupport::TimeWithZone` is now always an instance of - `Time` in the UTC timezone, whether that's as the UTC time directly or - a representation of the local time in the timezone. There should be no - consequences of this internal change and if there are it's a bug due to - leaky abstractions. - - *Andrew White* - -* Add `DateTime#subsec` to return the fraction of a second as a `Rational`. - - *Andrew White* - -* Add additional aliases for `DateTime#utc` to mirror the ones on - `ActiveSupport::TimeWithZone` and `Time`. - - *Andrew White* - -* Add `DateTime#localtime` to return an instance of `Time` in the system's - local timezone. Also aliased to `getlocal`. - - *Andrew White*, *Yuichiro Kaneko* - -* Add `Time#sec_fraction` to return the fraction of a second as a `Rational`. - - *Andrew White* - -* Add `ActiveSupport.to_time_preserves_timezone` config option to control - how `to_time` handles timezones. In Ruby 2.4+ the behavior will change - from converting to the local system timezone, to preserving the timezone - of the receiver. This config option defaults to false so that apps made - with earlier versions of Rails are not affected when upgrading, e.g: - - >> ENV['TZ'] = 'US/Eastern' - - >> "2016-04-23T10:23:12.000Z".to_time - => "2016-04-23T06:23:12.000-04:00" - - >> ActiveSupport.to_time_preserves_timezone = true - - >> "2016-04-23T10:23:12.000Z".to_time - => "2016-04-23T10:23:12.000Z" - - Fixes #24617. - - *Andrew White* - -* `ActiveSupport::TimeZone.country_zones(country_code)` looks up the - country's time zones by its two-letter ISO3166 country code, e.g. - - >> ActiveSupport::TimeZone.country_zones(:jp).map(&:to_s) - => ["(GMT+09:00) Osaka"] - - >> ActiveSupport::TimeZone.country_zones(:uy).map(&:to_s) - => ["(GMT-03:00) Montevideo"] - - *Andrey Novikov* - -* `Array#sum` compat with Ruby 2.4's native method. - - Ruby 2.4 introduces `Array#sum`, but it only supports numeric elements, - breaking our `Enumerable#sum` which supports arbitrary `Object#+`. - To fix, override `Array#sum` with our compatible implementation. - - Native Ruby 2.4: - - %w[ a b ].sum - # => TypeError: String can't be coerced into Fixnum - - With `Enumerable#sum` shim: - - %w[ a b ].sum - # => 'ab' - - We tried shimming the fast path and falling back to the compatible path - if it fails, but that ends up slower even in simple cases due to the cost - of exception handling. Our only choice is to override the native `Array#sum` - with our `Enumerable#sum`. - - *Jeremy Daer* - -* `ActiveSupport::Duration` supports ISO8601 formatting and parsing. - - ActiveSupport::Duration.parse('P3Y6M4DT12H30M5S') - # => 3 years, 6 months, 4 days, 12 hours, 30 minutes, and 5 seconds - - (3.years + 3.days).iso8601 - # => "P3Y3D" - - Inspired by Arnau Siches' [ISO8601 gem](https://github.com/arnau/ISO8601/) - and rewritten by Andrey Novikov with suggestions from Andrew White. Test - data from the ISO8601 gem redistributed under MIT license. - - (Will be used to support the PostgreSQL interval data type.) - - *Andrey Novikov*, *Arnau Siches*, *Andrew White* - -* `Cache#fetch(key, force: true)` forces a cache miss, so it must be called - with a block to provide a new value to cache. Fetching with `force: true` - but without a block now raises ArgumentError. - - cache.fetch('key', force: true) # => ArgumentError - - *Santosh Wadghule* - -* `ActiveSupport::Duration` supports weeks and hours. - - [1.hour.inspect, 1.hour.value, 1.hour.parts] - # => ["3600 seconds", 3600, [[:seconds, 3600]]] # Before - # => ["1 hour", 3600, [[:hours, 1]]] # After - - [1.week.inspect, 1.week.value, 1.week.parts] - # => ["7 days", 604800, [[:days, 7]]] # Before - # => ["1 week", 604800, [[:weeks, 1]]] # After - - This brings us into closer conformance with ISO8601 and relieves some - astonishment about getting `1.hour.inspect # => 3600 seconds`. - - Compatibility: The duration's `value` remains the same, so apps using - durations are oblivious to the new time periods. Apps, libraries, and - plugins that work with the internal `parts` hash will need to broaden - their time period handling to cover hours & weeks. - - *Andrey Novikov* - -* Fix behavior of JSON encoding for `Exception`. - - *namusyaka* - -* Make `number_to_phone` format number with regexp pattern. - - number_to_phone(18812345678, pattern: /(\d{3})(\d{4})(\d{4})/) - # => 188-1234-5678 - - *Pan Gaoyong* - -* Match `String#to_time`'s behaviour to that of ruby's implementation for edge cases. - - `nil` is now returned instead of the current date if the string provided does - contain time information, but none that is used to build the `Time` object. - - Fixes #22958. - - *Siim Liiser* - -* Rely on the native DateTime#<=> implementation to handle non-datetime like - objects instead of returning `nil` ourselves. This restores the ability - of `DateTime` instances to be compared with a `Numeric` that represents an - astronomical julian day number. - - Fixes #24228. - - *Andrew White* - -* Add `String#upcase_first` method. - - *Glauco Custódio*, *bogdanvlviv* - -* Prevent `Marshal.load` from looping infinitely when trying to autoload a constant - which resolves to a different name. - - *Olek Janiszewski* - -* Deprecate `Module.local_constants`. Please use `Module.constants(false)` instead. - - *Yuichiro Kaneko* - -* Publish `ActiveSupport::Executor` and `ActiveSupport::Reloader` APIs to allow - components and libraries to manage, and participate in, the execution of - application code, and the application reloading process. - - *Matthew Draper* - - -## Rails 5.0.0.beta3 (February 24, 2016) ## - -* Deprecate arguments on `assert_nothing_raised`. - - `assert_nothing_raised` does not assert the arguments that have been passed - in (usually a specific exception class) since the method only yields the - block. So as not to confuse the users that the arguments have meaning, they - are being deprecated. - - *Tara Scherner de la Fuente* - -* Make `benchmark('something', silence: true)` actually work. - - *DHH* - -* Add `#on_weekday?` method to `Date`, `Time`, and `DateTime`. - - `#on_weekday?` returns `true` if the receiving date/time does not fall on a Saturday - or Sunday. - - *Vipul A M* - -* Add `Array#second_to_last` and `Array#third_to_last` methods. - - *Brian Christian* - -* Fix regression in `Hash#dig` for HashWithIndifferentAccess. - - *Jon Moss* - - -## Rails 5.0.0.beta2 (February 01, 2016) ## - -* Change `number_to_currency` behavior for checking negativity. - - Used `to_f.negative` instead of using `to_f.phase` for checking negativity - of a number in number_to_currency helper. - This change works same for all cases except when number is "-0.0". - - -0.0.to_f.negative? => false - -0.0.to_f.phase? => 3.14 - - This change reverts changes from https://github.com/rails/rails/pull/6512. - But it should be acceptable as we could not find any currency which - supports negative zeros. - - *Prathamesh Sonpatki*, *Rafael Mendonça França* - -* Match `HashWithIndifferentAccess#default`'s behaviour with `Hash#default`. - - *David Cornu* - -* Adds `:exception_object` key to `ActiveSupport::Notifications::Instrumenter` - payload when an exception is raised. - - Adds new key/value pair to payload when an exception is raised: - e.g. `:exception_object => #<RuntimeError: FAIL>`. - - *Ryan T. Hosford* - -* Support extended grapheme clusters and UAX 29. - - *Adam Roben* - -* Add petabyte and exabyte numeric conversion. - - *Akshay Vishnoi* - - -## Rails 5.0.0.beta1 (December 18, 2015) ## - -* Add thread_m/cattr_accessor/reader/writer suite of methods for declaring class and module variables that live per-thread. - This makes it easy to declare per-thread globals that are encapsulated. Note: This is a sharp edge. A wild proliferation - of globals is A Bad Thing. But like other sharp tools, when it's right, it's right. - - Here's an example of a simple event tracking system where the object being tracked needs not pass a creator that it - doesn't need itself along: - - module Current - thread_mattr_accessor :account - thread_mattr_accessor :user - - def self.reset() self.account = self.user = nil end - end - - class ApplicationController < ActionController::Base - before_action :set_current - after_action { Current.reset } + class Partition + def initialize(first_event) + @events = [ first_event ] + end - private - def set_current - Current.account = Account.find(params[:account_id]) - Current.user = Current.account.users.find(params[:user_id]) + def people + if @events.first.detail.people.any? + @events.collect { |e| Array(e.detail.people) }.flatten.uniq + else + @events.collect(&:creator).uniq end - end - - class MessagesController < ApplicationController - def create - @message = Message.create!(message_params) end - end - - class Message < ApplicationRecord - has_many :events - after_create :track_created private - def track_created - events.create! origin: self, action: :create + def respond_to_missing?(name, include_private = false) + @events.respond_to?(name, include_private) end - end - - class Event < ApplicationRecord - belongs_to :creator, class_name: 'User' - before_validation { self.creator ||= Current.user } - end - - *DHH* - - -* Deprecated `Module#qualified_const_` in favour of the builtin Module#const_ - methods. - - *Genadi Samokovarov* - -* Deprecate passing string to define callback. - - *Yuichiro Kaneko* - -* `ActiveSupport::Cache::Store#namespaced_key`, - `ActiveSupport::Cache::MemCachedStore#escape_key`, and - `ActiveSupport::Cache::FileStore#key_file_path` - are deprecated and replaced with `normalize_key` that now calls `super`. - - `ActiveSupport::Cache::LocaleCache#set_cache_value` is deprecated and replaced with `write_cache_value`. - - *Michael Grosser* - -* Implements an evented file watcher to asynchronously detect changes in the - application source code, routes, locales, etc. - - This watcher is disabled by default, applications my enable it in the configuration: - - # config/environments/development.rb - config.file_watcher = ActiveSupport::EventedFileUpdateChecker - - This feature depends on the [listen](https://github.com/guard/listen) gem: - - group :development do - gem 'listen', '~> 3.0.5' - end - - *Puneet Agarwal* and *Xavier Noria* - -* Added `Time.days_in_year` to return the number of days in the given year, or the - current year if no argument is provided. - - *Jon Pascoe* - -* Updated `parameterize` to preserve the case of a string, optionally. - - Example: - - parameterize("Donald E. Knuth", separator: '_') # => "donald_e_knuth" - parameterize("Donald E. Knuth", preserve_case: true) # => "Donald-E-Knuth" - - *Swaathi Kakarla* - -* `HashWithIndifferentAccess.new` respects the default value or proc on objects - that respond to `#to_hash`. `.new_from_hash_copying_default` simply invokes `.new`. - All calls to `.new_from_hash_copying_default` are replaced with `.new`. - - *Gordon Chan* - -* Change Integer#year to return a Fixnum instead of a Float to improve - consistency. - - Integer#years returned a Float while the rest of the accompanying methods - (days, weeks, months, etc.) return a Fixnum. - - Before: - - 1.year # => 31557600.0 - - After: - - 1.year # => 31557600 - - *Konstantinos Rousis* - -* Handle invalid UTF-8 strings when HTML escaping. - - Use `ActiveSupport::Multibyte::Unicode.tidy_bytes` to handle invalid UTF-8 - strings in `ERB::Util.unwrapped_html_escape` and `ERB::Util.html_escape_once`. - Prevents user-entered input passed from a querystring into a form field from - causing invalid byte sequence errors. - *Grey Baker* - -* Update `ActiveSupport::Multibyte::Chars#slice!` to return `nil` if the - arguments are out of bounds, to mirror the behavior of `String#slice!` - - *Gourav Tiwari* - -* Fix `number_to_human` so that 999999999 rounds to "1 Billion" instead of - "1000 Million". - - *Max Jacobson* - -* Fix `ActiveSupport::Deprecation#deprecate_methods` to report using the - current deprecator instance, where applicable. - - *Brandon Dunne* - -* `Cache#fetch` instrumentation marks whether it was a `:hit`. - - *Robin Clowers* - -* `assert_difference` and `assert_no_difference` now returns the result of the - yielded block. - - Example: - - post = assert_difference -> { Post.count }, 1 do - Post.create - end - - *Lucas Mazza* - -* Short-circuit `blank?` on date and time values since they are never blank. - - Fixes #21657. - - *Andrew White* - -* Replaced deprecated `ThreadSafe::Cache` with its successor `Concurrent::Map` now that - the thread_safe gem has been merged into concurrent-ruby. - - *Jerry D'Antonio* - -* Updated Unicode version to 8.0.0 - - *Anshul Sharma* - -* `number_to_currency` and `number_with_delimiter` now accept custom `delimiter_pattern` option - to handle placement of delimiter, to support currency formats like INR - - Example: - - number_to_currency(1230000, delimiter_pattern: /(\d+?)(?=(\d\d)+(\d)(?!\d))/, unit: '₹', format: "%u %n") - # => '₹ 12,30,000.00' - - *Vipul A M* - -* Deprecate `:prefix` option of `number_to_human_size` with no replacement. - - *Jean Boussier* - -* Fix `TimeWithZone#eql?` to properly handle `TimeWithZone` created from `DateTime`: - twz = DateTime.now.in_time_zone - twz.eql?(twz.dup) => true - - Fixes #14178. - - *Roque Pinel* - -* ActiveSupport::HashWithIndifferentAccess `select` and `reject` will now return - enumerator if called without block. - - Fixes #20095. - - *Bernard Potocki* - -* Removed `ActiveSupport::Concurrency::Latch`, superseded by `Concurrent::CountDownLatch` - from the concurrent-ruby gem. - - *Jerry D'Antonio* - -* Fix not calling `#default` on `HashWithIndifferentAccess#to_hash` when only - `default_proc` is set, which could raise. - - *Simon Eskildsen* - -* Fix setting `default_proc` on `HashWithIndifferentAccess#dup`. - - *Simon Eskildsen* - -* Fix a range of values for parameters of the Time#change. - - *Nikolay Kondratyev* - -* Add `Enumerable#pluck` to get the same values from arrays as from ActiveRecord - associations. - - Fixes #20339. - - *Kevin Deisz* - -* Add a bang version to `ActiveSupport::OrderedOptions` get methods which will raise - an `KeyError` if the value is `.blank?`. - - Before: - - if (slack_url = Rails.application.secrets.slack_url).present? - # Do something worthwhile - else - # Raise as important secret password is not specified + def method_missing(method, *args, &block) + @events.send(method, *args, &block) + end end - After: - - slack_url = Rails.application.secrets.slack_url! - - *Aditya Sanghi*, *Gaurish Sharma* - -* Remove deprecated `Class#superclass_delegating_accessor`. - Use `Class#class_attribute` instead. - - *Akshay Vishnoi* - -* Patch `Delegator` to work with `#try`. - - Fixes #5790. - - *Nate Smith* - -* Add `Integer#positive?` and `Integer#negative?` query methods - in the vein of `Fixnum#zero?`. - - This makes it nicer to do things like `bunch_of_numbers.select(&:positive?)`. - - *DHH* - -* Encoding `ActiveSupport::TimeWithZone` to YAML now preserves the timezone information. - - Fixes #9183. - - *Andrew White* - -* Added `ActiveSupport::TimeZone#strptime` to allow parsing times as if - from a given timezone. - - *Paul A Jungwirth* - -* `ActiveSupport::Callbacks#skip_callback` now raises an `ArgumentError` if - an unrecognized callback is removed. - - *Iain Beeston* - -* Added `ActiveSupport::ArrayInquirer` and `Array#inquiry`. - - Wrapping an array in an `ArrayInquirer` gives a friendlier way to check its - contents: - - variants = ActiveSupport::ArrayInquirer.new([:phone, :tablet]) - - variants.phone? # => true - variants.tablet? # => true - variants.desktop? # => false - - variants.any?(:phone, :tablet) # => true - variants.any?(:phone, :desktop) # => true - variants.any?(:desktop, :watch) # => false - - `Array#inquiry` is a shortcut for wrapping the receiving array in an - `ArrayInquirer`. - - *George Claghorn* - -* Deprecate `alias_method_chain` in favour of `Module#prepend` introduced in - Ruby 2.0. - - *Kir Shatrov* + With `Module#delegate_missing_to`, the above is condensed to: -* Added `#without` on `Enumerable` and `Array` to return a copy of an - enumerable without the specified elements. + class Partition + delegate_missing_to :@events - *Todd Bealmear* - -* Fixed a problem where `String#truncate_words` would get stuck with a complex - string. - - *Henrik Nygren* - -* Fixed a roundtrip problem with `AS::SafeBuffer` where primitive-like strings - will be dumped as primitives: - - Before: - - YAML.load ActiveSupport::SafeBuffer.new("Hello").to_yaml # => "Hello" - YAML.load ActiveSupport::SafeBuffer.new("true").to_yaml # => true - YAML.load ActiveSupport::SafeBuffer.new("false").to_yaml # => false - YAML.load ActiveSupport::SafeBuffer.new("1").to_yaml # => 1 - YAML.load ActiveSupport::SafeBuffer.new("1.1").to_yaml # => 1.1 - - After: - - YAML.load ActiveSupport::SafeBuffer.new("Hello").to_yaml # => "Hello" - YAML.load ActiveSupport::SafeBuffer.new("true").to_yaml # => "true" - YAML.load ActiveSupport::SafeBuffer.new("false").to_yaml # => "false" - YAML.load ActiveSupport::SafeBuffer.new("1").to_yaml # => "1" - YAML.load ActiveSupport::SafeBuffer.new("1.1").to_yaml # => "1.1" - - *Godfrey Chan* - -* Enable `number_to_percentage` to keep the number's precision by allowing - `:precision` to be `nil`. - - *Jack Xu* - -* `config_accessor` became a private method, as with Ruby's `attr_accessor`. - - *Akira Matsuda* - -* `AS::Testing::TimeHelpers#travel_to` now changes `DateTime.now` as well as - `Time.now` and `Date.today`. - - *Yuki Nishijima* - -* Add `file_fixture` to `ActiveSupport::TestCase`. - It provides a simple mechanism to access sample files in your test cases. - - By default file fixtures are stored in `test/fixtures/files`. This can be - configured per test-case using the `file_fixture_path` class attribute. - - *Yves Senn* - -* Return value of yielded block in `File.atomic_write`. - - *Ian Ker-Seymer* - -* Duplicate frozen array when assigning it to a `HashWithIndifferentAccess` so - that it doesn't raise a `RuntimeError` when calling `map!` on it in `convert_value`. - - Fixes #18550. - - *Aditya Kapoor* - -* Add missing time zone definitions for Russian Federation and sync them - with `zone.tab` file from tzdata version 2014j (latest). - - *Andrey Novikov* - -* Add `SecureRandom.base58` for generation of random base58 strings. - - *Matthew Draper*, *Guillermo Iguaran* - -* Add `#prev_day` and `#next_day` counterparts to `#yesterday` and - `#tomorrow` for `Date`, `Time`, and `DateTime`. - - *George Claghorn* - -* Add `same_time` option to `#next_week` and `#prev_week` for `Date`, `Time`, - and `DateTime`. - - *George Claghorn* - -* Add `#on_weekend?`, `#next_weekday`, `#prev_weekday` methods to `Date`, - `Time`, and `DateTime`. - - `#on_weekend?` returns `true` if the receiving date/time falls on a Saturday - or Sunday. - - `#next_weekday` returns a new date/time representing the next day that does - not fall on a Saturday or Sunday. - - `#prev_weekday` returns a new date/time representing the previous day that - does not fall on a Saturday or Sunday. - - *George Claghorn* - -* Change the default test order from `:sorted` to `:random`. - - *Rafael Mendonça França* - -* Remove deprecated `ActiveSupport::JSON::Encoding::CircularReferenceError`. - - *Rafael Mendonça França* - -* Remove deprecated methods `ActiveSupport::JSON::Encoding.encode_big_decimal_as_string=` - and `ActiveSupport::JSON::Encoding.encode_big_decimal_as_string`. - - *Rafael Mendonça França* - -* Remove deprecated `ActiveSupport::SafeBuffer#prepend`. - - *Rafael Mendonça França* - -* Remove deprecated methods at `Kernel`. - - `silence_stderr`, `silence_stream`, `capture` and `quietly`. - - *Rafael Mendonça França* - -* Remove deprecated `active_support/core_ext/big_decimal/yaml_conversions` - file. - - *Rafael Mendonça França* - -* Remove deprecated methods `ActiveSupport::Cache::Store.instrument` and - `ActiveSupport::Cache::Store.instrument=`. - - *Rafael Mendonça França* - -* Change the way in which callback chains can be halted. - - The preferred method to halt a callback chain from now on is to explicitly - `throw(:abort)`. - In the past, callbacks could only be halted by explicitly providing a - terminator and by having a callback match the conditions of the terminator. - -* Add `ActiveSupport.halt_callback_chains_on_return_false` - - Setting `ActiveSupport.halt_callback_chains_on_return_false` - to `true` will let an app support the deprecated way of halting Active Record, - and Active Model callback chains by returning `false`. - - Setting the value to `false` will tell the app to ignore any `false` value - returned by those callbacks, and only halt the chain upon `throw(:abort)`. - - When the configuration option is missing, its value is `true`, so older apps - ported to Rails 5.0 will not break (but display a deprecation warning). - For new Rails 5.0 apps, its value is set to `false` in an initializer, so - these apps will support the new behavior by default. - - *claudiob*, *Roque Pinel* - -* Changes arguments and default value of CallbackChain's `:terminator` option - - Chains of callbacks defined without an explicit `:terminator` option will - now be halted as soon as a `before_` callback throws `:abort`. - - Chains of callbacks defined with a `:terminator` option will maintain their - existing behavior of halting as soon as a `before_` callback matches the - terminator's expectation. - - *claudiob* - -* Deprecate `MissingSourceFile` in favor of `LoadError`. - - `MissingSourceFile` was just an alias to `LoadError` and was not being - raised inside the framework. - - *Rafael Mendonça França* - -* Add support for error dispatcher classes in `ActiveSupport::Rescuable`. - Now it acts closer to Ruby's rescue. - - Example: - - class BaseController < ApplicationController - module ErrorDispatcher - def self.===(other) - Exception === other && other.respond_to?(:status) - end + def initialize(first_event) + @events = [ first_event ] end - rescue_from ErrorDispatcher do |error| - render status: error.status, json: { error: error.to_s } + def people + if @events.first.detail.people.any? + @events.collect { |e| Array(e.detail.people) }.flatten.uniq + else + @events.collect(&:creator).uniq + end end end - *Genadi Samokovarov* - -* Add `#verified` and `#valid_message?` methods to `ActiveSupport::MessageVerifier` + *Genadi Samokovarov*, *DHH* - Previously, the only way to decode a message with `ActiveSupport::MessageVerifier` - was to use `#verify`, which would raise an exception on invalid messages. Now - `#verified` can also be used, which returns `nil` on messages that cannot be - decoded. +* Rescuable: If a handler doesn't match the exception, check for handlers + matching the exception's cause. - Previously, there was no way to check if a message's format was valid without - attempting to decode it. `#valid_message?` is a boolean convenience method that - checks whether the message is valid without actually decoding it. - - *Logan Leger* + *Jeremy Daer* -Please check [4-2-stable](https://github.com/rails/rails/blob/4-2-stable/activesupport/CHANGELOG.md) for previous changes. +Please check [5-0-stable](https://github.com/rails/rails/blob/5-0-stable/activesupport/CHANGELOG.md) for previous changes. diff --git a/activesupport/Rakefile b/activesupport/Rakefile index 33ee62aa1b..7b56e36abf 100644 --- a/activesupport/Rakefile +++ b/activesupport/Rakefile @@ -3,7 +3,6 @@ require 'rake/testtask' task :default => :test task :package -task "package:clean" Rake::TestTask.new do |t| t.libs << 'test' diff --git a/activesupport/activesupport.gemspec b/activesupport/activesupport.gemspec index 71fe4d7253..c6ca969844 100644 --- a/activesupport/activesupport.gemspec +++ b/activesupport/activesupport.gemspec @@ -23,5 +23,5 @@ Gem::Specification.new do |s| s.add_dependency 'i18n', '~> 0.7' s.add_dependency 'tzinfo', '~> 1.1' s.add_dependency 'minitest', '~> 5.1' - s.add_dependency 'concurrent-ruby', '~> 1.0' + s.add_dependency 'concurrent-ruby', '~> 1.0', '>= 1.0.2' end diff --git a/activesupport/lib/active_support/array_inquirer.rb b/activesupport/lib/active_support/array_inquirer.rb index f59ddf5403..ea328f603e 100644 --- a/activesupport/lib/active_support/array_inquirer.rb +++ b/activesupport/lib/active_support/array_inquirer.rb @@ -9,8 +9,10 @@ module ActiveSupport # variants.desktop? # => false class ArrayInquirer < Array # Passes each element of +candidates+ collection to ArrayInquirer collection. - # The method returns true if at least one element is the same. If +candidates+ - # collection is not given, method returns true. + # The method returns true if any element from the ArrayInquirer collection + # is equal to the stringified or symbolized form of any element in the +candidates+ collection. + # + # If +candidates+ collection is not given, method returns true. # # variants = ActiveSupport::ArrayInquirer.new([:phone, :tablet]) # diff --git a/activesupport/lib/active_support/core_ext/array/grouping.rb b/activesupport/lib/active_support/core_ext/array/grouping.rb index 34af83d1ab..ea9d85f6e3 100644 --- a/activesupport/lib/active_support/core_ext/array/grouping.rb +++ b/activesupport/lib/active_support/core_ext/array/grouping.rb @@ -89,24 +89,19 @@ class Array # [1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]] # (1..10).to_a.split { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]] def split(value = nil) + arr = self.dup + result = [] if block_given? - inject([[]]) do |results, element| - if yield(element) - results << [] - else - results.last << element - end - - results + while (idx = arr.index { |i| yield i }) + result << arr.shift(idx) + arr.shift end else - arr = self.dup - result = [] while (idx = arr.index(value)) result << arr.shift(idx) arr.shift end - result << arr end + result << arr end end diff --git a/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb b/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb index 22fc7ecf92..074e2eabf8 100644 --- a/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb +++ b/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb @@ -3,10 +3,8 @@ require 'bigdecimal/util' module ActiveSupport module BigDecimalWithDefaultFormat #:nodoc: - DEFAULT_STRING_FORMAT = 'F' - - def to_s(format = nil) - super(format || DEFAULT_STRING_FORMAT) + def to_s(format = 'F') + super(format) end end end diff --git a/activesupport/lib/active_support/core_ext/class/subclasses.rb b/activesupport/lib/active_support/core_ext/class/subclasses.rb index b0f9a8be34..1d8c33b43e 100644 --- a/activesupport/lib/active_support/core_ext/class/subclasses.rb +++ b/activesupport/lib/active_support/core_ext/class/subclasses.rb @@ -26,8 +26,6 @@ class Class # Returns an array with the direct children of +self+. # - # Integer.subclasses # => [Fixnum, Bignum] - # # class Foo; end # class Bar < Foo; end # class Baz < Bar; end diff --git a/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb index 4da7fdd159..6206546672 100644 --- a/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb @@ -294,6 +294,11 @@ module DateAndTime end alias :at_end_of_year :end_of_year + # Returns a Range representing the whole day of the current date/time. + def all_day + beginning_of_day..end_of_day + end + # Returns a Range representing the whole week of the current date/time. # Week starts on start_day, default is <tt>Date.week_start</tt> or <tt>config.week_start</tt> when set. def all_week(start_day = Date.beginning_of_week) diff --git a/activesupport/lib/active_support/core_ext/date_and_time/zones.rb b/activesupport/lib/active_support/core_ext/date_and_time/zones.rb index d29a8db5cf..e2432c8f8a 100644 --- a/activesupport/lib/active_support/core_ext/date_and_time/zones.rb +++ b/activesupport/lib/active_support/core_ext/date_and_time/zones.rb @@ -5,7 +5,7 @@ module DateAndTime # # Time.zone = 'Hawaii' # => 'Hawaii' # Time.utc(2000).in_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00 - # Date.new(2000).in_time_zone # => Sat, 01 Jan 2000 00:00:00 HST -10:00 + # Date.new(2000).in_time_zone # => Sat, 01 Jan 2000 00:00:00 HST -10:00 # # This method is similar to Time#localtime, except that it uses <tt>Time.zone</tt> as the local zone # instead of the operating system's time zone. @@ -14,7 +14,7 @@ module DateAndTime # and the conversion will be based on that zone instead of <tt>Time.zone</tt>. # # Time.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00 - # Date.new(2000).in_time_zone('Alaska') # => Sat, 01 Jan 2000 00:00:00 AKST -09:00 + # Date.new(2000).in_time_zone('Alaska') # => Sat, 01 Jan 2000 00:00:00 AKST -09:00 def in_time_zone(zone = ::Time.zone) time_zone = ::Time.find_zone! zone time = acts_like?(:time) ? self : nil diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 941f20c19d..8ebe758078 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -17,11 +17,12 @@ module Enumerable # The default sum of an empty list is zero. You can override this default: # # [].sum(Payment.new(0)) { |i| i.amount } # => Payment.new(0) - def sum(identity = 0, &block) + def sum(identity = nil, &block) if block_given? map(&block).sum(identity) else - inject(:+) || identity + sum = identity ? inject(identity, :+) : inject(:+) + sum || identity || 0 end end @@ -33,7 +34,9 @@ module Enumerable # => { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...} def index_by if block_given? - Hash[map { |elem| [yield(elem), elem] }] + result = {} + each { |elem| result[yield(elem)] = elem } + result else to_enum(:index_by) { size if respond_to?(:size) } end @@ -91,15 +94,16 @@ end class Range #:nodoc: # Optimize range sum to use arithmetic progression if a block is not given and # we have a range of numeric values. - def sum(identity = 0) + def sum(identity = nil) if block_given? || !(first.is_a?(Integer) && last.is_a?(Integer)) super else actual_last = exclude_end? ? (last - 1) : last if actual_last >= first - (actual_last - first + 1) * (actual_last + first) / 2 + sum = identity || 0 + sum + (actual_last - first + 1) * (actual_last + first) / 2 else - identity + identity || 0 end end end @@ -112,11 +116,15 @@ end # just calling the compat method in the first place. if Array.instance_methods(false).include?(:sum) && !(%w[a].sum rescue false) class Array - remove_method :sum + alias :orig_sum :sum - def sum(*args) #:nodoc: - # Use Enumerable#sum instead. - super + def sum(init = nil, &block) #:nodoc: + if init.is_a?(Numeric) || first.is_a?(Numeric) + init ||= 0 + orig_sum(init, &block) + else + super + end end end end diff --git a/activesupport/lib/active_support/core_ext/hash/compact.rb b/activesupport/lib/active_support/core_ext/hash/compact.rb index 5dc9a05ec7..62ea579c65 100644 --- a/activesupport/lib/active_support/core_ext/hash/compact.rb +++ b/activesupport/lib/active_support/core_ext/hash/compact.rb @@ -1,9 +1,9 @@ class Hash # Returns a hash with non +nil+ values. # - # hash = { a: true, b: false, c: nil} - # hash.compact # => { a: true, b: false} - # hash # => { a: true, b: false, c: nil} + # hash = { a: true, b: false, c: nil } + # hash.compact # => { a: true, b: false } + # hash # => { a: true, b: false, c: nil } # { c: nil }.compact # => {} def compact self.select { |_, value| !value.nil? } @@ -11,9 +11,9 @@ class Hash # Replaces current hash with non +nil+ values. # - # hash = { a: true, b: false, c: nil} - # hash.compact! # => { a: true, b: false} - # hash # => { a: true, b: false} + # hash = { a: true, b: false, c: nil } + # hash.compact! # => { a: true, b: false } + # hash # => { a: true, b: false } def compact! self.reject! { |_, value| value.nil? } end diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb index dd5ebe6d8d..2fc514cfce 100644 --- a/activesupport/lib/active_support/core_ext/hash/conversions.rb +++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb @@ -55,8 +55,7 @@ class Hash # # XML_TYPE_NAMES = { # "Symbol" => "symbol", - # "Fixnum" => "integer", - # "Bignum" => "integer", + # "Integer" => "integer", # "BigDecimal" => "decimal", # "Float" => "float", # "TrueClass" => "boolean", diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index 24450cd221..7f968d10b5 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -148,7 +148,6 @@ class Module # Foo.new("Bar").name # raises NoMethodError: undefined method `name' # # The target method must be public, otherwise it will raise +NoMethodError+. - # def delegate(*methods, to: nil, prefix: nil, allow_nil: nil) unless to raise ArgumentError, 'Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, to: :greeter).' @@ -212,4 +211,68 @@ class Module module_eval(method_def, file, line) end end + + # When building decorators, a common pattern may emerge: + # + # class Partition + # def initialize(first_event) + # @events = [ first_event ] + # end + # + # def people + # if @events.first.detail.people.any? + # @events.collect { |e| Array(e.detail.people) }.flatten.uniq + # else + # @events.collect(&:creator).uniq + # end + # end + # + # private + # def respond_to_missing?(name, include_private = false) + # @events.respond_to?(name, include_private) + # end + # + # def method_missing(method, *args, &block) + # @events.send(method, *args, &block) + # end + # end + # + # With `Module#delegate_missing_to`, the above is condensed to: + # + # class Partition + # delegate_missing_to :@events + # + # def initialize(first_event) + # @events = [ first_event ] + # end + # + # def people + # if @events.first.detail.people.any? + # @events.collect { |e| Array(e.detail.people) }.flatten.uniq + # else + # @events.collect(&:creator).uniq + # end + # end + # end + # + # The target can be anything callable withing the object. E.g. instance + # variables, methods, constants ant the likes. + def delegate_missing_to(target) + target = target.to_s + target = "self.#{target}" if DELEGATION_RESERVED_METHOD_NAMES.include?(target) + + module_eval <<-RUBY, __FILE__, __LINE__ + 1 + def respond_to_missing?(name, include_private = false) + #{target}.respond_to?(name, include_private) + end + + def method_missing(method, *args, &block) + if #{target}.respond_to?(method) + #{target}.public_send(method, *args, &block) + else + super + end + end + RUBY + end end diff --git a/activesupport/lib/active_support/core_ext/numeric/conversions.rb b/activesupport/lib/active_support/core_ext/numeric/conversions.rb index b25925b9d4..6586a351f8 100644 --- a/activesupport/lib/active_support/core_ext/numeric/conversions.rb +++ b/activesupport/lib/active_support/core_ext/numeric/conversions.rb @@ -134,6 +134,12 @@ module ActiveSupport::NumericWithFormat deprecate to_formatted_s: :to_s end -[Fixnum, Bignum, Float, BigDecimal].each do |klass| - klass.prepend(ActiveSupport::NumericWithFormat) +# Ruby 2.4+ unifies Fixnum & Bignum into Integer. +if Integer == Fixnum + Integer.prepend ActiveSupport::NumericWithFormat +else + Fixnum.prepend ActiveSupport::NumericWithFormat + Bignum.prepend ActiveSupport::NumericWithFormat end +Float.prepend ActiveSupport::NumericWithFormat +BigDecimal.prepend ActiveSupport::NumericWithFormat diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb index 699b2fb42b..cb74bad73e 100644 --- a/activesupport/lib/active_support/core_ext/object/blank.rb +++ b/activesupport/lib/active_support/core_ext/object/blank.rb @@ -97,6 +97,8 @@ class Hash end class String + BLANK_RE = /\A[[:space:]]*\z/ + # A string is blank if it's empty or contains whitespaces only: # # ''.blank? # => true @@ -113,10 +115,7 @@ class String # The regexp that matches blank strings is expensive. For the case of empty # strings we can speed up this method (~3.5x) with an empty? call. The # penalty for the rest of strings is marginal. - # - # Double negation in the second operand is also a performance tweak, it is - # faster than the positive \A[[:space:]]*\z. - empty? || !(/[[:^space:]]/ === self) + empty? || BLANK_RE === self end end diff --git a/activesupport/lib/active_support/core_ext/object/duplicable.rb b/activesupport/lib/active_support/core_ext/object/duplicable.rb index befa5aee21..9bc5ee65ba 100644 --- a/activesupport/lib/active_support/core_ext/object/duplicable.rb +++ b/activesupport/lib/active_support/core_ext/object/duplicable.rb @@ -70,7 +70,7 @@ class Numeric # Numbers are not duplicable: # # 3.duplicable? # => false - # 3.dup # => TypeError: can't dup Fixnum + # 3.dup # => TypeError: can't dup Integer def duplicable? false end diff --git a/activesupport/lib/active_support/core_ext/object/try.rb b/activesupport/lib/active_support/core_ext/object/try.rb index 8c16d95b62..3b6d9da216 100644 --- a/activesupport/lib/active_support/core_ext/object/try.rb +++ b/activesupport/lib/active_support/core_ext/object/try.rb @@ -99,7 +99,7 @@ class Object # # "a".try!(:upcase) # => "A" # nil.try!(:upcase) # => nil - # 123.try!(:upcase) # => NoMethodError: undefined method `upcase' for 123:Fixnum + # 123.try!(:upcase) # => NoMethodError: undefined method `upcase' for 123:Integer end class Delegator diff --git a/activesupport/lib/active_support/core_ext/string/access.rb b/activesupport/lib/active_support/core_ext/string/access.rb index ebd0dd3fc7..213a91aa7a 100644 --- a/activesupport/lib/active_support/core_ext/string/access.rb +++ b/activesupport/lib/active_support/core_ext/string/access.rb @@ -1,5 +1,5 @@ class String - # If you pass a single Fixnum, returns a substring of one character at that + # If you pass a single integer, returns a substring of one character at that # position. The first character of the string is at position 0, the next at # position 1, and so on. If a range is supplied, a substring containing # characters at offsets given by the range is returned. In both cases, if an diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index b755726db2..e81b48ab26 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -227,11 +227,6 @@ class Time end alias :at_end_of_minute :end_of_minute - # Returns a Range representing the whole day of the current time. - def all_day - beginning_of_day..end_of_day - end - def plus_with_duration(other) #:nodoc: if ActiveSupport::Duration === other other.since(self) diff --git a/activesupport/lib/active_support/deprecation.rb b/activesupport/lib/active_support/deprecation.rb index 24545d766c..b581710067 100644 --- a/activesupport/lib/active_support/deprecation.rb +++ b/activesupport/lib/active_support/deprecation.rb @@ -32,7 +32,7 @@ module ActiveSupport # and the second is a library name # # ActiveSupport::Deprecation.new('2.0', 'MyLibrary') - def initialize(deprecation_horizon = '5.1', gem_name = 'Rails') + def initialize(deprecation_horizon = '5.2', gem_name = 'Rails') self.gem_name = gem_name self.deprecation_horizon = deprecation_horizon # By default, warnings are not silenced and debugging is off. diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb index 3bde541009..47d09f4f5a 100644 --- a/activesupport/lib/active_support/duration.rb +++ b/activesupport/lib/active_support/duration.rb @@ -159,8 +159,10 @@ module ActiveSupport if t.acts_like?(:time) || t.acts_like?(:date) if type == :seconds t.since(sign * number) - elsif [:hours, :minutes].include?(type) - t.in_time_zone.advance(type => sign * number) + elsif type == :minutes + t.since(sign * number * 60) + elsif type == :hours + t.since(sign * number * 3600) else t.advance(type => sign * number) end diff --git a/activesupport/lib/active_support/gem_version.rb b/activesupport/lib/active_support/gem_version.rb index 4166ffc2fb..74f2d8dd4b 100644 --- a/activesupport/lib/active_support/gem_version.rb +++ b/activesupport/lib/active_support/gem_version.rb @@ -6,9 +6,9 @@ module ActiveSupport module VERSION MAJOR = 5 - MINOR = 0 + MINOR = 1 TINY = 0 - PRE = "beta3" + PRE = "alpha" STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".") end diff --git a/activesupport/lib/active_support/rescuable.rb b/activesupport/lib/active_support/rescuable.rb index 73bc52b56f..2c05deee41 100644 --- a/activesupport/lib/active_support/rescuable.rb +++ b/activesupport/lib/active_support/rescuable.rb @@ -1,7 +1,6 @@ require 'active_support/concern' require 'active_support/core_ext/class/attribute' require 'active_support/core_ext/string/inflections' -require 'active_support/core_ext/array/extract_options' module ActiveSupport # Rescuable module adds support for easier exception handling. @@ -48,14 +47,12 @@ module ActiveSupport # end # # Exceptions raised inside exception handlers are not propagated up. - def rescue_from(*klasses, &block) - options = klasses.extract_options! - - unless options.has_key?(:with) + def rescue_from(*klasses, with: nil, &block) + unless with if block_given? - options[:with] = block + with = block else - raise ArgumentError, "Need a handler. Supply an options hash that has a :with key as the last argument." + raise ArgumentError, 'Need a handler. Pass the with: keyword argument or provide a block.' end end @@ -65,65 +62,104 @@ module ActiveSupport elsif klass.is_a?(String) klass else - raise ArgumentError, "#{klass} is neither an Exception nor a String" + raise ArgumentError, "#{klass.inspect} must be an Exception class or a String referencing an Exception class" end # Put the new handler at the end because the list is read in reverse. - self.rescue_handlers += [[key, options[:with]]] + self.rescue_handlers += [[key, with]] end end - end - # Tries to rescue the exception by looking up and calling a registered handler. - def rescue_with_handler(exception) - if handler = handler_for_rescue(exception) - handler.arity != 0 ? handler.call(exception) : handler.call - true # don't rely on the return value of the handler + # Matches an exception to a handler based on the exception class. + # + # If no handler matches the exception, check for a handler matching the + # (optional) exception.cause. If no handler matches the exception or its + # cause, this returns nil so you can deal with unhandled exceptions. + # Be sure to re-raise unhandled exceptions if this is what you expect. + # + # begin + # … + # rescue => exception + # rescue_with_handler(exception) || raise + # end + # + # Returns the exception if it was handled and nil if it was not. + def rescue_with_handler(exception, object: self) + if handler = handler_for_rescue(exception, object: object) + handler.call exception + exception + end end - end - def handler_for_rescue(exception) - # We go from right to left because pairs are pushed onto rescue_handlers - # as rescue_from declarations are found. - _, rescuer = self.class.rescue_handlers.reverse.detect do |klass_name, handler| - # The purpose of allowing strings in rescue_from is to support the - # declaration of handler associations for exception classes whose - # definition is yet unknown. - # - # Since this loop needs the constants it would be inconsistent to - # assume they should exist at this point. An early raised exception - # could trigger some other handler and the array could include - # precisely a string whose corresponding constant has not yet been - # seen. This is why we are tolerant to unknown constants. - # - # Note that this tolerance only matters if the exception was given as - # a string, otherwise a NameError will be raised by the interpreter - # itself when rescue_from CONSTANT is executed. - klass = self.class.const_get(klass_name) rescue nil - klass ||= (klass_name.constantize rescue nil) - klass === exception if klass + def handler_for_rescue(exception, object: self) #:nodoc: + case rescuer = find_rescue_handler(exception) + when Symbol + method = object.method(rescuer) + if method.arity == 0 + -> e { method.call } + else + method + end + when Proc + if rescuer.arity == 0 + -> e { object.instance_exec(&rescuer) } + else + -> e { object.instance_exec(e, &rescuer) } + end + end end - case rescuer - when Symbol - method(rescuer) - when Proc - if rescuer.arity == 0 - Proc.new { instance_exec(&rescuer) } - else - Proc.new { |_exception| instance_exec(_exception, &rescuer) } + private + def find_rescue_handler(exception) + if exception + # Handlers are in order of declaration but the most recently declared + # is the highest priority match, so we search for matching handlers + # in reverse. + _, handler = rescue_handlers.reverse_each.detect do |class_or_name, _| + if klass = constantize_rescue_handler_class(class_or_name) + klass === exception + end + end + + handler || find_rescue_handler(exception.cause) + end + end + + def constantize_rescue_handler_class(class_or_name) + case class_or_name + when String, Symbol + begin + # Try a lexical lookup first since we support + # + # class Super + # rescue_from 'Error', with: … + # end + # + # class Sub + # class Error < StandardError; end + # end + # + # so an Error raised in Sub will hit the 'Error' handler. + const_get class_or_name + rescue NameError + class_or_name.safe_constantize + end + else + class_or_name + end end - end end - def index_of_handler_for_rescue(exception) - handlers = self.class.rescue_handlers.reverse_each.with_index - _, index = handlers.detect do |(klass_name, _), _| - klass = self.class.const_get(klass_name) rescue nil - klass ||= (klass_name.constantize rescue nil) - klass === exception if klass - end - index + # Delegates to the class method, but uses the instance as the subject for + # rescue_from handlers (method calls, instance_exec blocks). + def rescue_with_handler(exception) + self.class.rescue_with_handler exception, object: self + end + + # Internal handler lookup. Delegates to class method. Some libraries call + # this directly, so keeping it around for compatibility. + def handler_for_rescue(exception) #:nodoc: + self.class.handler_for_rescue exception, object: self end end end diff --git a/activesupport/lib/active_support/xml_mini.rb b/activesupport/lib/active_support/xml_mini.rb index df7b081993..99fc26549e 100644 --- a/activesupport/lib/active_support/xml_mini.rb +++ b/activesupport/lib/active_support/xml_mini.rb @@ -32,20 +32,25 @@ module ActiveSupport "binary" => "base64" } unless defined?(DEFAULT_ENCODINGS) - TYPE_NAMES = { - "Symbol" => "symbol", - "Fixnum" => "integer", - "Bignum" => "integer", - "BigDecimal" => "decimal", - "Float" => "float", - "TrueClass" => "boolean", - "FalseClass" => "boolean", - "Date" => "date", - "DateTime" => "dateTime", - "Time" => "dateTime", - "Array" => "array", - "Hash" => "hash" - } unless defined?(TYPE_NAMES) + unless defined?(TYPE_NAMES) + TYPE_NAMES = { + "Symbol" => "symbol", + "Integer" => "integer", + "BigDecimal" => "decimal", + "Float" => "float", + "TrueClass" => "boolean", + "FalseClass" => "boolean", + "Date" => "date", + "DateTime" => "dateTime", + "Time" => "dateTime", + "Array" => "array", + "Hash" => "hash" + } + + # No need to map these on Ruby 2.4+ + TYPE_NAMES["Fixnum"] = "integer" unless Fixnum == Integer + TYPE_NAMES["Bignum"] = "integer" unless Bignum == Integer + end FORMATTING = { "symbol" => Proc.new { |symbol| symbol.to_s }, diff --git a/activesupport/lib/active_support/xml_mini/rexml.rb b/activesupport/lib/active_support/xml_mini/rexml.rb index 924ed72345..95af5af2c0 100644 --- a/activesupport/lib/active_support/xml_mini/rexml.rb +++ b/activesupport/lib/active_support/xml_mini/rexml.rb @@ -20,11 +20,9 @@ module ActiveSupport data = StringIO.new(data || '') end - char = data.getc - if char.nil? + if data.eof? {} else - data.ungetc(char) silence_warnings { require 'rexml/document' } unless defined?(REXML::Document) doc = REXML::Document.new(data) diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index 7f0fcd6996..bab7440397 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -18,6 +18,9 @@ Thread.abort_on_exception = true # Show backtraces for deprecated behavior for quicker cleanup. ActiveSupport::Deprecation.debug = true +# Default to old to_time behavior but allow running tests with new behavior +ActiveSupport.to_time_preserves_timezone = ENV['PRESERVE_TIMEZONES'] == '1' + # Disable available locale checks to avoid warnings running the test suite. I18n.enforce_available_locales = false diff --git a/activesupport/test/core_ext/array/conversions_test.rb b/activesupport/test/core_ext/array/conversions_test.rb index 507e13f968..de36e2026d 100644 --- a/activesupport/test/core_ext/array/conversions_test.rb +++ b/activesupport/test/core_ext/array/conversions_test.rb @@ -101,10 +101,10 @@ class ToXmlTest < ActiveSupport::TestCase end def test_to_xml_with_non_hash_elements - xml = [1, 2, 3].to_xml(skip_instruct: true, indent: 0) + xml = %w[1 2 3].to_xml(skip_instruct: true, indent: 0) - assert_equal '<fixnums type="array"><fixnum', xml.first(29) - assert xml.include?(%(<fixnum type="integer">2</fixnum>)), xml + assert_equal '<strings type="array"><string', xml.first(29) + assert xml.include?(%(<string>2</string>)), xml end def test_to_xml_with_non_hash_different_type_elements diff --git a/activesupport/test/core_ext/array/grouping_test.rb b/activesupport/test/core_ext/array/grouping_test.rb index fb7367b0bf..0682241f0b 100644 --- a/activesupport/test/core_ext/array/grouping_test.rb +++ b/activesupport/test/core_ext/array/grouping_test.rb @@ -3,11 +3,12 @@ require 'active_support/core_ext/array' class GroupingTest < ActiveSupport::TestCase def setup - Fixnum.send :private, :/ # test we avoid Integer#/ (redefined by mathn) + # In Ruby < 2.4, test we avoid Integer#/ (redefined by mathn) + Fixnum.send :private, :/ unless Fixnum == Integer end def teardown - Fixnum.send :public, :/ + Fixnum.send :public, :/ unless Fixnum == Integer end def test_in_groups_of_with_perfect_fit diff --git a/activesupport/test/core_ext/bigdecimal_test.rb b/activesupport/test/core_ext/bigdecimal_test.rb index 423a3f2e9d..6e82e3892b 100644 --- a/activesupport/test/core_ext/bigdecimal_test.rb +++ b/activesupport/test/core_ext/bigdecimal_test.rb @@ -5,5 +5,7 @@ class BigDecimalTest < ActiveSupport::TestCase def test_to_s bd = BigDecimal.new '0.01' assert_equal '0.01', bd.to_s + assert_equal '+0.01', bd.to_s('+F') + assert_equal '+0.0 1', bd.to_s('+1F') end end diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 932675a50d..8052d38c33 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -284,6 +284,23 @@ class DateExtCalculationsTest < ActiveSupport::TestCase end end + def test_all_day + beginning_of_day = Time.local(2011,6,7,0,0,0) + end_of_day = Time.local(2011,6,7,23,59,59,Rational(999999999, 1000)) + assert_equal beginning_of_day..end_of_day, Date.new(2011,6,7).all_day + end + + def test_all_day_when_zone_is_set + zone = ActiveSupport::TimeZone["Hawaii"] + with_env_tz "UTC" do + with_tz_default zone do + beginning_of_day = zone.local(2011,6,7,0,0,0) + end_of_day = zone.local(2011,6,7,23,59,59,Rational(999999999, 1000)) + assert_equal beginning_of_day..end_of_day, Date.new(2011,6,7).all_day + end + end + end + def test_all_week assert_equal Date.new(2011,6,6)..Date.new(2011,6,12), Date.new(2011,6,7).all_week assert_equal Date.new(2011,6,5)..Date.new(2011,6,11), Date.new(2011,6,7).all_week(:sunday) diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index fcd739c804..306316efcd 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -69,8 +69,14 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase def test_to_time with_env_tz 'US/Eastern' do assert_instance_of Time, DateTime.new(2005, 2, 21, 10, 11, 12, 0).to_time - assert_equal Time.local(2005, 2, 21, 5, 11, 12), DateTime.new(2005, 2, 21, 10, 11, 12, 0).to_time - assert_equal Time.local(2005, 2, 21, 5, 11, 12).utc_offset, DateTime.new(2005, 2, 21, 10, 11, 12, 0).to_time.utc_offset + + if ActiveSupport.to_time_preserves_timezone + assert_equal Time.local(2005, 2, 21, 5, 11, 12).getlocal(0), DateTime.new(2005, 2, 21, 10, 11, 12, 0).to_time + assert_equal Time.local(2005, 2, 21, 5, 11, 12).getlocal(0).utc_offset, DateTime.new(2005, 2, 21, 10, 11, 12, 0).to_time.utc_offset + else + assert_equal Time.local(2005, 2, 21, 5, 11, 12), DateTime.new(2005, 2, 21, 10, 11, 12, 0).to_time + assert_equal Time.local(2005, 2, 21, 5, 11, 12).utc_offset, DateTime.new(2005, 2, 21, 10, 11, 12, 0).to_time.utc_offset + end end end diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb index bef660fe12..502e2811fa 100644 --- a/activesupport/test/core_ext/duration_test.rb +++ b/activesupport/test/core_ext/duration_test.rb @@ -12,7 +12,7 @@ class DurationTest < ActiveSupport::TestCase assert d.is_a?(ActiveSupport::Duration) assert_kind_of ActiveSupport::Duration, d assert_kind_of Numeric, d - assert_kind_of Fixnum, d + assert_kind_of Integer, d assert !d.is_a?(Hash) k = Class.new @@ -88,6 +88,15 @@ class DurationTest < ActiveSupport::TestCase assert_equal 1 + 1.second, 1.second + 1, "Duration + Numeric should == Numeric + Duration" end + def test_time_plus_duration_returns_same_time_datatype + twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone['Moscow'] , Time.utc(2016,4,28,00,45)) + now = Time.now.utc + %w( second minute hour day week month year ).each do |unit| + assert_equal((now + 1.send(unit)).class, Time, "Time + 1.#{unit} must be Time") + assert_equal((twz + 1.send(unit)).class, ActiveSupport::TimeWithZone, "TimeWithZone + 1.#{unit} must be TimeWithZone") + end + end + def test_argument_error e = assert_raise ArgumentError do 1.second.ago('') diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index 976c8b2b81..99c3236c35 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -22,6 +22,11 @@ class EnumerableTests < ActiveSupport::TestCase end end + def assert_typed_equal(e, v, cls, msg=nil) + assert_kind_of(cls, v, msg) + assert_equal(e, v, msg) + end + def test_sums enum = GenericEnumerable.new([5, 15, 10]) assert_equal 30, enum.sum @@ -38,6 +43,40 @@ class EnumerableTests < ActiveSupport::TestCase payments = GenericEnumerable.new([ SummablePayment.new(5), SummablePayment.new(15) ]) assert_equal SummablePayment.new(20), payments.sum assert_equal SummablePayment.new(20), payments.sum { |p| p } + + sum = GenericEnumerable.new([3, 5.quo(1)]).sum + assert_typed_equal(8, sum, Rational) + + sum = GenericEnumerable.new([3, 5.quo(1)]).sum(0.0) + assert_typed_equal(8.0, sum, Float) + + sum = GenericEnumerable.new([3, 5.quo(1), 7.0]).sum + assert_typed_equal(15.0, sum, Float) + + sum = GenericEnumerable.new([3, 5.quo(1), Complex(7)]).sum + assert_typed_equal(Complex(15), sum, Complex) + assert_typed_equal(15, sum.real, Rational) + assert_typed_equal(0, sum.imag, Integer) + + sum = GenericEnumerable.new([3.5, 5]).sum + assert_typed_equal(8.5, sum, Float) + + sum = GenericEnumerable.new([2, 8.5]).sum + assert_typed_equal(10.5, sum, Float) + + sum = GenericEnumerable.new([1.quo(2), 1]).sum + assert_typed_equal(3.quo(2), sum, Rational) + + sum = GenericEnumerable.new([1.quo(2), 1.quo(3)]).sum + assert_typed_equal(5.quo(6), sum, Rational) + + sum = GenericEnumerable.new([2.0, 3.0*Complex::I]).sum + assert_typed_equal(Complex(2.0, 3.0), sum, Complex) + assert_typed_equal(2.0, sum.real, Float) + assert_typed_equal(3.0, sum.imag, Float) + + sum = GenericEnumerable.new([1, 2]).sum(10) {|v| v * 2 } + assert_typed_equal(16, sum, Integer) end def test_nil_sums @@ -55,6 +94,7 @@ class EnumerableTests < ActiveSupport::TestCase assert_equal 0, GenericEnumerable.new([]).sum assert_equal 0, GenericEnumerable.new([]).sum { |i| i + 10 } assert_equal Payment.new(0), GenericEnumerable.new([]).sum(Payment.new(0)) + assert_typed_equal 0.0, GenericEnumerable.new([]).sum(0.0), Float end def test_range_sums @@ -68,6 +108,10 @@ class EnumerableTests < ActiveSupport::TestCase assert_equal 5, (10..0).sum(5) assert_equal 10, (10..10).sum assert_equal 42, (10...10).sum(42) + assert_typed_equal 20.0, (1..4).sum(0.0) { |i| i * 2 }, Float + assert_typed_equal 10.0, (1..4).sum(0.0), Float + assert_typed_equal 20.0, (1..4).sum(10.0), Float + assert_typed_equal 5.0, (10..0).sum(5.0), Float end def test_array_sums @@ -86,6 +130,40 @@ class EnumerableTests < ActiveSupport::TestCase payments = [ SummablePayment.new(5), SummablePayment.new(15) ] assert_equal SummablePayment.new(20), payments.sum assert_equal SummablePayment.new(20), payments.sum { |p| p } + + sum = [3, 5.quo(1)].sum + assert_typed_equal(8, sum, Rational) + + sum = [3, 5.quo(1)].sum(0.0) + assert_typed_equal(8.0, sum, Float) + + sum = [3, 5.quo(1), 7.0].sum + assert_typed_equal(15.0, sum, Float) + + sum = [3, 5.quo(1), Complex(7)].sum + assert_typed_equal(Complex(15), sum, Complex) + assert_typed_equal(15, sum.real, Rational) + assert_typed_equal(0, sum.imag, Integer) + + sum = [3.5, 5].sum + assert_typed_equal(8.5, sum, Float) + + sum = [2, 8.5].sum + assert_typed_equal(10.5, sum, Float) + + sum = [1.quo(2), 1].sum + assert_typed_equal(3.quo(2), sum, Rational) + + sum = [1.quo(2), 1.quo(3)].sum + assert_typed_equal(5.quo(6), sum, Rational) + + sum = [2.0, 3.0*Complex::I].sum + assert_typed_equal(Complex(2.0, 3.0), sum, Complex) + assert_typed_equal(2.0, sum.real, Float) + assert_typed_equal(3.0, sum.imag, Float) + + sum = [1, 2].sum(10) {|v| v * 2 } + assert_typed_equal(16, sum, Integer) end def test_index_by diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index be8583e704..f0a4c4dddc 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -36,12 +36,12 @@ class HashExtTest < ActiveSupport::TestCase def setup @strings = { 'a' => 1, 'b' => 2 } @nested_strings = { 'a' => { 'b' => { 'c' => 3 } } } - @symbols = { :a => 1, :b => 2 } + @symbols = { :a => 1, :b => 2 } @nested_symbols = { :a => { :b => { :c => 3 } } } - @mixed = { :a => 1, 'b' => 2 } - @nested_mixed = { 'a' => { :b => { 'c' => 3 } } } - @fixnums = { 0 => 1, 1 => 2 } - @nested_fixnums = { 0 => { 1 => { 2 => 3} } } + @mixed = { :a => 1, 'b' => 2 } + @nested_mixed = { 'a' => { :b => { 'c' => 3 } } } + @integers = { 0 => 1, 1 => 2 } + @nested_integers = { 0 => { 1 => { 2 => 3} } } @illegal_symbols = { [] => 3 } @nested_illegal_symbols = { [] => { [] => 3} } @upcase_strings = { 'A' => 1, 'B' => 2 } @@ -196,14 +196,14 @@ class HashExtTest < ActiveSupport::TestCase assert_equal @nested_illegal_symbols, @nested_illegal_symbols.deep_dup.deep_symbolize_keys! end - def test_symbolize_keys_preserves_fixnum_keys - assert_equal @fixnums, @fixnums.symbolize_keys - assert_equal @fixnums, @fixnums.dup.symbolize_keys! + def test_symbolize_keys_preserves_integer_keys + assert_equal @integers, @integers.symbolize_keys + assert_equal @integers, @integers.dup.symbolize_keys! end - def test_deep_symbolize_keys_preserves_fixnum_keys - assert_equal @nested_fixnums, @nested_fixnums.deep_symbolize_keys - assert_equal @nested_fixnums, @nested_fixnums.deep_dup.deep_symbolize_keys! + def test_deep_symbolize_keys_preserves_integer_keys + assert_equal @nested_integers, @nested_integers.deep_symbolize_keys + assert_equal @nested_integers, @nested_integers.deep_dup.deep_symbolize_keys! end def test_stringify_keys @@ -299,14 +299,14 @@ class HashExtTest < ActiveSupport::TestCase assert_raise(NoMethodError) { @nested_illegal_symbols.with_indifferent_access.deep_dup.deep_symbolize_keys! } end - def test_symbolize_keys_preserves_fixnum_keys_for_hash_with_indifferent_access - assert_equal @fixnums, @fixnums.with_indifferent_access.symbolize_keys - assert_raise(NoMethodError) { @fixnums.with_indifferent_access.dup.symbolize_keys! } + def test_symbolize_keys_preserves_integer_keys_for_hash_with_indifferent_access + assert_equal @integers, @integers.with_indifferent_access.symbolize_keys + assert_raise(NoMethodError) { @integers.with_indifferent_access.dup.symbolize_keys! } end - def test_deep_symbolize_keys_preserves_fixnum_keys_for_hash_with_indifferent_access - assert_equal @nested_fixnums, @nested_fixnums.with_indifferent_access.deep_symbolize_keys - assert_raise(NoMethodError) { @nested_fixnums.with_indifferent_access.deep_dup.deep_symbolize_keys! } + def test_deep_symbolize_keys_preserves_integer_keys_for_hash_with_indifferent_access + assert_equal @nested_integers, @nested_integers.with_indifferent_access.deep_symbolize_keys + assert_raise(NoMethodError) { @nested_integers.with_indifferent_access.deep_dup.deep_symbolize_keys! } end def test_stringify_keys_for_hash_with_indifferent_access diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index ae4aed0554..566f29b470 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -40,6 +40,12 @@ class Someone < Struct.new(:name, :place) FAILED_DELEGATE_LINE_2 = __LINE__ + 1 delegate :bar, :to => :place, :allow_nil => true + + private + + def private_name + "Private" + end end Invoice = Struct.new(:client) do @@ -83,6 +89,20 @@ Product = Struct.new(:name) do end end +DecoratedTester = Struct.new(:client) do + delegate_missing_to :client +end + +class DecoratedReserved + delegate_missing_to :case + + attr_reader :case + + def initialize(kase) + @case = kase + end +end + class Block def hello? true @@ -173,6 +193,21 @@ class ModuleTest < ActiveSupport::TestCase end end + def test_delegation_target_when_prefix_is_true + assert_nothing_raised do + Name.send :delegate, :go, to: :you, prefix: true + end + assert_nothing_raised do + Name.send :delegate, :go, to: :_you, prefix: true + end + assert_raise(ArgumentError) do + Name.send :delegate, :go, to: :You, prefix: true + end + assert_raise(ArgumentError) do + Name.send :delegate, :go, to: :@you, prefix: true + end + end + def test_delegation_prefix invoice = Invoice.new(@david) assert_equal invoice.client_name, "David" @@ -316,6 +351,30 @@ class ModuleTest < ActiveSupport::TestCase assert has_block.hello? end + def test_delegate_to_missing_with_method + assert_equal "David", DecoratedTester.new(@david).name + end + + def test_delegate_to_missing_with_reserved_methods + assert_equal "David", DecoratedReserved.new(@david).name + end + + def test_delegate_to_missing_does_not_delegate_to_private_methods + e = assert_raises(NoMethodError) do + DecoratedReserved.new(@david).private_name + end + + assert_match(/undefined method `private_name' for/, e.message) + end + + def test_delegate_to_missing_does_not_delegate_to_fake_methods + e = assert_raises(NoMethodError) do + DecoratedReserved.new(@david).my_fake_method + end + + assert_match(/undefined method `my_fake_method' for/, e.message) + end + def test_parent assert_equal Yz::Zy, Yz::Zy::Cd.parent assert_equal Yz, Yz::Zy.parent diff --git a/activesupport/test/core_ext/numeric_ext_test.rb b/activesupport/test/core_ext/numeric_ext_test.rb index 5654aeb4f8..69c30a8a9e 100644 --- a/activesupport/test/core_ext/numeric_ext_test.rb +++ b/activesupport/test/core_ext/numeric_ext_test.rb @@ -387,16 +387,9 @@ class NumericExtFormattingTest < ActiveSupport::TestCase end def test_to_s__injected_on_proper_types - assert_equal Fixnum, 1230.class assert_equal '1.23 Thousand', 1230.to_s(:human) - - assert_equal Float, Float(1230).class assert_equal '1.23 Thousand', Float(1230).to_s(:human) - - assert_equal Bignum, (100**10).class assert_equal '100000 Quadrillion', (100**10).to_s(:human) - - assert_equal BigDecimal, BigDecimal("1000010").class assert_equal '1 Million', BigDecimal("1000010").to_s(:human) end diff --git a/activesupport/test/core_ext/object/deep_dup_test.rb b/activesupport/test/core_ext/object/deep_dup_test.rb index 791b5e7172..aa839201ea 100644 --- a/activesupport/test/core_ext/object/deep_dup_test.rb +++ b/activesupport/test/core_ext/object/deep_dup_test.rb @@ -51,7 +51,7 @@ class DeepDupTest < ActiveSupport::TestCase end def test_deep_dup_with_hash_class_key - hash = { Fixnum => 1 } + hash = { Integer => 1 } dup = hash.deep_dup assert_equal 1, dup.keys.length end diff --git a/activesupport/test/core_ext/object/json_gem_encoding_test.rb b/activesupport/test/core_ext/object/json_gem_encoding_test.rb index 02ab17fb64..2cbb1d590f 100644 --- a/activesupport/test/core_ext/object/json_gem_encoding_test.rb +++ b/activesupport/test/core_ext/object/json_gem_encoding_test.rb @@ -10,7 +10,7 @@ require 'json/encoding_test_cases' # The AS::JSON encoder requires the BigDecimal core_ext, which, unfortunately, # changes the BigDecimal#to_s output, and consequently the JSON gem output. So -# we need to require this unfront to ensure we don't get a false failure, but +# we need to require this upfront to ensure we don't get a false failure, but # ideally we should just fix the BigDecimal core_ext to not change to_s without # arguments. require 'active_support/core_ext/big_decimal' diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index f38b225b38..d68a77680b 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -10,6 +10,7 @@ require 'active_support/core_ext/string/strip' require 'active_support/core_ext/string/output_safety' require 'active_support/core_ext/string/indent' require 'time_zone_test_helpers' +require 'yaml' class StringInflectionsTest < ActiveSupport::TestCase include InflectorTestCases @@ -344,7 +345,7 @@ class StringInflectionsTest < ActiveSupport::TestCase end class StringAccessTest < ActiveSupport::TestCase - test "#at with Fixnum, returns a substring of one character at that position" do + test "#at with Integer, returns a substring of one character at that position" do assert_equal "h", "hello".at(0) end @@ -357,19 +358,19 @@ class StringAccessTest < ActiveSupport::TestCase assert_equal nil, "hello".at(/nonexisting/) end - test "#from with positive Fixnum, returns substring from the given position to the end" do + test "#from with positive Integer, returns substring from the given position to the end" do assert_equal "llo", "hello".from(2) end - test "#from with negative Fixnum, position is counted from the end" do + test "#from with negative Integer, position is counted from the end" do assert_equal "lo", "hello".from(-2) end - test "#to with positive Fixnum, substring from the beginning to the given position" do + test "#to with positive Integer, substring from the beginning to the given position" do assert_equal "hel", "hello".to(2) end - test "#to with negative Fixnum, position is counted from the end" do + test "#to with negative Integer, position is counted from the end" do assert_equal "hell", "hello".to(-2) end @@ -383,14 +384,14 @@ class StringAccessTest < ActiveSupport::TestCase assert_equal 'x', 'x'.first end - test "#first with Fixnum, returns a substring from the beginning to position" do + test "#first with Integer, returns a substring from the beginning to position" do assert_equal "he", "hello".first(2) assert_equal "", "hello".first(0) assert_equal "hello", "hello".first(10) assert_equal 'x', 'x'.first(4) end - test "#first with Fixnum >= string length still returns a new string" do + test "#first with Integer >= string length still returns a new string" do string = "hello" different_string = string.first(5) assert_not_same different_string, string @@ -401,14 +402,14 @@ class StringAccessTest < ActiveSupport::TestCase assert_equal 'x', 'x'.last end - test "#last with Fixnum, returns a substring from the end to position" do + test "#last with Integer, returns a substring from the end to position" do assert_equal "llo", "hello".last(3) assert_equal "hello", "hello".last(10) assert_equal "", "hello".last(0) assert_equal 'x', 'x'.last(4) end - test "#last with Fixnum >= string length still returns a new string" do + test "#last with Integer >= string length still returns a new string" do string = "hello" different_string = string.last(5) assert_not_same different_string, string @@ -463,10 +464,17 @@ class StringConversionsTest < ActiveSupport::TestCase def test_string_to_time_utc_offset with_env_tz "US/Eastern" do - assert_equal 0, "2005-02-27 23:50".to_time(:utc).utc_offset - assert_equal(-18000, "2005-02-27 23:50".to_time.utc_offset) - assert_equal 0, "2005-02-27 22:50 -0100".to_time(:utc).utc_offset - assert_equal(-18000, "2005-02-27 22:50 -0100".to_time.utc_offset) + if ActiveSupport.to_time_preserves_timezone + assert_equal 0, "2005-02-27 23:50".to_time(:utc).utc_offset + assert_equal(-18000, "2005-02-27 23:50".to_time.utc_offset) + assert_equal 0, "2005-02-27 22:50 -0100".to_time(:utc).utc_offset + assert_equal(-3600, "2005-02-27 22:50 -0100".to_time.utc_offset) + else + assert_equal 0, "2005-02-27 23:50".to_time(:utc).utc_offset + assert_equal(-18000, "2005-02-27 23:50".to_time.utc_offset) + assert_equal 0, "2005-02-27 22:50 -0100".to_time(:utc).utc_offset + assert_equal(-18000, "2005-02-27 22:50 -0100".to_time.utc_offset) + end end end @@ -674,7 +682,7 @@ class OutputSafetyTest < ActiveSupport::TestCase assert_equal @string, @string.html_safe end - test "A fixnum is safe by default" do + test "An integer is safe by default" do assert 5.html_safe? end @@ -805,7 +813,7 @@ class OutputSafetyTest < ActiveSupport::TestCase assert_equal ["<p>", "<b>", "<h1>"], @other_string end - test "Concatting a fixnum to safe always yields safe" do + test "Concatting an integer to safe always yields safe" do string = @string.html_safe string = string.concat(13) assert_equal "hello".concat(13), string diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb index 45c88b79cb..ec34bd823d 100644 --- a/activesupport/test/deprecation_test.rb +++ b/activesupport/test/deprecation_test.rb @@ -341,7 +341,7 @@ class DeprecationTest < ActiveSupport::TestCase end def test_default_deprecation_horizon_should_always_bigger_than_current_rails_version - assert ActiveSupport::Deprecation.new.deprecation_horizon > ActiveSupport::VERSION::STRING + assert_operator ActiveSupport::Deprecation.new.deprecation_horizon, :>, ActiveSupport::VERSION::STRING end def test_default_gem_name diff --git a/activesupport/test/inflector_test_cases.rb b/activesupport/test/inflector_test_cases.rb index 14fe97a986..c7dc1dadb6 100644 --- a/activesupport/test/inflector_test_cases.rb +++ b/activesupport/test/inflector_test_cases.rb @@ -270,7 +270,8 @@ module InflectorTestCases "maybe you'll be there" => "Maybe You'll Be There", "¿por qué?" => '¿Por Qué?', "Fred’s" => "Fred’s", - "Fred`s" => "Fred`s" + "Fred`s" => "Fred`s", + ActiveSupport::SafeBuffer.new("confirmation num") => "Confirmation Num" } OrdinalNumbers = { diff --git a/activesupport/test/rescuable_test.rb b/activesupport/test/rescuable_test.rb index bd43ad0797..e42e6d2973 100644 --- a/activesupport/test/rescuable_test.rb +++ b/activesupport/test/rescuable_test.rb @@ -3,9 +3,6 @@ require 'abstract_unit' class WraithAttack < StandardError end -class NuclearExplosion < StandardError -end - class MadRonon < StandardError end @@ -19,6 +16,10 @@ module WeirdError end class Stargate + # Nest this so the 'NuclearExplosion' handler needs a lexical const_get + # to find it. + class NuclearExplosion < StandardError; end + attr_accessor :result include ActiveSupport::Rescuable @@ -57,6 +58,14 @@ class Stargate raise MadRonon.new("dex") end + def fall_back_to_cause + # This exception is the cause and has a handler. + ronanize + rescue + # This is the exception we'll handle that doesn't have a cause. + raise 'unhandled RuntimeError with a handleable cause' + end + def weird StandardError.new.tap do |exc| def exc.weird? @@ -74,7 +83,6 @@ class Stargate def sos_first @result = 'sos_first' end - end class CoolStargate < Stargate @@ -127,4 +135,9 @@ class RescuableTest < ActiveSupport::TestCase result = @cool_stargate.send(:rescue_handlers).collect(&:first) assert_equal expected, result end + + def test_rescue_falls_back_to_exception_cause + @stargate.dispatch :fall_back_to_cause + assert_equal 'unhandled RuntimeError with a handleable cause', @stargate.result + end end diff --git a/activesupport/test/xml_mini/rexml_engine_test.rb b/activesupport/test/xml_mini/rexml_engine_test.rb index f0067ca656..6e9ce7ac11 100644 --- a/activesupport/test/xml_mini/rexml_engine_test.rb +++ b/activesupport/test/xml_mini/rexml_engine_test.rb @@ -22,14 +22,24 @@ class REXMLEngineTest < ActiveSupport::TestCase morning </root> eoxml - assert_equal_rexml(io) + hash = ActiveSupport::XmlMini.parse(io) + assert hash.has_key?('root') + assert hash['root'].has_key?('products') + assert_match "good", hash['root']['__content__'] + products = hash['root']['products'] + assert products.has_key?("__content__") + assert_match 'hello everyone', products['__content__'] + end + + def test_parse_from_empty_string + ActiveSupport::XmlMini.backend = 'REXML' + assert_equal({}, ActiveSupport::XmlMini.parse("")) + end + + def test_parse_from_frozen_string + ActiveSupport::XmlMini.backend = 'REXML' + xml_string = "<root></root>".freeze + assert_equal({"root" => {}}, ActiveSupport::XmlMini.parse(xml_string)) end - private - def assert_equal_rexml(xml) - parsed_xml = ActiveSupport::XmlMini.parse(xml) - xml.rewind if xml.respond_to?(:rewind) - hash = ActiveSupport::XmlMini.with_backend('REXML') { ActiveSupport::XmlMini.parse(xml) } - assert_equal(hash, parsed_xml) - end end |