Changing some validations to be compatible with activemodel.
This commit is contained in:
parent
d4010ad76e
commit
58d621d399
|
@ -65,7 +65,7 @@ module CouchRest
|
|||
# validator to be automatically created on the property
|
||||
#
|
||||
# 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
|
||||
# is set to true
|
||||
#
|
||||
|
@ -97,8 +97,7 @@ module CouchRest
|
|||
|
||||
# presence
|
||||
if opts[:allow_nil] == false
|
||||
# validates_present property.name, opts
|
||||
validates_present property.name, options_with_message(opts, property, :presence)
|
||||
validates_presence_of property.name, options_with_message(opts, property, :presence)
|
||||
end
|
||||
|
||||
# length
|
||||
|
@ -111,8 +110,7 @@ module CouchRest
|
|||
else
|
||||
opts[:maximum] = len
|
||||
end
|
||||
# validates_length property.name, opts
|
||||
validates_length property.name, options_with_message(opts, property, :length)
|
||||
validates_length_of property.name, options_with_message(opts, property, :length)
|
||||
end
|
||||
|
||||
# format
|
||||
|
@ -142,13 +140,11 @@ module CouchRest
|
|||
# numeric validator
|
||||
if "Integer" == property.type
|
||||
opts[:integer_only] = true
|
||||
# validates_is_number property.name, opts
|
||||
validates_is_number property.name, options_with_message(opts, property, :is_number)
|
||||
validates_numericality_of property.name, options_with_message(opts, property, :is_number)
|
||||
elsif Float == property.type
|
||||
opts[:precision] = property.precision
|
||||
opts[:scale] = property.scale
|
||||
# validates_is_number property.name, opts
|
||||
validates_is_number property.name, options_with_message(opts, property, :is_number)
|
||||
validates_numericality_of property.name, options_with_message(opts, property, :is_number)
|
||||
end
|
||||
|
||||
# marked the property has checked
|
||||
|
|
|
@ -81,19 +81,27 @@ module CouchRest
|
|||
# attr_accessor :password_confirmation
|
||||
# attr_accessor :email_repeated
|
||||
#
|
||||
# validates_is_confirmed :password
|
||||
# validates_is_confirmed :email, :confirm => :email_repeated
|
||||
# validates_confirmation_of :password
|
||||
# validates_confirmation_of :email, :confirm => :email_repeated
|
||||
#
|
||||
# # a call to valid? will return false unless:
|
||||
# # password == password_confirmation
|
||||
# # and
|
||||
# # email == email_repeated
|
||||
#
|
||||
def validates_is_confirmed(*fields)
|
||||
def validates_confirmation_of(*fields)
|
||||
opts = opts_from_validator_args(fields)
|
||||
add_validator_to_context(opts, fields, CouchRest::Validation::ConfirmationValidator)
|
||||
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 Validation
|
||||
end # module CouchRest
|
||||
|
|
|
@ -99,19 +99,24 @@ module CouchRest
|
|||
# property :email, String
|
||||
# property :zip_code, String
|
||||
#
|
||||
# validates_format :email, :as => :email_address
|
||||
# validates_format :zip_code, :with => /^\d{5}$/
|
||||
# validates_format_of :email, :as => :email_address
|
||||
# validates_format_of :zip_code, :with => /^\d{5}$/
|
||||
#
|
||||
# # a call to valid? will return false unless:
|
||||
# # email is formatted like an email address
|
||||
# # and
|
||||
# # zip_code is a string of 5 digits
|
||||
#
|
||||
def validates_format(*fields)
|
||||
def validates_format_of(*fields)
|
||||
opts = opts_from_validator_args(fields)
|
||||
add_validator_to_context(opts, fields, CouchRest::Validation::FormatValidator)
|
||||
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 Validation
|
||||
end # module CouchRest
|
||||
|
|
|
@ -115,20 +115,25 @@ module CouchRest
|
|||
# property low, Integer
|
||||
# property just_right, Integer
|
||||
#
|
||||
# validates_length :high, :min => 100000000000
|
||||
# validates_length :low, :equals => 0
|
||||
# validates_length :just_right, :within => 1..10
|
||||
# validates_length_of :high, :min => 100000000000
|
||||
# validates_length_of :low, :equals => 0
|
||||
# validates_length_of :just_right, :within => 1..10
|
||||
#
|
||||
# # a call to valid? will return false unless:
|
||||
# # high is greater than or equal to 100000000000
|
||||
# # low is equal to 0
|
||||
# # 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)
|
||||
add_validator_to_context(opts, fields, CouchRest::Validation::LengthValidator)
|
||||
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 Validation
|
||||
end # module CouchRest
|
||||
|
|
|
@ -94,11 +94,16 @@ module CouchRest
|
|||
|
||||
# Validate whether a field is numeric
|
||||
#
|
||||
def validates_is_number(*fields)
|
||||
def validates_numericality_of(*fields)
|
||||
opts = opts_from_validator_args(fields)
|
||||
add_validator_to_context(opts, fields, CouchRest::Validation::NumericValidator)
|
||||
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 Validation
|
||||
end # module CouchRest
|
||||
|
|
|
@ -93,17 +93,22 @@ module CouchRest
|
|||
# property :another_required, String
|
||||
# property :yet_again, String
|
||||
#
|
||||
# validates_present :required_attribute
|
||||
# validates_present :another_required, :yet_again
|
||||
# validates_presence_of :required_attribute
|
||||
# validates_presence_of :another_required, :yet_again
|
||||
#
|
||||
# # a call to valid? will return false unless
|
||||
# # all three attributes are !blank?
|
||||
# end
|
||||
def validates_present(*fields)
|
||||
def validates_presence_of(*fields)
|
||||
opts = opts_from_validator_args(fields)
|
||||
add_validator_to_context(opts, fields, CouchRest::Validation::RequiredFieldValidator)
|
||||
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 Validation
|
||||
end # module CouchRest
|
||||
|
|
|
@ -224,7 +224,7 @@ describe CouchRest::CastedModel do
|
|||
|
||||
it "should not fail if the casted model doesn't have validation" do
|
||||
Cat.property :masters, :cast_as => ['Person'], :default => []
|
||||
Cat.validates_present :name
|
||||
Cat.validates_presence_of :name
|
||||
cat = Cat.new(:name => 'kitty')
|
||||
cat.should be_valid
|
||||
cat.masters.push Person.new
|
||||
|
|
|
@ -730,7 +730,7 @@ describe "ExtendedDocument" do
|
|||
|
||||
it "should not fail if the nested casted model doesn't have validation" do
|
||||
Cat.property :trainer, :cast_as => 'Person'
|
||||
Cat.validates_present :name
|
||||
Cat.validates_presence_of :name
|
||||
cat = Cat.new(:name => 'Mr Bigglesworth')
|
||||
cat.trainer = Person.new
|
||||
cat.trainer.validatable?.should be_false
|
||||
|
|
2
spec/fixtures/more/card.rb
vendored
2
spec/fixtures/more/card.rb
vendored
|
@ -17,6 +17,6 @@ class Card < CouchRest::ExtendedDocument
|
|||
timestamps!
|
||||
|
||||
# Validation
|
||||
validates_present :first_name
|
||||
validates_presence_of :first_name
|
||||
|
||||
end
|
2
spec/fixtures/more/cat.rb
vendored
2
spec/fixtures/more/cat.rb
vendored
|
@ -15,5 +15,5 @@ class CatToy < Hash
|
|||
|
||||
property :name
|
||||
|
||||
validates_present :name
|
||||
validates_presence_of :name
|
||||
end
|
4
spec/fixtures/more/invoice.rb
vendored
4
spec/fixtures/more/invoice.rb
vendored
|
@ -11,7 +11,7 @@ class Invoice < CouchRest::ExtendedDocument
|
|||
property :location
|
||||
|
||||
# Validation
|
||||
validates_present :client_name, :employee_name
|
||||
validates_present :location, :message => "Hey stupid!, you forgot the location"
|
||||
validates_presence_of :client_name, :employee_name
|
||||
validates_presence_of :location, :message => "Hey stupid!, you forgot the location"
|
||||
|
||||
end
|
Loading…
Reference in a new issue