Skip to content

Commit

Permalink
validates_inclusion_of and validates_exclusion_of allow formatted :me…
Browse files Browse the repository at this point in the history
…ssage strings. Closes #8132 [devrieda, Mike Naberezny]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8172 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Nov 20, 2007
1 parent 609a03f commit 8ed83b9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@


* Add 'foxy' support for fixtures of polymorphic associations. #10183 [jbarnette, David Lowenfels] * Add 'foxy' support for fixtures of polymorphic associations. #10183 [jbarnette, David Lowenfels]


* validates_inclusion_of and validates_exclusion_of allow formatted :message strings. #8132 [devrieda, Mike Naberezny]

* attr_readonly behaves well with optimistic locking. #10188 [Nick Bugajski] * attr_readonly behaves well with optimistic locking. #10188 [Nick Bugajski]


* Base#to_xml supports the nil="true" attribute like Hash#to_xml. #8268 [Catfish] * Base#to_xml supports the nil="true" attribute like Hash#to_xml. #8268 [Catfish]
Expand Down
10 changes: 6 additions & 4 deletions activerecord/lib/active_record/validations.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -689,8 +689,9 @@ def validates_format_of(*attr_names)
# Validates whether the value of the specified attribute is available in a particular enumerable object. # Validates whether the value of the specified attribute is available in a particular enumerable object.
# #
# class Person < ActiveRecord::Base # class Person < ActiveRecord::Base
# validates_inclusion_of :gender, :in=>%w( m f ), :message=>"woah! what are you then!??!!" # validates_inclusion_of :gender, :in => %w( m f ), :message => "woah! what are you then!??!!"
# validates_inclusion_of :age, :in=>0..99 # validates_inclusion_of :age, :in => 0..99
# validates_inclusion_of :format, :in => %w( jpg gif png ), :message => "extension %s is not included in the list"
# end # end
# #
# Configuration options: # Configuration options:
Expand All @@ -713,7 +714,7 @@ def validates_inclusion_of(*attr_names)
raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?") raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?")


validates_each(attr_names, configuration) do |record, attr_name, value| validates_each(attr_names, configuration) do |record, attr_name, value|
record.errors.add(attr_name, configuration[:message]) unless enum.include?(value) record.errors.add(attr_name, configuration[:message] % value) unless enum.include?(value)
end end
end end


Expand All @@ -722,6 +723,7 @@ def validates_inclusion_of(*attr_names)
# class Person < ActiveRecord::Base # class Person < ActiveRecord::Base
# validates_exclusion_of :username, :in => %w( admin superuser ), :message => "You don't belong here" # validates_exclusion_of :username, :in => %w( admin superuser ), :message => "You don't belong here"
# validates_exclusion_of :age, :in => 30..60, :message => "This site is only for under 30 and over 60" # validates_exclusion_of :age, :in => 30..60, :message => "This site is only for under 30 and over 60"
# validates_exclusion_of :format, :in => %w( mov avi ), :message => "extension %s is not allowed"
# end # end
# #
# Configuration options: # Configuration options:
Expand All @@ -744,7 +746,7 @@ def validates_exclusion_of(*attr_names)
raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?") raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?")


validates_each(attr_names, configuration) do |record, attr_name, value| validates_each(attr_names, configuration) do |record, attr_name, value|
record.errors.add(attr_name, configuration[:message]) if enum.include?(value) record.errors.add(attr_name, configuration[:message] % value) if enum.include?(value)
end end
end end


Expand Down
22 changes: 22 additions & 0 deletions activerecord/test/validations_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -547,6 +547,17 @@ def test_validates_length_of_with_allow_blank
assert Topic.create("title" => "abcde").valid? assert Topic.create("title" => "abcde").valid?
end end


def test_validates_inclusion_of_with_formatted_message
Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ), :message => "option %s is not in the list" )

assert Topic.create("title" => "a", "content" => "abc").valid?

t = Topic.create("title" => "uhoh", "content" => "abc")
assert !t.valid?
assert t.errors.on(:title)
assert_equal "option uhoh is not in the list", t.errors["title"]
end

def test_numericality_with_allow_nil_and_getter_method def test_numericality_with_allow_nil_and_getter_method
Developer.validates_numericality_of( :salary, :allow_nil => true) Developer.validates_numericality_of( :salary, :allow_nil => true)
developer = Developer.new("name" => "michael", "salary" => nil) developer = Developer.new("name" => "michael", "salary" => nil)
Expand All @@ -561,6 +572,17 @@ def test_validates_exclusion_of
assert !Topic.create("title" => "monkey", "content" => "abc").valid? assert !Topic.create("title" => "monkey", "content" => "abc").valid?
end end


def test_validates_exclusion_of_with_formatted_message
Topic.validates_exclusion_of( :title, :in => %w( abe monkey ), :message => "option %s is restricted" )

assert Topic.create("title" => "something", "content" => "abc")

t = Topic.create("title" => "monkey")
assert !t.valid?
assert t.errors.on(:title)
assert_equal "option monkey is restricted", t.errors["title"]
end

def test_validates_length_of_using_minimum def test_validates_length_of_using_minimum
Topic.validates_length_of :title, :minimum => 5 Topic.validates_length_of :title, :minimum => 5


Expand Down

0 comments on commit 8ed83b9

Please sign in to comment.