Created
April 26, 2012 23:13
-
-
Save bjjb/2504017 to your computer and use it in GitHub Desktop.
Table-less UserSession "model" for Rails 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding : utf-8 -*- | |
class UserSession | |
include ActiveModel::Conversion | |
include ActiveModel::Validations | |
extend ActiveModel::Naming | |
validates_presence_of :email | |
validates_presence_of :password | |
attr_accessor :email, :password | |
def initialize(*args) | |
attributes = HashWithIndifferentAccess.new(args.extract_options!) | |
self.email = attributes[:email] | |
self.password = attributes[:password] | |
end | |
validate do | |
unless user.try(:authenticate, password) | |
errors.add(:base, "Invalid email or password.") | |
end | |
end | |
def user | |
@user ||= User.find_by_email(email) if email | |
end | |
def persisted? | |
false # always POST | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment