Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

LengthValidator can use general messages

closes #225
  • Loading branch information...
commit 9c58c89d2b27fefaa365054e1368b53643a3820d 1 parent 34e65eb
Brian Cardarella bcardarella authored
4 lib/client_side_validations/active_model/length.rb
... ... @@ -1,3 +1,5 @@
  1 +require 'ruby-debug'
  2 +
1 3 module ClientSideValidations::ActiveModel
2 4 module Length
3 5
@@ -9,7 +11,7 @@ def client_side_hash(model, attribute)
9 11
10 12 self.class::MESSAGES.each do |option, message_type|
11 13 if count = options[option]
12   - options[:message] = options[message_type]
  14 + options[:message] = options[message_type] if options[message_type].present?
13 15 options.delete(:message) if options[:message].nil?
14 16 hash[:messages][option] = model.errors.generate_message(attribute, message_type, options.merge(:count => count))
15 17 hash[option] = count
21 test/active_model/cases/test_length_validator.rb
@@ -22,6 +22,18 @@ def test_length_client_side_hash_with_custom_message
22 22 assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :is => 10, :wrong_length => "is the wrong length (should be %{count} words)").client_side_hash(@person, :first_name)
23 23 end
24 24
  25 + def test_length_client_side_hash_with_custom_general_message
  26 + expected_hash = {
  27 + :messages => {
  28 + :minimum => "is not the correct length",
  29 + :maximum => "is way too long"
  30 + },
  31 + :minimum => 4,
  32 + :maximum => 10
  33 + }
  34 + assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :minimum => 4, :maximum => 10, :message => "is not the correct length", :too_long => "is way too long").client_side_hash(@person, :first_name)
  35 + end
  36 +
25 37 def test_length_client_side_hash_with_js_tokenizer
26 38 expected_hash = {
27 39 :messages => {
@@ -57,5 +69,14 @@ def test_length_client_side_hash_with_range
57 69 assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :within => 5..10).client_side_hash(@person, :first_name)
58 70 end
59 71
  72 + def test_length_client_side_hash
  73 + expected_hash = {
  74 + :messages => {
  75 + :is => "is the wrong length (should be 10 characters)"
  76 + },
  77 + :is => 10
  78 + }
  79 + assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :is => 10).client_side_hash(@person, :first_name)
  80 + end
60 81 end
61 82

0 comments on commit 9c58c89

Please sign in to comment.
Something went wrong with that request. Please try again.