Last active
December 30, 2015 05:39
-
-
Save timurvafin/7784069 to your computer and use it in GitHub Desktop.
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
# app/controllers/application_controller.rb | |
class ApplicationController < ActionController::Base | |
include Concerns::SessionManagementr.rb | |
end |
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
# 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 |
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
# 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 |
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
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