Skip to content

Instantly share code, notes, and snippets.

@bjjb
Created April 26, 2012 23:13
Show Gist options
  • Save bjjb/2504017 to your computer and use it in GitHub Desktop.
Save bjjb/2504017 to your computer and use it in GitHub Desktop.
Table-less UserSession "model" for Rails 3
# -*- 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