Created
September 6, 2012 15:56
-
-
Save ddemaree/3657772 to your computer and use it in GitHub Desktop.
Sample code for WCR2012 lightning talk
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
class ApplicationController < ActionController::Base | |
def current_user | |
User.find_by_id(session[:user_id]) | |
end | |
def current_user=(user) | |
session[:user_id] = user.id | |
end | |
def logged_in? | |
!!self.current_user | |
end | |
def require_login | |
unless logged_in? | |
redirect_to login_url and return false | |
end | |
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/authentication.rb | |
module Authentication | |
def current_user | |
User.find_by_id(session[:user_id]) | |
end | |
def current_user=(user) | |
session[:user_id] = user.id | |
end | |
def logged_in? | |
!!self.current_user | |
end | |
# Any other methods you want, so long as they | |
# pertain to authentication | |
end | |
class ApplicationController < ActionController::Base | |
include Authentication | |
# Where do I look for the authentication stuff? | |
# In Authentication, of course! | |
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
def foo | |
# Does some stuff ... | |
if @user.status == :active | |
@user.status = :inactive | |
@user.save! | |
end | |
end | |
def bar | |
# Does some other stuff ... | |
if @user.status == :active | |
@user.status = :inactive | |
@user.save! | |
end | |
end | |
def baz | |
# Does yet other stuff ... | |
if @user.status == :active | |
@user.status = :inactive | |
@user.save! | |
end | |
end | |
def deactivate_user(user) | |
if user.status == :active | |
user.status = :inactive | |
user.save! | |
end | |
end | |
def foo | |
# Does some stuff ... | |
deactivate_user(@user) | |
end | |
def bar | |
# Does some other stuff ... | |
deactivate_user(@user) | |
end | |
def baz | |
# Does yet other stuff ... | |
deactivate_user(@user) | |
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
class User < ActiveRecord::Base | |
def adobe_profile | |
AdobeId::Profile.new(self.adobe_profile_id) | |
end | |
def connected_to_adobe_profile? | |
!self.adobe_profile_id.nil? | |
end | |
def connect_to_adobe_profile(profile_obj) | |
self.adobe_profile_id = profile_obj.id | |
self.email_unique = false | |
save | |
end | |
def disconnect_adobe_profile | |
self.adobe_profile_id = nil | |
self.email_unique = true | |
save | |
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
module UserAdobeProfile | |
def adobe_profile | |
AdobeId::Profile.new(self.adobe_profile_id) | |
end | |
def connected_to_adobe_profile? | |
!self.adobe_profile_id.nil? | |
end | |
# Anything else pertaining to users' Adobe profiles | |
end | |
class User < ActiveRecord::Base | |
include UserAdobeProfile | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment