More more logic to Authenticatable.

This commit is contained in:
José Valim 2010-04-16 22:00:06 +02:00
parent 9291ab55b8
commit 3135487931
3 changed files with 26 additions and 28 deletions

View File

@ -77,7 +77,7 @@ module Devise
# Return modules for the mapping.
def modules
@modules ||= to.devise_modules
@modules ||= to.respond_to?(:devise_modules) ? to.devise_modules : []
end
# Gives the class the mapping points to.

View File

@ -45,7 +45,6 @@ module Devise
# for a complete description on those values.
#
def devise(*modules)
define_devise_class_attributes!
include Devise::Models::Authenticatable
options = modules.extract_options!
@ -75,32 +74,6 @@ module Devise
def devise_modules_hook!
yield
end
# Find an initialize a record setting an error if it can't be found.
def find_or_initialize_with_error_by(attribute, value, error=:invalid) #:nodoc:
if value.present?
conditions = { attribute => value }
record = find(:first, :conditions => conditions)
end
unless record
record = new
if value.present?
record.send(:"#{attribute}=", value)
else
error = :blank
end
record.errors.add(attribute, error)
end
record
end
def define_devise_class_attributes! #:nodoc:
return if respond_to?(:devise_modules)
class_attribute :devise_modules, :instance_writer => false
self.devise_modules = []
end
end
end

View File

@ -39,6 +39,11 @@ module Devise
module Authenticatable
extend ActiveSupport::Concern
included do
class_attribute :devise_modules, :instance_writer => false
self.devise_modules ||= []
end
# Check if the current object is valid for authentication. This method and find_for_authentication
# are the methods used in a Warden::Strategy to check if a model should be signed in or not.
#
@ -86,6 +91,26 @@ module Devise
def find_for_authentication(conditions)
find(:first, :conditions => conditions)
end
# Find an initialize a record setting an error if it can't be found.
def find_or_initialize_with_error_by(attribute, value, error=:invalid) #:nodoc:
if value.present?
conditions = { attribute => value }
record = find(:first, :conditions => conditions)
end
unless record
record = new
if value.present?
record.send(:"#{attribute}=", value)
else
error = :blank
end
record.errors.add(attribute, error)
end
record
end
end
end
end