Skip to content

Commit

Permalink
* Extracted Authlogics regular expressions into its own module to all…
Browse files Browse the repository at this point in the history
…ow easy use of them outside of Authlogic. See Authlogic::Regex for more info.
  • Loading branch information
binarylogic committed Apr 29, 2009
1 parent f638b12 commit c5e610a
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
@@ -1,6 +1,7 @@
== 2.0.12

* Added the ability to add a last_request_update_allowed? method in your controller to pragmatically tell Authlogic when and when not to update the last_request_at field in your database. This only takes effect if the method if present.
* Extracted Authlogic's regular expressions into it's own module to allow easy use of them outside of Authlogic. See Authlogic::Regex for more info.

== 2.0.11 release 2009-4-25

Expand Down
6 changes: 6 additions & 0 deletions README.rdoc
Expand Up @@ -24,6 +24,12 @@ You can also log out / destroy the session:

session.destroy

Sessions are automatically maintained. You can switch this on and off with configuration, but the following will automatically log a user in after a successful registration:

User.create(params[:user])

This also updates the session when the user changes his/her password.

== Helpful links

* <b>Documentation:</b> http://authlogic.rubyforge.org
Expand Down
1 change: 1 addition & 0 deletions lib/authlogic.rb
Expand Up @@ -3,6 +3,7 @@
require File.dirname(__FILE__) + "/authlogic/version"
require File.dirname(__FILE__) + "/authlogic/i18n"
require File.dirname(__FILE__) + "/authlogic/random"
require File.dirname(__FILE__) + "/authlogic/regex"

require File.dirname(__FILE__) + "/authlogic/controller_adapters/abstract_adapter"
require File.dirname(__FILE__) + "/authlogic/controller_adapters/rails_adapter" if defined?(Rails)
Expand Down
13 changes: 2 additions & 11 deletions lib/authlogic/acts_as_authentic/email.rb
Expand Up @@ -62,10 +62,10 @@ def merge_validates_length_of_email_field_options(options = {})
# merge options into it. Checkout the convenience function merge_validates_format_of_email_field_options to merge
# options.</b>
#
# * <tt>Default:</tt> {:with => email_regex, :message => I18n.t('error_messages.email_invalid', :default => "should look like an email address.")}
# * <tt>Default:</tt> {:with => Authlogic::Regex.email, :message => I18n.t('error_messages.email_invalid', :default => "should look like an email address.")}
# * <tt>Accepts:</tt> Hash of options accepted by validates_format_of
def validates_format_of_email_field_options(value = nil)
config(:validates_format_of_email_field_options, value, {:with => email_regex, :message => I18n.t('error_messages.email_invalid', :default => "should look like an email address.")})
config(:validates_format_of_email_field_options, value, {:with => Authlogic::Regex.email, :message => I18n.t('error_messages.email_invalid', :default => "should look like an email address.")})
end
alias_method :validates_format_of_email_field_options=, :validates_format_of_email_field_options

Expand All @@ -91,15 +91,6 @@ def validates_uniqueness_of_email_field_options(value = nil)
def merge_validates_uniqueness_of_email_field_options(options = {})
self.validates_uniqueness_of_email_field_options = validates_uniqueness_of_email_field_options.merge(options)
end

private
def email_regex
return @email_regex if @email_regex
email_name_regex = '[\w\.%\+\-]+'
domain_head_regex = '(?:[A-Z0-9\-]+\.)+'
domain_tld_regex = '(?:[A-Z]{2,4}|museum|travel)'
@email_regex = /\A#{email_name_regex}@#{domain_head_regex}#{domain_tld_regex}\z/i
end
end

# All methods relating to the email field
Expand Down
4 changes: 2 additions & 2 deletions lib/authlogic/acts_as_authentic/login.rb
Expand Up @@ -59,10 +59,10 @@ def merge_validates_length_of_login_field_options(options = {})
# merge options into it. Checkout the convenience function merge_validates_format_of_login_field_options to merge
# options.</b>
#
# * <tt>Default:</tt> {:with => /\A\w[\w\.\-_@ ]+\z/, :message => I18n.t('error_messages.login_invalid', :default => "should use only letters, numbers, spaces, and .-_@ please.")}
# * <tt>Default:</tt> {:with => Authlogic::Regex.login, :message => I18n.t('error_messages.login_invalid', :default => "should use only letters, numbers, spaces, and .-_@ please.")}
# * <tt>Accepts:</tt> Hash of options accepted by validates_format_of
def validates_format_of_login_field_options(value = nil)
config(:validates_format_of_login_field_options, value, {:with => /\A\w[\w\.+-_@ ]+\z/, :message => I18n.t('error_messages.login_invalid', :default => "should use only letters, numbers, spaces, and .-_@ please.")})
config(:validates_format_of_login_field_options, value, {:with => Authlogic::Regex.login, :message => I18n.t('error_messages.login_invalid', :default => "should use only letters, numbers, spaces, and .-_@ please.")})
end
alias_method :validates_format_of_login_field_options=, :validates_format_of_login_field_options

Expand Down
25 changes: 25 additions & 0 deletions lib/authlogic/regex.rb
@@ -0,0 +1,25 @@
module Authlogic
# This is a module the contains regular expressions used throughout Authlogic. The point of extracting
# them out into their own module is to make them easily available to you for other uses. Ex:
#
# validates_format_of :my_email_field, :with => Authlogic::Regex.email
module Regex
# A general email regular expression. It allows top level domains (TLD) to be from 2 - 4 in length, any
# TLD longer than that must be manually specified. The decisions behind this regular expression were made
# by reading this website: http://www.regular-expressions.info/email.html, which is an excellent resource
# for regular expressions.
def self.email
return @email_regex if @email_regex
email_name_regex = '[\w\.%\+\-]+'
domain_head_regex = '(?:[A-Z0-9\-]+\.)+'
domain_tld_regex = '(?:[A-Z]{2,4}|museum|travel)'
@email_regex = /\A#{email_name_regex}@#{domain_head_regex}#{domain_tld_regex}\z/i
end

# A simple regular expression that only allows for letters, numbers, spaces, and .-_@. Just a standard login / username
# regular expression.
def self.login
/\A\w[\w\.+-_@ ]+\z/
end
end
end
2 changes: 1 addition & 1 deletion test/acts_as_authentic_test/email_test.rb
Expand Up @@ -33,7 +33,7 @@ def test_validates_length_of_email_field_options_config
end

def test_validates_format_of_email_field_options_config
default = {:with => User.send(:email_regex), :message => I18n.t('error_messages.email_invalid', :default => "should look like an email address.")}
default = {:with => Authlogic::Regex.email, :message => I18n.t('error_messages.email_invalid', :default => "should look like an email address.")}
assert_equal default, User.validates_format_of_email_field_options
assert_equal default, Employee.validates_format_of_email_field_options

Expand Down

0 comments on commit c5e610a

Please sign in to comment.