Skip to content
Browse files

LengthValidator can use general messages

closes #225
  • Loading branch information...
1 parent 34e65eb commit 9c58c89d2b27fefaa365054e1368b53643a3820d @bcardarella bcardarella committed Jan 24, 2012
View
4 lib/client_side_validations/active_model/length.rb
@@ -1,3 +1,5 @@
+require 'ruby-debug'
+
module ClientSideValidations::ActiveModel
module Length
@@ -9,7 +11,7 @@ def client_side_hash(model, attribute)
self.class::MESSAGES.each do |option, message_type|
if count = options[option]
- options[:message] = options[message_type]
+ options[:message] = options[message_type] if options[message_type].present?
options.delete(:message) if options[:message].nil?
hash[:messages][option] = model.errors.generate_message(attribute, message_type, options.merge(:count => count))
hash[option] = count
View
21 test/active_model/cases/test_length_validator.rb
@@ -22,6 +22,18 @@ def test_length_client_side_hash_with_custom_message
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)
end
+ def test_length_client_side_hash_with_custom_general_message
+ expected_hash = {
+ :messages => {
+ :minimum => "is not the correct length",
+ :maximum => "is way too long"
+ },
+ :minimum => 4,
+ :maximum => 10
+ }
+ 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)
+ end
+
def test_length_client_side_hash_with_js_tokenizer
expected_hash = {
:messages => {
@@ -57,5 +69,14 @@ def test_length_client_side_hash_with_range
assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :within => 5..10).client_side_hash(@person, :first_name)
end
+ def test_length_client_side_hash
+ expected_hash = {
+ :messages => {
+ :is => "is the wrong length (should be 10 characters)"
+ },
+ :is => 10
+ }
+ assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :is => 10).client_side_hash(@person, :first_name)
+ end
end

0 comments on commit 9c58c89

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