public
Created

Sample code for WCR2012 lightning talk

  • Download Gist
application_controller.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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
authentication.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# 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
duplication.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
 
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
user.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
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
user_adobe_profile.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.