From 6a23bf0f4c33151e0cec0648e271dc6f5ab3f686 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 19 Aug 2014 19:32:51 -0700 Subject: Preparing for 4.2.0.beta1 release --- activesupport/lib/active_support/gem_version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/gem_version.rb b/activesupport/lib/active_support/gem_version.rb index 83a3bf7a5d..e026c4f899 100644 --- a/activesupport/lib/active_support/gem_version.rb +++ b/activesupport/lib/active_support/gem_version.rb @@ -8,7 +8,7 @@ module ActiveSupport MAJOR = 4 MINOR = 2 TINY = 0 - PRE = "alpha" + PRE = "beta1" STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".") end -- cgit v1.2.3 From 141d864e0ec1098176141e6cb6aef3eaf07e830f Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 29 Aug 2014 14:58:36 -0700 Subject: Added yield to Object#presence --- activesupport/lib/active_support/core_ext/object/blank.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb index 38e43478df..164a3c47d0 100644 --- a/activesupport/lib/active_support/core_ext/object/blank.rb +++ b/activesupport/lib/active_support/core_ext/object/blank.rb @@ -39,9 +39,20 @@ class Object # # region = params[:state].presence || params[:country].presence || 'US' # + # You can also use this with a block that will be yielded if the object is present + # and the result of that block will then be returned + # + # person.presence { |p| p.name.first } || 'Nobody' + # # @return [Object] def presence - self if present? + if present? + if block_given? + yield self + else + self + end + end end end -- cgit v1.2.3 From cc0d8fbec96aeb1664fe98d45929a236b18aec81 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 29 Aug 2014 15:04:37 -0700 Subject: Update examples to show real worth --- activesupport/lib/active_support/core_ext/object/blank.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb index 164a3c47d0..893858fcd6 100644 --- a/activesupport/lib/active_support/core_ext/object/blank.rb +++ b/activesupport/lib/active_support/core_ext/object/blank.rb @@ -42,7 +42,7 @@ class Object # You can also use this with a block that will be yielded if the object is present # and the result of that block will then be returned # - # person.presence { |p| p.name.first } || 'Nobody' + # project.account.owner.presence { |p| p.name.first } || 'Nobody' # # @return [Object] def presence -- cgit v1.2.3 From 961945046848492ddc541700419cf553e7817c94 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 29 Aug 2014 15:19:32 -0700 Subject: Use instance_eval on @tenderlove's suggestion :trollface: --- activesupport/lib/active_support/core_ext/object/blank.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb index 893858fcd6..99fd22ff56 100644 --- a/activesupport/lib/active_support/core_ext/object/blank.rb +++ b/activesupport/lib/active_support/core_ext/object/blank.rb @@ -40,15 +40,16 @@ class Object # region = params[:state].presence || params[:country].presence || 'US' # # You can also use this with a block that will be yielded if the object is present - # and the result of that block will then be returned + # and the result of that block will then be returned. The block itself is run against + # the instance you're running #presence on (using instance_eval) # - # project.account.owner.presence { |p| p.name.first } || 'Nobody' + # project.account.owner.presence { name.first } || 'Nobody' # # @return [Object] - def presence + def presence(&block) if present? if block_given? - yield self + instance_eval(&block) else self end -- cgit v1.2.3 From 5e51bdda59c9ba8e5faf86294e3e431bd45f1830 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 29 Aug 2014 15:32:24 -0700 Subject: We tenderized the wrong method! Object#try already had the yield option, just needed some tenderloving instance_eval to fit the bill --- .../lib/active_support/core_ext/object/blank.rb | 16 ++-------------- activesupport/lib/active_support/core_ext/object/try.rb | 11 ++++++++++- 2 files changed, 12 insertions(+), 15 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb index 99fd22ff56..38e43478df 100644 --- a/activesupport/lib/active_support/core_ext/object/blank.rb +++ b/activesupport/lib/active_support/core_ext/object/blank.rb @@ -39,21 +39,9 @@ class Object # # region = params[:state].presence || params[:country].presence || 'US' # - # You can also use this with a block that will be yielded if the object is present - # and the result of that block will then be returned. The block itself is run against - # the instance you're running #presence on (using instance_eval) - # - # project.account.owner.presence { name.first } || 'Nobody' - # # @return [Object] - def presence(&block) - if present? - if block_given? - instance_eval(&block) - else - self - end - end + def presence + self if present? end end diff --git a/activesupport/lib/active_support/core_ext/object/try.rb b/activesupport/lib/active_support/core_ext/object/try.rb index 48190e1e66..31919474ed 100644 --- a/activesupport/lib/active_support/core_ext/object/try.rb +++ b/activesupport/lib/active_support/core_ext/object/try.rb @@ -33,6 +33,11 @@ class Object # ... # end # + # You can also call try with a block without accepting an argument, and the block + # will be instance_eval'ed instead: + # + # @person.try { upcase.truncate(50) } + # # Please also note that +try+ is defined on +Object+, therefore it won't work # with instances of classes that do not have +Object+ among their ancestors, # like direct subclasses of +BasicObject+. For example, using +try+ with @@ -40,7 +45,11 @@ class Object # delegator itself. def try(*a, &b) if a.empty? && block_given? - yield self + if b.arity.zero? + instance_eval(&b) + else + yield self + end else public_send(*a, &b) if respond_to?(a.first) end -- cgit v1.2.3 From 365aa654d8bbe2889b75df2f5147bc25c54fa751 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 29 Aug 2014 18:19:30 -0700 Subject: reduce object allocations in utc_offset `try` allocates an array on every call, we should avoid calling it in hotspots. This reduced AttributeMethodsTest#test_setting_time_zone_aware_attribute_with_string from 18k allocations to 14k --- activesupport/lib/active_support/values/time_zone.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index ee62523824..49dfee96ec 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -276,8 +276,8 @@ module ActiveSupport if @utc_offset @utc_offset else - @current_period ||= tzinfo.try(:current_period) - @current_period.try(:utc_offset) + @current_period ||= tzinfo.current_period if tzinfo + @current_period.utc_offset if @current_period end end -- cgit v1.2.3 From 84c0f73c8daf50fa98d1c4a0c1bab8708e49d0e4 Mon Sep 17 00:00:00 2001 From: Robin Dupret Date: Sat, 30 Aug 2014 11:58:23 +0200 Subject: Refer to the library name instead of the constant When we are loading a component and we want to know its version, we are actually not speaking about the constant but the library itself. [ci skip] [Godfrey Chan & Xavier Noria] --- activesupport/lib/active_support/gem_version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/gem_version.rb b/activesupport/lib/active_support/gem_version.rb index e026c4f899..0a2b4c1618 100644 --- a/activesupport/lib/active_support/gem_version.rb +++ b/activesupport/lib/active_support/gem_version.rb @@ -1,5 +1,5 @@ module ActiveSupport - # Returns the version of the currently loaded ActiveSupport as a Gem::Version + # Returns the version of the currently loaded Active Support as a Gem::Version def self.gem_version Gem::Version.new VERSION::STRING end -- cgit v1.2.3 From 6e0f273dfd4dc30ec4fa1c6db1079f5fd3f717ff Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Sun, 10 Aug 2014 14:56:03 +0800 Subject: Use `safe_constantize`. Fixes https://github.com/rails/rails/issues/9933. --- activesupport/lib/active_support/testing/constant_lookup.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/testing/constant_lookup.rb b/activesupport/lib/active_support/testing/constant_lookup.rb index 1b2a75c35d..07d477c0db 100644 --- a/activesupport/lib/active_support/testing/constant_lookup.rb +++ b/activesupport/lib/active_support/testing/constant_lookup.rb @@ -36,12 +36,8 @@ module ActiveSupport while names.size > 0 do names.last.sub!(/Test$/, "") begin - constant = names.join("::").constantize + constant = names.join("::").safe_constantize break(constant) if yield(constant) - rescue NoMethodError # subclass of NameError - raise - rescue NameError - # Constant wasn't found, move on ensure names.pop end -- cgit v1.2.3 From fdc5e768ca29e3ad4288dcd8a8c7147cea8a1f97 Mon Sep 17 00:00:00 2001 From: Peter Jaros Date: Wed, 3 Sep 2014 16:00:06 -0400 Subject: Methods are not duplicable. --- activesupport/lib/active_support/core_ext/object/duplicable.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/object/duplicable.rb b/activesupport/lib/active_support/core_ext/object/duplicable.rb index c5d59128e5..665cb0f96d 100644 --- a/activesupport/lib/active_support/core_ext/object/duplicable.rb +++ b/activesupport/lib/active_support/core_ext/object/duplicable.rb @@ -91,3 +91,13 @@ class BigDecimal # can't dup, so use superclass implementation end end + +class Method + # Methods are not duplicable: + # + # method(:puts).duplicable? # => false + # method(:puts).dup # => TypeError: allocator undefined for Method + def duplicable? + false + end +end -- cgit v1.2.3 From 57b2c371f03982813f6dc2e7f07467b4fca3a6ce Mon Sep 17 00:00:00 2001 From: Agis- Date: Mon, 1 Sep 2014 15:54:29 +0300 Subject: Time#change throws exception with an out-of-range :usec https://github.com/rails/rails/commit/98b46bf5e201307cae56ee14bf41363a539779c5 did not properly handled out-of-range `:usec`s. Passing a `:usec` that's out of range now throws an `ArgumentError` as it should. Fixes #16759. --- activesupport/lib/active_support/core_ext/time/calculations.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 89cd7516cd..6a7bf7445a 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -87,6 +87,7 @@ class Time elsif zone ::Time.local(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec) else + raise ArgumentError, 'argument out of range' if new_usec > 999999 ::Time.new(new_year, new_month, new_day, new_hour, new_min, new_sec + (new_usec.to_r / 1000000), utc_offset) end end -- cgit v1.2.3 From ccbb48196efe06a0c1c360951caff74ee74a8d14 Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Sat, 6 Sep 2014 04:56:25 +0930 Subject: Fix for inflector's incorrect camelCase replacement for acronyms Fixes #8015, #9756. [Fred Wu & Matthew Draper] --- activesupport/lib/active_support/inflector/methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb index 53022de549..f35e71ce81 100644 --- a/activesupport/lib/active_support/inflector/methods.rb +++ b/activesupport/lib/active_support/inflector/methods.rb @@ -91,7 +91,7 @@ module ActiveSupport def underscore(camel_cased_word) return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/ word = camel_cased_word.to_s.gsub('::', '/') - word.gsub!(/(?:([A-Za-z\d])|^)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" } + word.gsub!(/(?:([A-Za-z\d])|\b)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" } word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") -- cgit v1.2.3 From 2b41343c34bcbe809537590152506690b84832df Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Mon, 8 Sep 2014 05:32:16 -0700 Subject: Default to sorting user's test cases for now Goals: 1. Default to :random for newly generated applications 2. Default to :sorted for existing applications with a warning 3. Only show the warning once 4. Only show the warning if the app actually uses AS::TestCase Fixes #16769 --- activesupport/lib/active_support/test_case.rb | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb index 0df599b692..33139320fa 100644 --- a/activesupport/lib/active_support/test_case.rb +++ b/activesupport/lib/active_support/test_case.rb @@ -12,10 +12,42 @@ require 'active_support/core_ext/kernel/reporting' require 'active_support/deprecation' module ActiveSupport + class << self + delegate :test_order, :test_order=, to: :'ActiveSupport::TestCase' + end + class TestCase < ::Minitest::Test Assertion = Minitest::Assertion + @@test_order = nil + class << self + def test_order=(new_order) + @@test_order = new_order + end + + def test_order + if @@test_order.nil? + ActiveSupport::Deprecation.warn "You did not specify a value for the " \ + "configuration option 'active_support.test_order'. In Rails 5.0, " \ + "the default value of this option will change from `:sorted` to " \ + "`:random`.\n" \ + "To disable this warning and keep the current behavior, you can add " \ + "the following line to your `config/environments/test.rb`:\n" \ + "\n" \ + " Rails.application.configure do\n" \ + " config.active_support.test_order = :sorted\n" \ + " end\n" \ + "\n" \ + "Alternatively, you can opt into the future behavior by setting this " \ + "option to `:random`." + + @@test_order = :sorted + end + + @@test_order + end + alias :my_tests_are_order_dependent! :i_suck_and_my_tests_are_order_dependent! end -- cgit v1.2.3 From 53e877f7d9291b2bf0b8c425f9e32ef35829f35b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 11 Sep 2014 00:54:43 -0300 Subject: Define the configuration at Active Support --- activesupport/lib/active_support.rb | 10 ++++++++++ activesupport/lib/active_support/test_case.rb | 17 +++++++---------- 2 files changed, 17 insertions(+), 10 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index ab0054b339..94468240a4 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -70,6 +70,16 @@ module ActiveSupport NumberHelper.eager_load! end + + @@test_order = nil + + def self.test_order=(new_order) + @@test_order = new_order + end + + def self.test_order + @@test_order + end end autoload :I18n, "active_support/i18n" diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb index 33139320fa..4c3e77b7fd 100644 --- a/activesupport/lib/active_support/test_case.rb +++ b/activesupport/lib/active_support/test_case.rb @@ -12,22 +12,18 @@ require 'active_support/core_ext/kernel/reporting' require 'active_support/deprecation' module ActiveSupport - class << self - delegate :test_order, :test_order=, to: :'ActiveSupport::TestCase' - end - class TestCase < ::Minitest::Test Assertion = Minitest::Assertion - @@test_order = nil - class << self def test_order=(new_order) - @@test_order = new_order + ActiveSupport.test_order = new_order end def test_order - if @@test_order.nil? + test_order = ActiveSupport.test_order + + if test_order.nil? ActiveSupport::Deprecation.warn "You did not specify a value for the " \ "configuration option 'active_support.test_order'. In Rails 5.0, " \ "the default value of this option will change from `:sorted` to " \ @@ -42,10 +38,11 @@ module ActiveSupport "Alternatively, you can opt into the future behavior by setting this " \ "option to `:random`." - @@test_order = :sorted + test_order = :sorted + self.test_order = test_order end - @@test_order + test_order end alias :my_tests_are_order_dependent! :i_suck_and_my_tests_are_order_dependent! -- cgit v1.2.3 From 4bf9d1938bb144ef2a6c3d03a2d01add62e90114 Mon Sep 17 00:00:00 2001 From: Kostiantyn Kahanskyi Date: Fri, 12 Sep 2014 14:58:04 +0200 Subject: MessageVerifier raises an appropriate exception if the secret is nil Otherwise this will lead to another error later on when generating a signature: TypeError (no implicit conversion of nil into String). --- activesupport/lib/active_support/message_verifier.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/message_verifier.rb b/activesupport/lib/active_support/message_verifier.rb index 8e6e1dcfeb..41e2e5a88f 100644 --- a/activesupport/lib/active_support/message_verifier.rb +++ b/activesupport/lib/active_support/message_verifier.rb @@ -27,6 +27,7 @@ module ActiveSupport class InvalidSignature < StandardError; end def initialize(secret, options = {}) + raise ArgumentError, 'Secret should not be nil.' if secret.nil? @secret = secret @digest = options[:digest] || 'SHA1' @serializer = options[:serializer] || Marshal -- cgit v1.2.3 From 1ac20c59f305b9375add46f81b233f93a94ae70d Mon Sep 17 00:00:00 2001 From: Kostiantyn Kahanskyi Date: Fri, 12 Sep 2014 18:47:33 +0200 Subject: Changes "if secret.nil?" to unless secret in MessageVerfier --- activesupport/lib/active_support/message_verifier.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/message_verifier.rb b/activesupport/lib/active_support/message_verifier.rb index 41e2e5a88f..6cb2884fb7 100644 --- a/activesupport/lib/active_support/message_verifier.rb +++ b/activesupport/lib/active_support/message_verifier.rb @@ -27,7 +27,7 @@ module ActiveSupport class InvalidSignature < StandardError; end def initialize(secret, options = {}) - raise ArgumentError, 'Secret should not be nil.' if secret.nil? + raise ArgumentError, 'Secret should not be nil.' unless secret @secret = secret @digest = options[:digest] || 'SHA1' @serializer = options[:serializer] || Marshal -- cgit v1.2.3