diff options
author | Matt Rohrer <matt@prognostikos.com> | 2017-09-21 15:39:49 +0200 |
---|---|---|
committer | Matt Rohrer <matt@prognostikos.com> | 2017-10-26 10:01:06 +0200 |
commit | b8b089ef11300fab4625011b942ee4a1fbbdbbbd (patch) | |
tree | 2e7aae2d26e56036acced292301ff7fd8326b9b6 /activemodel/test/cases/validations | |
parent | 8cd143900978902ed9bbba10b34099a3140de5c6 (diff) | |
download | rails-b8b089ef11300fab4625011b942ee4a1fbbdbbbd.tar.gz rails-b8b089ef11300fab4625011b942ee4a1fbbdbbbd.tar.bz2 rails-b8b089ef11300fab4625011b942ee4a1fbbdbbbd.zip |
Allow passing a Proc or Symbol as an argument to length validator values
This brings the Length validator in line with the Numericality
validator, which currently supports Proc & Symbol arguments
Diffstat (limited to 'activemodel/test/cases/validations')
-rw-r--r-- | activemodel/test/cases/validations/length_validation_test.rb | 31 |
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 |