Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* Fixed bug where the find_with_login method was not being aliased if…

… you were using an alternate field besides login.
  • Loading branch information...
commit 1ffbc668f71d90a479c48365250a42e96d7cb06b 1 parent 92afd39
@binarylogic authored
View
7 lib/authlogic/acts_as_authentic/base.rb
@@ -61,13 +61,13 @@ def remove_acts_as_authentic_module(mod)
acts_as_authentic_modules.delete(mod)
acts_as_authentic_modules
end
-
+
private
def acts_as_authentic_modules
key = :acts_as_authentic_modules
inheritable_attributes.include?(key) ? read_inheritable_attribute(key) : []
end
-
+
def config(key, value, default_value = nil, read_value = nil)
if value == read_value
inheritable_attributes.include?(key) ? read_inheritable_attribute(key) : default_value
@@ -75,12 +75,11 @@ def config(key, value, default_value = nil, read_value = nil)
write_inheritable_attribute(key, value)
end
end
-
+
def first_column_to_exist(*columns_to_check)
columns_to_check.each { |column_name| return column_name.to_sym if column_names.include?(column_name.to_s) }
columns_to_check.first && columns_to_check.first.to_sym
end
-
end
end
end
View
20 lib/authlogic/acts_as_authentic/email.rb
@@ -73,8 +73,6 @@ def email_regex
module Methods
def self.included(klass)
klass.class_eval do
- extend ClassMethods
-
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
@@ -82,24 +80,6 @@ def self.included(klass)
end
end
end
-
- # Class methods relating to the email field
- module ClassMethods
- # Calls alias_method if your email_field name is "out of the norm".
- def self.included(klass)
- klass.send(:alias_method, "find_with_email", "find_with_#{email_field}") if klass.email_field != :email
- end
-
- # Please see the find_with_login method in Authlogic::ActsAsAuthentic::Login module. It's the same exact thing
- # but for the login field instead of the email field.
- def find_with_email(email)
- if validates_uniqueness_of_email_field_options[:case_sensitive] == false
- first(:conditions => ["LOWER(#{quoted_table_name}.#{email_field}) = ?", email.downcase])
- else
- send("find_by_#{email_field}", email)
- end
- end
- end
end
end
end
View
64 lib/authlogic/acts_as_authentic/login.rb
@@ -55,6 +55,39 @@ def validates_uniqueness_of_login_field_options(value = nil)
config(:validates_uniqueness_of_login_field_options, value, {:case_sensitive => false, :scope => validations_scope, :if => "#{login_field}_changed?".to_sym})
end
alias_method :validates_uniqueness_of_login_field_options=, :validates_uniqueness_of_login_field_options
+
+ # This method allows you to find a record with the given login. If you notice, with ActiveRecord you have the
+ # validates_uniqueness_of validation function. They give you a :case_sensitive option. I handle this in the same
+ # manner that they handle that. If you are using the login field and set false for the :case_sensitive option in
+ # validates_uniqueness_of_login_field_options this method will modify the query to look something like:
+ #
+ # first(:conditions => ["LOWER(#{quoted_table_name}.#{login_field}) = ?", login.downcase])
+ #
+ # If you don't specify this it calls the good old find_by_* method:
+ #
+ # find_by_login(login)
+ #
+ # The above also applies for using email as your login, except that you need to set the :case_sensitive in
+ # validates_uniqueness_of_email_field_options to false.
+ #
+ # The only reason I need to do the above is for Postgres and SQLite since they perform case sensitive searches with the
+ # find_by_* methods.
+ def find_by_smart_case_login_field(login)
+ if login_field
+ find_with_case(login_field, login, validates_uniqueness_of_login_field_options[:case_sensitive] != false)
+ else
+ find_with_case(email_field, login, validates_uniqueness_of_email_field_options[:case_sensitive] != false)
+ end
+ end
+
+ private
+ def find_with_case(field, value, sensitivity = true)
+ if sensitivity
+ send("find_by_#{field}", value)
+ else
+ first(:conditions => ["LOWER(#{quoted_table_name}.#{field}) = ?", value.downcase])
+ end
+ end
end
# All methods relating to the login field
@@ -62,8 +95,6 @@ module Methods
# Adds in various validations, modules, etc.
def self.included(klass)
klass.class_eval do
- extend ClassMethods
-
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
@@ -71,35 +102,6 @@ def self.included(klass)
end
end
end
-
- # Class methods relating to the login field.
- module ClassMethods
- # Calls alias_method if your login_field name is "out of the norm".
- def self.included(klass)
- klass.send(:alias_method, "find_with_login", "find_with_#{login_field}") if klass.login_field != :login
- end
-
- # This method allows you to find a record with the given login. If you notice, with ActiveRecord you have the
- # validates_uniqueness_of validation function. They give you a :case_sensitive option. I handle this in the same
- # manner that they handle that. If you set false for the :case_sensitive option in validates_uniqueness_of_login_field_options
- # this method will modify the query to look something like:
- #
- # first(:conditions => ["LOWER(#{quoted_table_name}.#{login_field}) = ?", login.downcase])
- #
- # If you don't specify this it calls the good old find_by_* method:
- #
- # find_by_login(login)
- #
- # The only reason I need to do the above is for Postgres and SQLite since they perform case sensitive searches with the
- # find_by_* methods.
- def find_with_login(login)
- if validates_uniqueness_of_login_field_options[:case_sensitive] == false
- first(:conditions => ["LOWER(#{quoted_table_name}.#{login_field}) = ?", login.downcase])
- else
- send("find_by_#{login_field}", login)
- end
- end
- end
end
end
end
View
4 lib/authlogic/session/password.rb
@@ -32,10 +32,10 @@ module Config
# Now just specifcy the name of this method for this configuration option and you are all set. You can do anything you want here. Maybe you allow users to have multiple logins
# and you want to search a has_many relationship, etc. The sky is the limit.
#
- # * <tt>Default:</tt> "find_by_case_insensitive_#{login_field}"
+ # * <tt>Default:</tt> "find_by_smart_case_login_field"
# * <tt>Accepts:</tt> Symbol or String
def find_by_login_method(value = nil)
- config(:find_by_login_method, value, "find_with_#{login_field}")
+ config(:find_by_login_method, value, "find_by_smart_case_login_field")
end
alias_method :find_by_login_method=, :find_by_login_method
View
7 test/acts_as_authentic_test/email_test.rb
@@ -89,12 +89,5 @@ def test_validates_uniqueness_of_email_field
assert !u.valid?
assert !u.errors.on(:email)
end
-
- def test_find_with_email
- ben = users(:ben)
- assert_equal ben, User.find_with_email("bjohnson@binarylogic.com")
- assert_equal ben, User.find_with_email("bJohnson@binarylogic.com")
- assert_equal ben, User.find_with_email("BJOHNSON@BINARYLOGIC.COM")
- end
end
end
View
13 test/acts_as_authentic_test/login_test.rb
@@ -90,11 +90,16 @@ def test_validates_uniqueness_of_login_field
assert !u.errors.on(:login)
end
- def test_find_with_login
+ def test_find_by_smart_case_login_field
ben = users(:ben)
- assert_equal ben, User.find_with_login("bjohnson")
- assert_equal ben, User.find_with_login("BJOHNSON")
- assert_equal ben, User.find_with_login("Bjohnson")
+ assert_equal ben, User.find_by_smart_case_login_field("bjohnson")
+ assert_equal ben, User.find_by_smart_case_login_field("BJOHNSON")
+ assert_equal ben, User.find_by_smart_case_login_field("Bjohnson")
+
+ drew = employees(:drew)
+ assert_equal drew, Employee.find_by_smart_case_login_field("dgainor@binarylogic.com")
+ assert_equal drew, Employee.find_by_smart_case_login_field("Dgainor@binarylogic.com")
+ assert_equal drew, Employee.find_by_smart_case_login_field("DGAINOR@BINARYLOGIC.COM")
end
end
end
View
7 test/libs/affiliate.rb
@@ -0,0 +1,7 @@
+class Affiliate < ActiveRecord::Base
+ acts_as_authentic do |c|
+ c.crypted_password_field = :pw_hash
+ end
+
+ belongs_to :company
+end
View
11 test/test_helper.rb
@@ -69,11 +69,22 @@
t.string :current_login_ip
t.string :last_login_ip
end
+
+ create_table :affiliates do |t|
+ t.datetime :created_at
+ t.datetime :updated_at
+ t.integer :company_id
+ t.string :username
+ t.string :pw_hash
+ t.string :pw_salt
+ t.string :persistence_token
+ end
end
require File.dirname(__FILE__) + '/../lib/authlogic' unless defined?(Authlogic)
require File.dirname(__FILE__) + '/../lib/authlogic/test_case'
require File.dirname(__FILE__) + '/libs/project'
+require File.dirname(__FILE__) + '/libs/affiliate'
require File.dirname(__FILE__) + '/libs/employee'
require File.dirname(__FILE__) + '/libs/employee_session'
require File.dirname(__FILE__) + '/libs/user'
Please sign in to comment.
Something went wrong with that request. Please try again.