diff options
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/CHANGELOG.md | 62 | ||||
-rw-r--r-- | activemodel/activemodel.gemspec | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/attribute_methods.rb | 10 | ||||
-rw-r--r-- | activemodel/lib/active_model/gem_version.rb | 6 | ||||
-rw-r--r-- | activemodel/lib/active_model/validations/numericality.rb | 11 | ||||
-rw-r--r-- | activemodel/test/cases/validations/numericality_validation_test.rb | 4 | ||||
-rw-r--r-- | activemodel/test/cases/validations_test.rb | 2 |
7 files changed, 23 insertions, 74 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 5588699d9b..b86e988841 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,61 +1 @@ -* Passwords with spaces only allowed in `ActiveModel::SecurePassword`. - - Presence validation can be used to restore old behavior. - - *Yevhene Shemet* - -* Validate options passed to `ActiveModel::Validations.validate`. - - Preventing, in many cases, the simple mistake of using `validate` instead of `validates`. - - *Sonny Michaud* - -* Deprecate `reset_#{attribute}` in favor of `restore_#{attribute}`. - - These methods may cause confusion with the `reset_changes`, which has - different behaviour. - - *Rafael Mendonça França* - -* Deprecate `ActiveModel::Dirty#reset_changes` in favor of `#clear_changes_information`. - - Method's name is causing confusion with the `reset_#{attribute}` methods. - While `reset_name` sets the value of the name attribute to previous value - `reset_changes` only discards the changes. - - *Rafael Mendonça França* - -* Added `restore_attributes` method to `ActiveModel::Dirty` API which restores - the value of changed attributes to previous value. - - *Igor G.* - -* Allow proc and symbol as values for `only_integer` of `NumericalityValidator` - - *Robin Mehner* - -* `has_secure_password` now verifies that the given password is less than 72 - characters if validations are enabled. - - Fixes #14591. - - *Akshay Vishnoi* - -* Remove deprecated `Validator#setup` without replacement. - - See #10716. - - *Kuldeep Aggarwal* - -* Add plural and singular form for length validator's default messages. - - *Abd ar-Rahman Hamid* - -* Introduce `validate` as an alias for `valid?`. - - This is more intuitive when you want to run validations but don't care about - the return value. - - *Henrik Nyh* - -Please check [4-1-stable](https://github.com/rails/rails/blob/4-1-stable/activemodel/CHANGELOG.md) for previous changes. +Please check [4-2-stable](https://github.com/rails/rails/blob/4-2-stable/activemodel/CHANGELOG.md) for previous changes. diff --git a/activemodel/activemodel.gemspec b/activemodel/activemodel.gemspec index 36e565f692..73600b83fb 100644 --- a/activemodel/activemodel.gemspec +++ b/activemodel/activemodel.gemspec @@ -7,7 +7,7 @@ Gem::Specification.new do |s| s.summary = 'A toolkit for building modeling frameworks (part of Rails).' s.description = 'A toolkit for building modeling frameworks like Active Record. Rich support for attributes, callbacks, validations, serialization, internationalization, and testing.' - s.required_ruby_version = '>= 1.9.3' + s.required_ruby_version = '>= 2.1.0' s.license = 'MIT' diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index ea07c5c039..96be551264 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -353,14 +353,12 @@ module ActiveModel @attribute_method_matchers_cache ||= ThreadSafe::Cache.new(initial_capacity: 4) end - def attribute_method_matcher(method_name) #:nodoc: + def attribute_method_matchers_matching(method_name) #:nodoc: attribute_method_matchers_cache.compute_if_absent(method_name) do # Must try to match prefixes/suffixes first, or else the matcher with no prefix/suffix # will match every time. matchers = attribute_method_matchers.partition(&:plain?).reverse.flatten(1) - match = nil - matchers.detect { |method| match = method.match(method_name) } - match + matchers.map { |method| method.match(method_name) }.compact end end @@ -469,8 +467,8 @@ module ActiveModel # Returns a struct representing the matching attribute method. # The struct's attributes are prefix, base and suffix. def match_attribute_method?(method_name) - match = self.class.send(:attribute_method_matcher, method_name) - match if match && attribute_method?(match.attr_name) + matches = self.class.send(:attribute_method_matchers_matching, method_name) + matches.detect { |match| attribute_method?(match.attr_name) } end def missing_attribute(attr_name, stack) diff --git a/activemodel/lib/active_model/gem_version.rb b/activemodel/lib/active_model/gem_version.rb index 932fe3e5a9..2403242ce6 100644 --- a/activemodel/lib/active_model/gem_version.rb +++ b/activemodel/lib/active_model/gem_version.rb @@ -5,10 +5,10 @@ module ActiveModel end module VERSION - MAJOR = 4 - MINOR = 2 + MAJOR = 5 + MINOR = 0 TINY = 0 - PRE = "beta4" + PRE = "alpha" STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".") end diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index 13d6a966c0..4ba4e3e8f7 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -23,6 +23,10 @@ module ActiveModel raw_value = record.send(before_type_cast) if record.respond_to?(before_type_cast) raw_value ||= value + if record_attribute_changed_in_place?(record, attr_name) + raw_value = value + end + return if options[:allow_nil] && raw_value.nil? unless value = parse_raw_value_as_a_number(raw_value) @@ -86,6 +90,13 @@ module ActiveModel options[:only_integer] end end + + private + + def record_attribute_changed_in_place?(record, attr_name) + record.respond_to?(:attribute_changed_in_place?) && + record.attribute_changed_in_place?(attr_name.to_s) + end end module HelperMethods diff --git a/activemodel/test/cases/validations/numericality_validation_test.rb b/activemodel/test/cases/validations/numericality_validation_test.rb index 3834d327ea..12a22f9c40 100644 --- a/activemodel/test/cases/validations/numericality_validation_test.rb +++ b/activemodel/test/cases/validations/numericality_validation_test.rb @@ -59,7 +59,7 @@ class NumericalityValidationTest < ActiveModel::TestCase def test_validates_numericality_of_with_integer_only_and_proc_as_value Topic.send(:define_method, :allow_only_integers?, lambda { false }) - Topic.validates_numericality_of :approved, only_integer: Proc.new {|topic| topic.allow_only_integers? } + Topic.validates_numericality_of :approved, only_integer: Proc.new(&:allow_only_integers?) invalid!(NIL + BLANK + JUNK) valid!(FLOATS + INTEGERS + BIGDECIMAL + INFINITY) @@ -130,7 +130,7 @@ class NumericalityValidationTest < ActiveModel::TestCase def test_validates_numericality_with_proc Topic.send(:define_method, :min_approved, lambda { 5 }) - Topic.validates_numericality_of :approved, greater_than_or_equal_to: Proc.new {|topic| topic.min_approved } + Topic.validates_numericality_of :approved, greater_than_or_equal_to: Proc.new(&:min_approved) invalid!([3, 4]) valid!([5, 6]) diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index de71bb6f42..98e0266d7e 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -282,7 +282,7 @@ class ValidationsTest < ActiveModel::TestCase ActiveModel::Validations::FormatValidator, ActiveModel::Validations::LengthValidator, ActiveModel::Validations::PresenceValidator - ], validators.map { |v| v.class }.sort_by { |c| c.to_s } + ], validators.map(&:class).sort_by(&:to_s) end def test_list_of_validators_will_be_empty_when_empty |