Skip to content

Instantly share code, notes, and snippets.

@timurvafin
Last active December 30, 2015 05:39
Show Gist options
  • Save timurvafin/7784069 to your computer and use it in GitHub Desktop.
Save timurvafin/7784069 to your computer and use it in GitHub Desktop.
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
include Concerns::SessionManagementr.rb
end
# app/models/current_user.rb
class CurrentUser
attr_reader :session
def initialize(session)
@session = session
end
def sign_in(user)
session[:user_id] = user.id
end
def sign_out
session[:user_id] = nil
end
def user
@user ||= User.find(session[:user_id]) if session[:user_id]
end
def signed_in?
user.present?
end
end
# app/controllers/concerns/session_management.rb
module Concerns::SessionManagement
extend ActiveSupport::Concern
included do
attr_accesible :current_user
delegate :sugned_in?, :current_user
helper_method :current_user, :signed_in?
end
end
self.current_user = CurrentUser.new(session).sign_in(user)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment