moduleDevisemoduleModelsautoload:Activatable,'devise/models/activatable'autoload:DatabaseAuthenticatable,'devise/models/database_authenticatable'autoload:Confirmable,'devise/models/confirmable'autoload:Lockable,'devise/models/lockable'autoload:Recoverable,'devise/models/recoverable'autoload:Rememberable,'devise/models/rememberable'autoload:Registerable,'devise/models/registerable'autoload:Timeoutable,'devise/models/timeoutable'autoload:Trackable,'devise/models/trackable'autoload:Validatable,'devise/models/validatable'# Creates configuration values for Devise and for the given module.## Devise::Models.config(Devise::Authenticable, :stretches, 10)## The line above creates:## 1) An accessor called Devise.stretches, which value is used by default;## 2) Some class methods for your model Model.stretches and Model.stretches=# which have higher priority than Devise.stretches;## 3) And an instance method stretches.## To add the class methods you need to have a module ClassMethods defined# inside the given class.#defself.config(mod,*accessors)#:nodoc:accessors.eachdo|accessor|mod.class_eval<<-METHOD,__FILE__,__LINE__+1
def #{accessor}
if defined?(@#{accessor})
@#{accessor}
elsif superclass.respond_to?(:#{accessor})
superclass.#{accessor}
else
Devise.#{accessor}
end
end
def #{accessor}=(value)
@#{accessor} = value
end
METHODendend# Include the chosen devise modules in your model:## devise :authenticatable, :confirmable, :recoverable## You can also give any of the devise configuration values in form of a hash,# with specific values for this model. Please check your Devise initializer# for a complete description on those values.#defdevise(*modules)raise"You need to give at least one Devise module"ifmodules.empty?options=modules.extract_options!ifmodules.delete(:authenticatable)ActiveSupport::Deprecation.warn":authenticatable as module is deprecated. Please give :database_authenticatable instead.",callermodules<<:database_authenticatableend@devise_modules=Devise::ALL&modules.map(&:to_sym).uniqDevise.orm_class.included_modules_hook(self)dodevise_modules.eachdo|m|includeDevise::Models.const_get(m.to_s.classify)endoptions.each{|key,value|send(:"#{key}=",value)}endend# Stores all modules included inside the model, so we are able to verify# which routes are needed.defdevise_modules@devise_modules||=[]end# Find an initialize a record setting an error if it can't be found.deffind_or_initialize_with_error_by(attribute,value,error=:invalid)ifvalue.present?conditions={attribute=>value}record=find(:first,:conditions=>conditions)endunlessrecordrecord=newifvalue.present?record.send(:"#{attribute}=",value)elseerror,skip_default=:blank,trueendadd_error_on(record,attribute,error,!skip_default)endrecordend# Wraps add error logic in a method that works for different frameworks.defadd_error_on(record,attribute,error,add_default=true)options=add_default?{:default=>error.to_s.gsub("_"," ")}:{}beginrecord.errors.add(attribute,error,options)rescueArgumentErrorrecord.errors.add(attribute,error.to_s.gsub("_"," "))endendendend