Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add :within shortcut on key definition for validates_inclusion_of.

  • Loading branch information...
commit e67145234522c4eef5fd1bb7cb8c9fb6272dece8 1 parent e21a6c1
Chris Hanks chanks authored jnunemaker committed
6 lib/mongo_mapper/plugins/keys.rb
@@ -122,6 +122,10 @@ def create_validations_for(key)
122 122 validates_format_of(attribute, :with => key.options[:format])
123 123 end
124 124
  125 + if key.options[:within]
  126 + validates_inclusion_of(attribute, :within => key.options[:within])
  127 + end
  128 +
125 129 if key.options[:length]
126 130 length_options = case key.options[:length]
127 131 when Integer
@@ -289,4 +293,4 @@ def set(value)
289 293 end
290 294 end
291 295 end
292   -end
  296 +end
15 test/unit/test_validations.rb
@@ -214,6 +214,19 @@ class ValidationsTest < Test::Unit::TestCase
214 214 doc.action = 'kick'
215 215 doc.should_not have_error_on(:action)
216 216 end
  217 +
  218 + should "work with :within shortcut on key definition" do
  219 + @document.key :action, String, :within => %w(kick run)
  220 +
  221 + doc = @document.new
  222 + doc.should have_error_on(:action, 'is not in the list')
  223 +
  224 + doc.action = 'fart'
  225 + doc.should have_error_on(:action, 'is not in the list')
  226 +
  227 + doc.action = 'kick'
  228 + doc.should_not have_error_on(:action)
  229 + end
217 230
218 231 should "not have error if allow nil is true and value is nil" do
219 232 @document.key :action, String
@@ -489,4 +502,4 @@ def action_present
489 502 doc.should_not have_error_on(:action)
490 503 end
491 504 end
492   -end
  505 +end

0 comments on commit e671452

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