aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2017-10-26 11:27:21 -0400
committerGitHub <noreply@github.com>2017-10-26 11:27:21 -0400
commit12de5e202ddfca27cccc5965b3048e88718a9ace (patch)
tree2e7aae2d26e56036acced292301ff7fd8326b9b6 /activemodel/test
parent8cd143900978902ed9bbba10b34099a3140de5c6 (diff)
parentb8b089ef11300fab4625011b942ee4a1fbbdbbbd (diff)
downloadrails-12de5e202ddfca27cccc5965b3048e88718a9ace.tar.gz
rails-12de5e202ddfca27cccc5965b3048e88718a9ace.tar.bz2
rails-12de5e202ddfca27cccc5965b3048e88718a9ace.zip
Merge pull request #30674 from prognostikos/allow_procs_for_length_validator
Allow passing a Proc or Symbol as an argument to length validator values
Diffstat (limited to 'activemodel/test')
-rw-r--r--activemodel/test/cases/validations/length_validation_test.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/activemodel/test/cases/validations/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb
index a0d8e058f5..42f76f3e3c 100644
--- a/activemodel/test/cases/validations/length_validation_test.rb
+++ b/activemodel/test/cases/validations/length_validation_test.rb
@@ -410,4 +410,35 @@ class LengthValidationTest < ActiveModel::TestCase
assert Topic.new("title" => "david").valid?
assert Topic.new("title" => "david2").invalid?
end
+
+ def test_validates_length_of_using_proc_as_maximum
+ Topic.validates_length_of :title, maximum: ->(model) { 5 }
+
+ t = Topic.new("title" => "valid", "content" => "whatever")
+ assert t.valid?
+
+ t.title = "notvalid"
+ assert t.invalid?
+ assert t.errors[:title].any?
+ assert_equal ["is too long (maximum is 5 characters)"], t.errors[:title]
+
+ t.title = ""
+ assert t.valid?
+ end
+
+ def test_validates_length_of_using_proc_as_maximum_with_model_method
+ Topic.send(:define_method, :max_title_length, lambda { 5 })
+ Topic.validates_length_of :title, maximum: Proc.new(&:max_title_length)
+
+ t = Topic.new("title" => "valid", "content" => "whatever")
+ assert t.valid?
+
+ t.title = "notvalid"
+ assert t.invalid?
+ assert t.errors[:title].any?
+ assert_equal ["is too long (maximum is 5 characters)"], t.errors[:title]
+
+ t.title = ""
+ assert t.valid?
+ end
end