Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed the low_message and high_message options for should_ensure_valu…

…e_in_range [#160 state:committed]
  • Loading branch information...
commit 42beb3beb7279e72d71465c5e42de64644a84d6a 1 parent 1ce9339
Joe Ferris jferris authored
11 lib/shoulda/active_record/macros.rb
View
@@ -296,13 +296,16 @@ def should_ensure_length_is(attribute, length, opts = {})
# should_ensure_value_in_range :age, (0..100)
#
def should_ensure_value_in_range(attribute, range, opts = {})
- message = get_options!([opts], :message)
- message ||= default_error_message(:inclusion)
-
+ message, low_message, high_message = get_options!([opts],
+ :message,
+ :low_message,
+ :high_message)
klass = model_class
matcher = ensure_inclusion_of(attribute).
in_range(range).
- with_message(message)
+ with_message(message).
+ with_low_message(low_message).
+ with_high_message(high_message)
should matcher.description do
assert_accepts matcher, get_instance_of(klass)
end
3  test/rails_root/app/models/user.rb
View
@@ -20,7 +20,8 @@ def self.recent_via_method(count)
validates_format_of :email, :with => /\w*@\w*.com/
validates_length_of :email, :in => 1..100
- validates_inclusion_of :age, :in => 1..100
+ validates_numericality_of :age, :greater_than_or_equal_to => 1,
+ :less_than_or_equal_to => 100
validates_acceptance_of :eula
validates_uniqueness_of :email, :scope => :name, :case_sensitive => false
validates_length_of :ssn, :is => 9, :message => "Social Security Number is not the right length"
11 test/unit/user_test.rb
View
@@ -38,7 +38,16 @@ class UserTest < Test::Unit::TestCase
should_not_allow_values_for :email, "blah", "b lah"
should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
should_ensure_length_in_range :email, 1..100
- should_ensure_value_in_range :age, 1..100
+ should_ensure_value_in_range :age, 1..100, :low_message => /greater/,
+ :high_message => /less/
+ should_fail do
+ should_ensure_value_in_range :age, 1..100, :low_message => /more/,
+ :high_message => /less/
+ end
+ should_fail do
+ should_ensure_value_in_range :age, 1..100, :low_message => /greater/,
+ :high_message => /fewer/
+ end
should_not_allow_mass_assignment_of :password
should_have_class_methods :find, :destroy
should_have_instance_methods :email, :age, :email=, :valid?
Please sign in to comment.
Something went wrong with that request. Please try again.