Skip to content

Instantly share code, notes, and snippets.

@YuriFontella
Created June 29, 2015 15:35
Show Gist options
  • Save YuriFontella/97fa55e7609a34338e55 to your computer and use it in GitHub Desktop.
Save YuriFontella/97fa55e7609a34338e55 to your computer and use it in GitHub Desktop.
class Session
include ActiveModel::Model
attr_accessor :email, :password
validates :email, :presence => { :message => 'Entre com o seu e-mail' }
validates :password, :presence => { :message => 'Coloque a sua senha' }
def initialize(attr={})
@email = attr[:email]
@password = attr[:password]
end
def authenticate!
return false if @email.blank? || @password.blank?
user = User.find_by(email: @email)
if user && user.authenticate(@password)
session[:id] = user.id
else
errors.add(:invalid, 'Usuário ou senha incorretos')
false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment