diff options
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activemodel/lib/active_model.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 12 | ||||
-rw-r--r-- | activemodel/lib/active_model/type.rb | 28 | ||||
-rw-r--r-- | activemodel/lib/active_model/type/big_integer.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/type/date_time.rb | 4 | ||||
-rw-r--r-- | activemodel/lib/active_model/type/helpers.rb | 8 | ||||
-rw-r--r-- | activemodel/lib/active_model/type/string.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/validations/exclusion.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/validations/inclusion.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/dirty_test.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/errors_test.rb | 12 | ||||
-rw-r--r-- | activemodel/test/cases/type/string_test.rb | 2 |
13 files changed, 57 insertions, 25 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 2916e5eabb..048c43f2c4 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,3 +1,7 @@ +* Add method `#merge!` for `ActiveModel::Errors`. + + *Jahfer Husain* + * Fix regression in numericality validator when comparing Decimal and Float input values with more scale than the schema. diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb index a2892e9ea9..ba1d2fbd44 100644 --- a/activemodel/lib/active_model.rb +++ b/activemodel/lib/active_model.rb @@ -23,7 +23,7 @@ require "active_support" require "active_support/rails" -require "active_model/version" +require_relative "active_model/version" module ActiveModel extend ActiveSupport::Autoload diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 942b4fa9bb..76c23df541 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -93,6 +93,18 @@ module ActiveModel @details = other.details.dup end + # Merges the errors from <tt>other</tt>. + # + # other - The ActiveModel::Errors instance. + # + # Examples + # + # person.errors.merge!(other) + def merge!(other) + @messages.merge!(other.messages) { |_, ary1, ary2| ary1 + ary2 } + @details.merge!(other.details) { |_, ary1, ary2| ary1 + ary2 } + end + # Clear the error messages. # # person.errors.full_messages # => ["name cannot be nil"] diff --git a/activemodel/lib/active_model/type.rb b/activemodel/lib/active_model/type.rb index 095801d8f0..1741a67a4d 100644 --- a/activemodel/lib/active_model/type.rb +++ b/activemodel/lib/active_model/type.rb @@ -1,19 +1,19 @@ -require "active_model/type/helpers" -require "active_model/type/value" +require_relative "type/helpers" +require_relative "type/value" -require "active_model/type/big_integer" -require "active_model/type/binary" -require "active_model/type/boolean" -require "active_model/type/date" -require "active_model/type/date_time" -require "active_model/type/decimal" -require "active_model/type/float" -require "active_model/type/immutable_string" -require "active_model/type/integer" -require "active_model/type/string" -require "active_model/type/time" +require_relative "type/big_integer" +require_relative "type/binary" +require_relative "type/boolean" +require_relative "type/date" +require_relative "type/date_time" +require_relative "type/decimal" +require_relative "type/float" +require_relative "type/immutable_string" +require_relative "type/integer" +require_relative "type/string" +require_relative "type/time" -require "active_model/type/registry" +require_relative "type/registry" module ActiveModel module Type diff --git a/activemodel/lib/active_model/type/big_integer.rb b/activemodel/lib/active_model/type/big_integer.rb index 3b629682fe..f461d7041e 100644 --- a/activemodel/lib/active_model/type/big_integer.rb +++ b/activemodel/lib/active_model/type/big_integer.rb @@ -1,4 +1,4 @@ -require "active_model/type/integer" +require_relative "integer" module ActiveModel module Type diff --git a/activemodel/lib/active_model/type/date_time.rb b/activemodel/lib/active_model/type/date_time.rb index 5cb0077e45..8ad0daa9a6 100644 --- a/activemodel/lib/active_model/type/date_time.rb +++ b/activemodel/lib/active_model/type/date_time.rb @@ -10,6 +10,10 @@ module ActiveModel :datetime end + def serialize(value) + super(cast(value)) + end + private def cast_value(value) diff --git a/activemodel/lib/active_model/type/helpers.rb b/activemodel/lib/active_model/type/helpers.rb index 82cd9ebe98..1fe06ab3d5 100644 --- a/activemodel/lib/active_model/type/helpers.rb +++ b/activemodel/lib/active_model/type/helpers.rb @@ -1,4 +1,4 @@ -require "active_model/type/helpers/accepts_multiparameter_time" -require "active_model/type/helpers/numeric" -require "active_model/type/helpers/mutable" -require "active_model/type/helpers/time_value" +require_relative "helpers/accepts_multiparameter_time" +require_relative "helpers/numeric" +require_relative "helpers/mutable" +require_relative "helpers/time_value" diff --git a/activemodel/lib/active_model/type/string.rb b/activemodel/lib/active_model/type/string.rb index 850cab962b..2fc027d3c4 100644 --- a/activemodel/lib/active_model/type/string.rb +++ b/activemodel/lib/active_model/type/string.rb @@ -1,4 +1,4 @@ -require "active_model/type/immutable_string" +require_relative "immutable_string" module ActiveModel module Type diff --git a/activemodel/lib/active_model/validations/exclusion.rb b/activemodel/lib/active_model/validations/exclusion.rb index b7156ba802..d587079bd3 100644 --- a/activemodel/lib/active_model/validations/exclusion.rb +++ b/activemodel/lib/active_model/validations/exclusion.rb @@ -1,4 +1,4 @@ -require "active_model/validations/clusivity" +require_relative "clusivity" module ActiveModel module Validations diff --git a/activemodel/lib/active_model/validations/inclusion.rb b/activemodel/lib/active_model/validations/inclusion.rb index c6c5bae649..74dac3b7df 100644 --- a/activemodel/lib/active_model/validations/inclusion.rb +++ b/activemodel/lib/active_model/validations/inclusion.rb @@ -1,4 +1,4 @@ -require "active_model/validations/clusivity" +require_relative "clusivity" module ActiveModel module Validations diff --git a/activemodel/test/cases/dirty_test.rb b/activemodel/test/cases/dirty_test.rb index fdd18d7601..0242b96083 100644 --- a/activemodel/test/cases/dirty_test.rb +++ b/activemodel/test/cases/dirty_test.rb @@ -96,7 +96,7 @@ class DirtyTest < ActiveModel::TestCase end test "attribute mutation" do - @model.instance_variable_set("@name", "Yam") + @model.instance_variable_set("@name", "Yam".dup) assert !@model.name_changed? @model.name.replace("Hadad") assert !@model.name_changed? diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 43aee5a814..7383b3e9b0 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -375,6 +375,18 @@ class ErrorsTest < ActiveModel::TestCase assert_equal [:name], person.errors.details.keys end + test "merge errors" do + errors = ActiveModel::Errors.new(Person.new) + errors.add(:name, :invalid) + + person = Person.new + person.errors.add(:name, :blank) + person.errors.merge!(errors) + + assert_equal({ name: ["can't be blank", "is invalid"] }, person.errors.messages) + assert_equal({ name: [{ error: :blank }, { error: :invalid }] }, person.errors.details) + end + test "errors are marshalable" do errors = ActiveModel::Errors.new(Person.new) errors.add(:name, :invalid) diff --git a/activemodel/test/cases/type/string_test.rb b/activemodel/test/cases/type/string_test.rb index 47d412e27e..5bf9fc3527 100644 --- a/activemodel/test/cases/type/string_test.rb +++ b/activemodel/test/cases/type/string_test.rb @@ -14,7 +14,7 @@ module ActiveModel test "cast strings are mutable" do type = Type::String.new - s = "foo" + s = "foo".dup assert_equal false, type.cast(s).frozen? assert_equal false, s.frozen? |