-
-
Save kdwinter/444888 to your computer and use it in GitHub Desktop.
module Authenticable | |
extend ActiveSupport::Concern | |
included do | |
key :username, String | |
key :email, String | |
key :crypted_password, String | |
key :password_salt, String | |
key :persistence_token, String | |
key :login_count, Integer, :default => 0 | |
key :last_request_at, Time | |
key :last_login_at, Time | |
key :current_login_at, Time | |
key :last_login_ip, String | |
key :current_login_ip, String | |
class << self | |
alias named_scope scope | |
end | |
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 | |
acts_as_authentic do |c| | |
c.login_field = 'username' | |
c.merge_validates_uniqueness_of_login_field_options :scope => '_id', :case_sensitive => true | |
end | |
end | |
module ClassMethods | |
def base_class | |
self | |
end | |
def primary_key | |
:_id | |
end | |
def default_timezone | |
:utc | |
end | |
def with_scope(qry) | |
qry = where(qry) if qry.is_a?(Hash) | |
yield qry | |
end | |
end | |
module InstanceMethods | |
def save(options = {}) | |
options = {:validate => options} unless options.is_a?(Hash) | |
super | |
end | |
def readonly? | |
false | |
end | |
end | |
end |
class User | |
include MongoMapper::Document | |
plugin Authenticable | |
timestamps! | |
end |
class UserSession < Authlogic::Session::Base | |
def to_key | |
new_record? ? nil : [self.send(self.class.primary_key)] | |
end | |
end |
You can put them anywhere in your load path. I personally made a new app/plugins directory in which I put authenticable.rb, and I put the validatable fix inside lib somewhere.
Got it!
Thank you
Is the validatable fix gone?
That fix should no longer be required with the newer versions (that make use of ActiveModel) of MongoMapper.
Awesome, thanks! I am close, but I keep getting:
undefined method `login_field' for User:Class
But I see:
34 model.acts_as_authentic do |c|
35 c.login_field = 'username'
36 c.merge_validates_uniqueness_of_login_field_options :scope => '_id', :case_sensitive => true
37 end
Any suggestions?
FYI, I am using this guide for the rest of AuthLogic / Rails 3 : http://www.dixis.com/?p=352
I'm occasionally now getting this issue when I create a new user in the system:
NoMethodError (undefined method []=' for BSON::ObjectId('4e6e5c8414a6940001000006'):BSON::ObjectId): 2011-09-12T19:24:52+00:00 app[web.1]: app/models/authenticable.rb:58:in
save'
2011-09-12T19:24:52+00:00 app[web.1]: app/controllers/users_controller.rb:4:in `create'
The error isn't consistant. I can get it to fail when I deploy on heroku+mongohq, but it doesn't fail when I run it locally (with a local mongodb).
Any ideas?
I'm getting the following error:
undefined local variable or method `model' for User:Class
With Rails 3.1.3.
i getting the following error:
uninitialized constant ActiveSupport::Concern (NameError)
with rails 2.3.11 and mongomapper
I'm getting same error with spockz...
How should i fix that error?
Thanks.
Hey Gigamo,
I have a newbie question, the files user.rb and user_session.rb goes into the app/models folder, but where does the other two files goes? Do I have to manually create a gem for each ?
Thank you