Changing some validations to be compatible with activemodel.

This commit is contained in:
Tapajós 2009-10-30 16:07:59 -02:00
parent d4010ad76e
commit 58d621d399
11 changed files with 54 additions and 30 deletions

View file

@ -65,7 +65,7 @@ module CouchRest
# validator to be automatically created on the property # validator to be automatically created on the property
# #
# Integer type # Integer type
# Using a Integer type causes a validates_is_number # Using a Integer type causes a validates_numericality_of
# validator to be created for the property. integer_only # validator to be created for the property. integer_only
# is set to true # is set to true
# #
@ -97,8 +97,7 @@ module CouchRest
# presence # presence
if opts[:allow_nil] == false if opts[:allow_nil] == false
# validates_present property.name, opts validates_presence_of property.name, options_with_message(opts, property, :presence)
validates_present property.name, options_with_message(opts, property, :presence)
end end
# length # length
@ -111,8 +110,7 @@ module CouchRest
else else
opts[:maximum] = len opts[:maximum] = len
end end
# validates_length property.name, opts validates_length_of property.name, options_with_message(opts, property, :length)
validates_length property.name, options_with_message(opts, property, :length)
end end
# format # format
@ -142,13 +140,11 @@ module CouchRest
# numeric validator # numeric validator
if "Integer" == property.type if "Integer" == property.type
opts[:integer_only] = true opts[:integer_only] = true
# validates_is_number property.name, opts validates_numericality_of property.name, options_with_message(opts, property, :is_number)
validates_is_number property.name, options_with_message(opts, property, :is_number)
elsif Float == property.type elsif Float == property.type
opts[:precision] = property.precision opts[:precision] = property.precision
opts[:scale] = property.scale opts[:scale] = property.scale
# validates_is_number property.name, opts validates_numericality_of property.name, options_with_message(opts, property, :is_number)
validates_is_number property.name, options_with_message(opts, property, :is_number)
end end
# marked the property has checked # marked the property has checked

View file

@ -81,19 +81,27 @@ module CouchRest
# attr_accessor :password_confirmation # attr_accessor :password_confirmation
# attr_accessor :email_repeated # attr_accessor :email_repeated
# #
# validates_is_confirmed :password # validates_confirmation_of :password
# validates_is_confirmed :email, :confirm => :email_repeated # validates_confirmation_of :email, :confirm => :email_repeated
# #
# # a call to valid? will return false unless: # # a call to valid? will return false unless:
# # password == password_confirmation # # password == password_confirmation
# # and # # and
# # email == email_repeated # # email == email_repeated
# #
def validates_is_confirmed(*fields) def validates_confirmation_of(*fields)
opts = opts_from_validator_args(fields) opts = opts_from_validator_args(fields)
add_validator_to_context(opts, fields, CouchRest::Validation::ConfirmationValidator) add_validator_to_context(opts, fields, CouchRest::Validation::ConfirmationValidator)
end end
def validates_is_confirmed(*fields)
warn "[DEPRECATION] `validates_is_confirmed` is deprecated. Please use `validates_confirmation_of` instead."
validates_confirmation_of(*fields)
end
end # module ValidatesIsConfirmed end # module ValidatesIsConfirmed
end # module Validation end # module Validation
end # module CouchRest end # module CouchRest

View file

@ -99,19 +99,24 @@ module CouchRest
# property :email, String # property :email, String
# property :zip_code, String # property :zip_code, String
# #
# validates_format :email, :as => :email_address # validates_format_of :email, :as => :email_address
# validates_format :zip_code, :with => /^\d{5}$/ # validates_format_of :zip_code, :with => /^\d{5}$/
# #
# # a call to valid? will return false unless: # # a call to valid? will return false unless:
# # email is formatted like an email address # # email is formatted like an email address
# # and # # and
# # zip_code is a string of 5 digits # # zip_code is a string of 5 digits
# #
def validates_format(*fields) def validates_format_of(*fields)
opts = opts_from_validator_args(fields) opts = opts_from_validator_args(fields)
add_validator_to_context(opts, fields, CouchRest::Validation::FormatValidator) add_validator_to_context(opts, fields, CouchRest::Validation::FormatValidator)
end end
def validates_format(*fields)
warn "[DEPRECATION] `validates_format` is deprecated. Please use `validates_format_of` instead."
validates_format_of(*fields)
end
end # module ValidatesFormat end # module ValidatesFormat
end # module Validation end # module Validation
end # module CouchRest end # module CouchRest

View file

@ -115,20 +115,25 @@ module CouchRest
# property low, Integer # property low, Integer
# property just_right, Integer # property just_right, Integer
# #
# validates_length :high, :min => 100000000000 # validates_length_of :high, :min => 100000000000
# validates_length :low, :equals => 0 # validates_length_of :low, :equals => 0
# validates_length :just_right, :within => 1..10 # validates_length_of :just_right, :within => 1..10
# #
# # a call to valid? will return false unless: # # a call to valid? will return false unless:
# # high is greater than or equal to 100000000000 # # high is greater than or equal to 100000000000
# # low is equal to 0 # # low is equal to 0
# # just_right is between 1 and 10 (inclusive of both 1 and 10) # # just_right is between 1 and 10 (inclusive of both 1 and 10)
# #
def validates_length(*fields) def validates_length_of(*fields)
opts = opts_from_validator_args(fields) opts = opts_from_validator_args(fields)
add_validator_to_context(opts, fields, CouchRest::Validation::LengthValidator) add_validator_to_context(opts, fields, CouchRest::Validation::LengthValidator)
end end
def validates_length(*fields)
warn "[DEPRECATION] `validates_length` is deprecated. Please use `validates_length_of` instead."
validates_length_of(*fields)
end
end # module ValidatesLength end # module ValidatesLength
end # module Validation end # module Validation
end # module CouchRest end # module CouchRest

View file

@ -94,11 +94,16 @@ module CouchRest
# Validate whether a field is numeric # Validate whether a field is numeric
# #
def validates_is_number(*fields) def validates_numericality_of(*fields)
opts = opts_from_validator_args(fields) opts = opts_from_validator_args(fields)
add_validator_to_context(opts, fields, CouchRest::Validation::NumericValidator) add_validator_to_context(opts, fields, CouchRest::Validation::NumericValidator)
end end
def validates_is_number(*fields)
warn "[DEPRECATION] `validates_is_number` is deprecated. Please use `validates_numericality_of` instead."
validates_numericality_of(*fields)
end
end # module ValidatesIsNumber end # module ValidatesIsNumber
end # module Validation end # module Validation
end # module CouchRest end # module CouchRest

View file

@ -93,17 +93,22 @@ module CouchRest
# property :another_required, String # property :another_required, String
# property :yet_again, String # property :yet_again, String
# #
# validates_present :required_attribute # validates_presence_of :required_attribute
# validates_present :another_required, :yet_again # validates_presence_of :another_required, :yet_again
# #
# # a call to valid? will return false unless # # a call to valid? will return false unless
# # all three attributes are !blank? # # all three attributes are !blank?
# end # end
def validates_present(*fields) def validates_presence_of(*fields)
opts = opts_from_validator_args(fields) opts = opts_from_validator_args(fields)
add_validator_to_context(opts, fields, CouchRest::Validation::RequiredFieldValidator) add_validator_to_context(opts, fields, CouchRest::Validation::RequiredFieldValidator)
end end
def validates_present(*fields)
warn "[DEPRECATION] `validates_present` is deprecated. Please use `validates_presence_of` instead."
validates_presence_of(*fields)
end
end # module ValidatesPresent end # module ValidatesPresent
end # module Validation end # module Validation
end # module CouchRest end # module CouchRest

View file

@ -224,7 +224,7 @@ describe CouchRest::CastedModel do
it "should not fail if the casted model doesn't have validation" do it "should not fail if the casted model doesn't have validation" do
Cat.property :masters, :cast_as => ['Person'], :default => [] Cat.property :masters, :cast_as => ['Person'], :default => []
Cat.validates_present :name Cat.validates_presence_of :name
cat = Cat.new(:name => 'kitty') cat = Cat.new(:name => 'kitty')
cat.should be_valid cat.should be_valid
cat.masters.push Person.new cat.masters.push Person.new

View file

@ -730,7 +730,7 @@ describe "ExtendedDocument" do
it "should not fail if the nested casted model doesn't have validation" do it "should not fail if the nested casted model doesn't have validation" do
Cat.property :trainer, :cast_as => 'Person' Cat.property :trainer, :cast_as => 'Person'
Cat.validates_present :name Cat.validates_presence_of :name
cat = Cat.new(:name => 'Mr Bigglesworth') cat = Cat.new(:name => 'Mr Bigglesworth')
cat.trainer = Person.new cat.trainer = Person.new
cat.trainer.validatable?.should be_false cat.trainer.validatable?.should be_false

View file

@ -17,6 +17,6 @@ class Card < CouchRest::ExtendedDocument
timestamps! timestamps!
# Validation # Validation
validates_present :first_name validates_presence_of :first_name
end end

View file

@ -15,5 +15,5 @@ class CatToy < Hash
property :name property :name
validates_present :name validates_presence_of :name
end end

View file

@ -11,7 +11,7 @@ class Invoice < CouchRest::ExtendedDocument
property :location property :location
# Validation # Validation
validates_present :client_name, :employee_name validates_presence_of :client_name, :employee_name
validates_present :location, :message => "Hey stupid!, you forgot the location" validates_presence_of :location, :message => "Hey stupid!, you forgot the location"
end end