Skip to content

Instantly share code, notes, and snippets.

@lucasrenan
Forked from will/authlogic_model.rb
Created October 16, 2011 20:40
Show Gist options
  • Save lucasrenan/1291395 to your computer and use it in GitHub Desktop.
Save lucasrenan/1291395 to your computer and use it in GitHub Desktop.
authlogic_model
module AuthlogicModel
def self.included(model)
model.class_eval do
extend ClassMethods
include InstanceMethods
#include ProtectedAttributes
property :username
property :email
property :crypted_password
property :password_salt
property :persistence_token
property :login_count, :type => Integer, :default => 0
property :failed_login_count, :type => Integer, :default => 0
property :last_login_at, :type => DateTime
property :current_login_at, :type => DateTime
property :last_login_ip
property :current_login_ip
timestamps!
view_by :username,
:map => %q(
function(doc) {
if ((doc['couchrest-type'] == 'User') && (doc['username'] != null)) {
emit(doc['username'].toLowerCase(), null);
}
})
include Authlogic::ActsAsAuthentic::Base
include Authlogic::ActsAsAuthentic::Email
include Authlogic::ActsAsAuthentic::LoggedInStatus
include Authlogic::ActsAsAuthentic::Login
include Authlogic::ActsAsAuthentic::MagicColumns
include Authlogic::ActsAsAuthentic::Password
include Authlogic::ActsAsAuthentic::PerishableToken
include Authlogic::ActsAsAuthentic::PersistenceToken
include Authlogic::ActsAsAuthentic::RestfulAuthentication
include Authlogic::ActsAsAuthentic::SessionMaintenance
include Authlogic::ActsAsAuthentic::SingleAccessToken
include Authlogic::ActsAsAuthentic::ValidationsScope
end
end
module ClassMethods
def base_class
self
end
def find_by_persistence_token(token)
nil
end
def before_validation(*args)
before_validate *args
end
def <(klass)
return true if klass == ::ActiveRecord::Base
super(klass)
end
def column_names
properties.map &:name
end
def quoted_table_name
'users'
end
def primary_key
:id
end
def scope(*args)
# do nothing
end
def named_scope(*args)
# do nothing
end
def default_timezone
:utc
end
def find_by_id(id)
get id
end
def find_by_username(username)
by_username(:key => username.downcase).first
end
def with_scope(query)
yield query
end
end
module InstanceMethods
def ==(other)
return false unless other.is_a? self.class
self.id == other.id
end
def persistence_token_changed?
return true if new_record?
super
end
def readonly?
false
end
def username=(username)
username.downcase!
super
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment