diff options
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/CHANGELOG.md | 19 | ||||
-rw-r--r-- | activemodel/Rakefile | 2 | ||||
-rw-r--r-- | activemodel/examples/validations.rb | 30 | ||||
-rw-r--r-- | activemodel/lib/active_model/gem_version.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/secure_password.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/validations.rb | 8 | ||||
-rw-r--r-- | activemodel/test/cases/helper.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/validations_test.rb | 4 |
8 files changed, 25 insertions, 44 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index c14b0688c7..5588699d9b 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -12,18 +12,21 @@ * Deprecate `reset_#{attribute}` in favor of `restore_#{attribute}`. - These methods may cause confusion with the `reset_changes` that behaves differently - of them. + 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`. - This method name is causing confusion with the `reset_#{attribute}` - methods. While `reset_name` set the value of the name attribute for the - previous value `reset_changes` only discard the changes and previous - changes. + 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 to restore all the - changed values to the previous data. +* Added `restore_attributes` method to `ActiveModel::Dirty` API which restores + the value of changed attributes to previous value. *Igor G.* diff --git a/activemodel/Rakefile b/activemodel/Rakefile index 1e90305a8c..407dda2ec3 100644 --- a/activemodel/Rakefile +++ b/activemodel/Rakefile @@ -6,7 +6,7 @@ task :default => :test Rake::TestTask.new do |t| t.libs << "test" - t.test_files = Dir.glob("#{dir}/test/cases/**/*_test.rb") + t.test_files = Dir.glob("#{dir}/test/cases/**/*_test.rb").sort t.warning = true t.verbose = true end diff --git a/activemodel/examples/validations.rb b/activemodel/examples/validations.rb deleted file mode 100644 index b8e74acd5e..0000000000 --- a/activemodel/examples/validations.rb +++ /dev/null @@ -1,30 +0,0 @@ -require File.expand_path('../../../load_paths', __FILE__) -require 'active_model' - -class Person - include ActiveModel::Conversion - include ActiveModel::Validations - - validates :name, presence: true - - attr_accessor :name - - def initialize(attributes = {}) - @name = attributes[:name] - end - - def persist - @persisted = true - end - - def persisted? - @persisted - end -end - -person1 = Person.new -p person1.valid? # => false -p person1.errors.messages # => {:name=>["can't be blank"]} - -person2 = Person.new(name: 'matz') -p person2.valid? # => true diff --git a/activemodel/lib/active_model/gem_version.rb b/activemodel/lib/active_model/gem_version.rb index dd652190f7..e37edcf581 100644 --- a/activemodel/lib/active_model/gem_version.rb +++ b/activemodel/lib/active_model/gem_version.rb @@ -8,7 +8,7 @@ module ActiveModel MAJOR = 4 MINOR = 2 TINY = 0 - PRE = "beta1" + PRE = "beta2" STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".") end diff --git a/activemodel/lib/active_model/secure_password.rb b/activemodel/lib/active_model/secure_password.rb index f6ad35769f..8f2a069ba3 100644 --- a/activemodel/lib/active_model/secure_password.rb +++ b/activemodel/lib/active_model/secure_password.rb @@ -75,7 +75,7 @@ module ActiveModel end validates_length_of :password, maximum: ActiveModel::SecurePassword::MAX_PASSWORD_LENGTH_ALLOWED - validates_confirmation_of :password, if: ->{ password.present? } + validates_confirmation_of :password, allow_blank: true end # This code is necessary as long as the protected_attributes gem is supported. diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 7ee033ba5f..60439f5631 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -86,6 +86,8 @@ module ActiveModel validates_with BlockValidator, _merge_attributes(attr_names), &block end + VALID_OPTIONS_FOR_VALIDATE = [:on, :if, :unless].freeze + # Adds a validation method or block to the class. This is useful when # overriding the +validate+ instance method becomes too unwieldy and # you're looking for more descriptive declaration of your validations. @@ -144,7 +146,11 @@ module ActiveModel options = args.extract_options! if args.all? { |arg| arg.is_a?(Symbol) } - options.assert_valid_keys([:on, :if, :unless]) + options.each_key do |k| + unless VALID_OPTIONS_FOR_VALIDATE.include?(k) + raise ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{VALID_OPTIONS_FOR_VALIDATE.map(&:inspect).join(', ')}. Perhaps you meant to call `validates` instead of `validate`?") + end + end end if options.key?(:on) diff --git a/activemodel/test/cases/helper.rb b/activemodel/test/cases/helper.rb index 5e80353836..4ce6103593 100644 --- a/activemodel/test/cases/helper.rb +++ b/activemodel/test/cases/helper.rb @@ -17,4 +17,4 @@ require 'mocha/setup' # FIXME: stop using mocha # FIXME: we have tests that depend on run order, we should fix that and # remove this method call. require 'active_support/test_case' -ActiveSupport::TestCase.my_tests_are_order_dependent! +ActiveSupport::TestCase.test_order = :sorted diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index d876f73052..de71bb6f42 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -167,10 +167,12 @@ class ValidationsTest < ActiveModel::TestCase end def test_invalid_options_to_validate - assert_raises(ArgumentError) do + error = assert_raises(ArgumentError) do # A common mistake -- we meant to call 'validates' Topic.validate :title, presence: true end + message = 'Unknown key: :presence. Valid keys are: :on, :if, :unless. Perhaps you meant to call `validates` instead of `validate`?' + assert_equal message, error.message end def test_errors_conversions |