Skip to content

Commit

Permalink
Added validates_login_field_options, validates_email_field_options, a…
Browse files Browse the repository at this point in the history
…nd validates_password_field_options to apply universal options for each fields validation calls.
  • Loading branch information
binarylogic committed Mar 29, 2009
1 parent 314af4d commit 1ff5fbf
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
== 2.0.5

* Stub out authenticate_with_http_basic for TestCase::Controller adapter.
* Added validates_login_field_options, validates_email_field_options, and validates_password_field_options to apply universal options for each fields validation calls.

== 2.0.4 released 2009-3-28

Expand Down
16 changes: 13 additions & 3 deletions lib/authlogic/acts_as_authentic/email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ def validate_email_field(value = nil)
end
alias_method :validate_email_field=, :validate_email_field

# This allows you to set options to be applied to ANY of the validations called on the email field. So only use options
# that are universally supported by all of the validation functions. Such as :if or :unless, etc.
#
# * <tt>Default:</tt> {}
# * <tt>Accepts:</tt> A hash of options universally supported by the validation func
def validates_email_field_options(value = nil)
config(:validates_email_field_options, value, {})
end
alias_method :validates_email_field_options=, :validates_email_field_options

# A hash of options for the validates_length_of call for the email field. Allows you to change this however you want.
#
# * <tt>Default:</tt> {:within => 6..100}
Expand Down Expand Up @@ -74,9 +84,9 @@ module Methods
def self.included(klass)
klass.class_eval do
if validate_email_field && email_field
validates_length_of email_field, validates_length_of_email_field_options
validates_format_of email_field, validates_format_of_email_field_options
validates_uniqueness_of email_field, validates_uniqueness_of_email_field_options
validates_length_of email_field, validates_email_field_options.merge(validates_length_of_email_field_options)
validates_format_of email_field, validates_email_field_options.merge(validates_format_of_email_field_options)
validates_uniqueness_of email_field, validates_email_field_options.merge(validates_uniqueness_of_email_field_options)
end
end
end
Expand Down
16 changes: 13 additions & 3 deletions lib/authlogic/acts_as_authentic/login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ def validate_login_field(value = nil)
end
alias_method :validate_login_field=, :validate_login_field

# This allows you to set options to be applied to ANY of the validations called on the login field. So only use options
# that are universally supported by all of the validation functions. Such as :if or :unless, etc.
#
# * <tt>Default:</tt> {}
# * <tt>Accepts:</tt> A hash of options universally supported by the validation func
def validates_login_field_options(value = nil)
config(:validates_login_field_options, value, {})
end
alias_method :validates_login_field_options=, :validates_login_field_options

# A hash of options for the validates_length_of call for the login field. Allows you to change this however you want.
#
# * <tt>Default:</tt> {:within => 6..100}
Expand Down Expand Up @@ -62,9 +72,9 @@ module Methods
def self.included(klass)
klass.class_eval do
if validate_login_field && login_field
validates_length_of login_field, validates_length_of_login_field_options
validates_format_of login_field, validates_format_of_login_field_options
validates_uniqueness_of login_field, validates_uniqueness_of_login_field_options
validates_length_of login_field, validates_login_field_options.merge(validates_length_of_login_field_options)
validates_format_of login_field, validates_login_field_options.merge(validates_format_of_login_field_options)
validates_uniqueness_of login_field, validates_login_field_options.merge(validates_uniqueness_of_login_field_options)
end
end
end
Expand Down
16 changes: 13 additions & 3 deletions lib/authlogic/acts_as_authentic/password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ def validate_password_field(value = nil)
end
alias_method :validate_password_field=, :validate_password_field

# This allows you to set options to be applied to ANY of the validations called on the password field. So only use options
# that are universally supported by all of the validation functions. Such as :if or :unless, etc.
#
# * <tt>Default:</tt> {}
# * <tt>Accepts:</tt> A hash of options universally supported by the validation func
def validates_password_field_options(value = nil)
config(:validates_password_field_options, value, {})
end
alias_method :validates_password_field_options=, :validates_password_field_options

# A hash of options for the validates_length_of call for the password field. Allows you to change this however you want.
#
# * <tt>Default:</tt> {:minimum => 4, :if => :require_password?}
Expand Down Expand Up @@ -120,9 +130,9 @@ module Methods
def self.included(klass)
klass.class_eval do
if validate_password_field
validates_length_of :password, validates_length_of_password_field_options
validates_confirmation_of :password, validates_confirmation_of_password_field_options
validates_length_of :password_confirmation, validates_length_of_password_confirmation_field_options
validates_length_of :password, validates_password_field_options.merge(validates_length_of_password_field_options)
validates_confirmation_of :password, validates_password_field_options.merge(validates_confirmation_of_password_field_options)
validates_length_of :password_confirmation, validates_password_field_options.merge(validates_length_of_password_confirmation_field_options)
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions test/acts_as_authentic_test/email_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ def test_validate_email_field_config
assert User.validate_email_field
end

def test_validates_email_field_options_config
default = {}
assert_equal default, User.validates_email_field_options
assert_equal default, Employee.validates_email_field_options

User.validates_email_field_options = {:yes => "no"}
assert_equal({:yes => "no"}, User.validates_email_field_options)
User.validates_email_field_options default
assert_equal default, User.validates_email_field_options
end

def test_validates_length_of_email_field_options_config
assert_equal({:within => 6..100}, User.validates_length_of_email_field_options)
assert_equal({:within => 6..100}, Employee.validates_length_of_email_field_options)
Expand Down
11 changes: 11 additions & 0 deletions test/acts_as_authentic_test/login_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ def test_validate_login_field_config
assert User.validate_login_field
end

def test_validates_login_field_options_config
default = {}
assert_equal default, User.validates_login_field_options
assert_equal default, Employee.validates_login_field_options

User.validates_login_field_options = {:yes => "no"}
assert_equal({:yes => "no"}, User.validates_login_field_options)
User.validates_login_field_options default
assert_equal default, User.validates_login_field_options
end

def test_validates_length_of_login_field_options_config
assert_equal({:within => 3..100}, User.validates_length_of_login_field_options)
assert_equal({:within => 3..100}, Employee.validates_length_of_login_field_options)
Expand Down
11 changes: 11 additions & 0 deletions test/acts_as_authentic_test/password_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ def test_validate_password_field_config
assert User.validate_password_field
end

def test_validates_password_field_options_config
default = {}
assert_equal default, User.validates_password_field_options
assert_equal default, Employee.validates_password_field_options

User.validates_password_field_options = {:yes => "no"}
assert_equal({:yes => "no"}, User.validates_password_field_options)
User.validates_password_field_options default
assert_equal default, User.validates_password_field_options
end

def test_validates_length_of_password_field_options_config
default = {:minimum => 4, :if => :require_password?}
assert_equal default, User.validates_length_of_password_field_options
Expand Down

0 comments on commit 1ff5fbf

Please sign in to comment.