aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/CHANGELOG.md9
-rw-r--r--activemodel/Rakefile2
-rw-r--r--activemodel/examples/validations.rb2
-rw-r--r--activemodel/lib/active_model.rb1
-rw-r--r--activemodel/lib/active_model/attribute_methods.rb8
-rw-r--r--activemodel/lib/active_model/deprecated_mass_assignment_security.rb21
-rw-r--r--activemodel/lib/active_model/errors.rb6
-rw-r--r--activemodel/lib/active_model/secure_password.rb2
-rw-r--r--activemodel/lib/active_model/validations/clusivity.rb7
-rw-r--r--activemodel/lib/active_model/validations/inclusion.rb2
-rw-r--r--activemodel/test/cases/attribute_methods_test.rb11
-rw-r--r--activemodel/test/cases/deprecated_mass_assignment_security_test.rb16
-rw-r--r--activemodel/test/cases/validations/inclusion_validation_test.rb1
13 files changed, 20 insertions, 68 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md
index 6fc34ecd60..0568e5d545 100644
--- a/activemodel/CHANGELOG.md
+++ b/activemodel/CHANGELOG.md
@@ -1,3 +1,12 @@
+* `inclusion` / `exclusion` validations with ranges will only use the faster
+ `Range#cover` for numerical ranges, and the more accurate `Range#include?`
+ for non-numerical ones.
+
+ Fixes range validations like `:a..:f` that used to pass with values like `:be`.
+ Fixes #10593
+
+ *Charles Bergeron*
+
* Fix regression in has_secure_password. When a password is set, but a
confirmation is an empty string, it would incorrectly save.
diff --git a/activemodel/Rakefile b/activemodel/Rakefile
index 45d1587ed6..f72b949c64 100644
--- a/activemodel/Rakefile
+++ b/activemodel/Rakefile
@@ -28,7 +28,7 @@ Gem::PackageTask.new(spec) do |p|
p.gem_spec = spec
end
-desc "Release to gemcutter"
+desc "Release to rubygems"
task :release => :package do
require 'rake/gemcutter'
Rake::Gemcutter::Tasks.new(spec).define
diff --git a/activemodel/examples/validations.rb b/activemodel/examples/validations.rb
index a56ec4db39..2c5cc11f49 100644
--- a/activemodel/examples/validations.rb
+++ b/activemodel/examples/validations.rb
@@ -4,7 +4,7 @@ class Person
include ActiveModel::Conversion
include ActiveModel::Validations
- validates_presence_of :name
+ validates :name, presence: true
attr_accessor :name
diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb
index 3bd5531356..ef4f2514be 100644
--- a/activemodel/lib/active_model.rb
+++ b/activemodel/lib/active_model.rb
@@ -37,7 +37,6 @@ module ActiveModel
autoload :ForbiddenAttributesProtection
autoload :Lint
autoload :Model
- autoload :DeprecatedMassAssignmentSecurity
autoload :Name, 'active_model/naming'
autoload :Naming
autoload :SecurePassword
diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb
index 98cde8ba59..986a7ade81 100644
--- a/activemodel/lib/active_model/attribute_methods.rb
+++ b/activemodel/lib/active_model/attribute_methods.rb
@@ -388,14 +388,6 @@ module ActiveModel
AttributeMethodMatch = Struct.new(:target, :attr_name, :method_name)
def initialize(options = {})
- if options[:prefix] == '' || options[:suffix] == ''
- message = "Specifying an empty prefix/suffix for an attribute method is no longer " \
- "necessary. If the un-prefixed/suffixed version of the method has not been " \
- "defined when `define_attribute_methods` is called, it will be defined " \
- "automatically."
- ActiveSupport::Deprecation.warn message
- end
-
@prefix, @suffix = options.fetch(:prefix, ''), options.fetch(:suffix, '')
@regex = /^(?:#{Regexp.escape(@prefix)})(.*)(?:#{Regexp.escape(@suffix)})$/
@method_missing_target = "#{@prefix}attribute#{@suffix}"
diff --git a/activemodel/lib/active_model/deprecated_mass_assignment_security.rb b/activemodel/lib/active_model/deprecated_mass_assignment_security.rb
deleted file mode 100644
index 1f409c87b9..0000000000
--- a/activemodel/lib/active_model/deprecated_mass_assignment_security.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-module ActiveModel
- module DeprecatedMassAssignmentSecurity # :nodoc:
- extend ActiveSupport::Concern
-
- module ClassMethods # :nodoc:
- def attr_protected(*args)
- raise "`attr_protected` is extracted out of Rails into a gem. " \
- "Please use new recommended protection model for params" \
- "(strong_parameters) or add `protected_attributes` to your " \
- "Gemfile to use old one."
- end
-
- def attr_accessible(*args)
- raise "`attr_accessible` is extracted out of Rails into a gem. " \
- "Please use new recommended protection model for params" \
- "(strong_parameters) or add `protected_attributes` to your " \
- "Gemfile to use old one."
- end
- end
- end
-end
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 0d7efab04b..9341f689de 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -289,7 +289,7 @@ module ActiveModel
# # => NameIsInvalid: name is invalid
#
# person.errors.messages # => {}
- def add(attribute, message = nil, options = {})
+ def add(attribute, message = :invalid, options = {})
message = normalize_message(attribute, message, options)
if exception = options[:strict]
exception = ActiveModel::StrictValidationFailed if exception == true
@@ -331,7 +331,7 @@ module ActiveModel
#
# person.errors.add :name, :blank
# person.errors.added? :name, :blank # => true
- def added?(attribute, message = nil, options = {})
+ def added?(attribute, message = :invalid, options = {})
message = normalize_message(attribute, message, options)
self[attribute].include? message
end
@@ -437,8 +437,6 @@ module ActiveModel
private
def normalize_message(attribute, message, options)
- message ||= :invalid
-
case message
when Symbol
generate_message(attribute, message, options.except(*CALLBACKS_OPTIONS))
diff --git a/activemodel/lib/active_model/secure_password.rb b/activemodel/lib/active_model/secure_password.rb
index e553590671..7156f1bb30 100644
--- a/activemodel/lib/active_model/secure_password.rb
+++ b/activemodel/lib/active_model/secure_password.rb
@@ -15,7 +15,7 @@ module ActiveModel
# argument. You can add more validations by hand if need be.
#
# If you don't need the confirmation validation, just don't set any
- # value to the password_confirmation attribute and the the validation
+ # value to the password_confirmation attribute and the validation
# will not be triggered.
#
# You need to add bcrypt-ruby (~> 3.0.0) to Gemfile to use #has_secure_password:
diff --git a/activemodel/lib/active_model/validations/clusivity.rb b/activemodel/lib/active_model/validations/clusivity.rb
index 49df98d6c1..c5aacb010a 100644
--- a/activemodel/lib/active_model/validations/clusivity.rb
+++ b/activemodel/lib/active_model/validations/clusivity.rb
@@ -31,10 +31,11 @@ module ActiveModel
end
# In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible values in the
- # range for equality, so it may be slow for large ranges. The new <tt>Range#cover?</tt>
- # uses the previous logic of comparing a value with the range endpoints.
+ # range for equality, which is slower but more accurate. <tt>Range#cover?</tt> uses
+ # the previous logic of comparing a value with the range endpoints, which is fast
+ # but is only accurate on numeric ranges.
def inclusion_method(enumerable)
- enumerable.is_a?(Range) ? :cover? : :include?
+ (enumerable.is_a?(Range) && enumerable.first.is_a?(Numeric)) ? :cover? : :include?
end
end
end
diff --git a/activemodel/lib/active_model/validations/inclusion.rb b/activemodel/lib/active_model/validations/inclusion.rb
index 1cfd86efee..24337614c5 100644
--- a/activemodel/lib/active_model/validations/inclusion.rb
+++ b/activemodel/lib/active_model/validations/inclusion.rb
@@ -28,7 +28,7 @@ module ActiveModel
# Configuration options:
# * <tt>:in</tt> - An enumerable object of available items. This can be
# supplied as a proc, lambda or symbol which returns an enumerable. If the
- # enumerable is a range the test is performed with <tt>Range#cover?</tt>,
+ # enumerable is a numerical range the test is performed with <tt>Range#cover?</tt>,
# otherwise with <tt>include?</tt>.
# * <tt>:within</tt> - A synonym(or alias) for <tt>:in</tt>
# * <tt>:message</tt> - Specifies a custom error message (default is: "is
diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb
index 25eb4860e3..e9cb5ccc96 100644
--- a/activemodel/test/cases/attribute_methods_test.rb
+++ b/activemodel/test/cases/attribute_methods_test.rb
@@ -202,17 +202,6 @@ class AttributeMethodsTest < ActiveModel::TestCase
assert_equal 'bar', m.foo_test
end
- test 'explicitly specifying an empty prefix/suffix is deprecated' do
- klass = Class.new(ModelWithAttributes)
-
- assert_deprecated { klass.attribute_method_suffix '' }
- assert_deprecated { klass.attribute_method_prefix '' }
-
- klass.define_attribute_methods(:foo)
-
- assert_equal 'value of foo', klass.new.foo
- end
-
test 'should not interfere with method_missing if the attr has a private/protected method' do
m = ModelWithAttributes2.new
m.attributes = { 'private_method' => '<3', 'protected_method' => 'O_o' }
diff --git a/activemodel/test/cases/deprecated_mass_assignment_security_test.rb b/activemodel/test/cases/deprecated_mass_assignment_security_test.rb
deleted file mode 100644
index c1fe8822cd..0000000000
--- a/activemodel/test/cases/deprecated_mass_assignment_security_test.rb
+++ /dev/null
@@ -1,16 +0,0 @@
-require 'cases/helper'
-require 'models/project'
-
-class DeprecatedMassAssignmentSecurityTest < ActiveModel::TestCase
- def test_attr_accessible_raise_error
- assert_raise RuntimeError, /protected_attributes/ do
- Project.attr_accessible :username
- end
- end
-
- def test_attr_protected_raise_error
- assert_raise RuntimeError, /protected_attributes/ do
- Project.attr_protected :username
- end
- end
-end
diff --git a/activemodel/test/cases/validations/inclusion_validation_test.rb b/activemodel/test/cases/validations/inclusion_validation_test.rb
index ceec9dc256..01a373d85d 100644
--- a/activemodel/test/cases/validations/inclusion_validation_test.rb
+++ b/activemodel/test/cases/validations/inclusion_validation_test.rb
@@ -14,6 +14,7 @@ class InclusionValidationTest < ActiveModel::TestCase
Topic.validates_inclusion_of(:title, in: 'aaa'..'bbb')
assert Topic.new("title" => "bbc", "content" => "abc").invalid?
assert Topic.new("title" => "aa", "content" => "abc").invalid?
+ assert Topic.new("title" => "aaab", "content" => "abc").invalid?
assert Topic.new("title" => "aaa", "content" => "abc").valid?
assert Topic.new("title" => "abc", "content" => "abc").valid?
assert Topic.new("title" => "bbb", "content" => "abc").valid?