Skip to content

Instantly share code, notes, and snippets.

@matthooks
Created April 13, 2009 22:01
Show Gist options
  • Save matthooks/94756 to your computer and use it in GitHub Desktop.
Save matthooks/94756 to your computer and use it in GitHub Desktop.
# ACTIVATIONS_CONTROLLER.RB
class ActivationsController < ApplicationController
before_filter :require_no_user, :only => [:new, :create]
before_filter :load_and_verify_user, :only => [:new, :create]
def new
render
end
def create
@user.assign_credentials!(params) unless params[:user].nil?
@user.activate!
@user.save do |result|
if result
@user.deliver_perishable_email!(:activation_confirmation)
flash[:notice] = "Your account has been activated."
redirect_to admin_user_path(@user)
else
render :action => :new
end
end
end
private
def load_and_verify_user
@user = User.find_using_perishable_token(params[:activation_code], 1.week)
logger.info ">>>>> LOAD #{session.inspect}"
if @user.nil? || @user.confirmed?
flash[:error] = "We could not locate your account."
redirect_to root_path and return
end
end
end
# USER.RB
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation, :openid_identifier
# AUTHLOGIC
acts_as_authentic do |u|
u.validates_length_of_password_field_options = validates_length_of_password_field_options.merge({ :on => :update })
end
# NAMED SCOPES
named_scope :active, :conditions => { :active => true }
named_scope :inactive, :conditions => { :active => false }
named_scope :by_created_at, :order => "created_at DESC"
# CALLBACKS
after_create :deliver_activation_instructions!
def signup!(params)
self.email = params[:user][:email]
save
end
def assign_credentials!(params)
self.password = params[:user][:password]
self.password_confirmation = params[:user][:password_confirmation]
self.openid_identifier = params[:user][:openid_identifier]
end
def activate!
self.active = true
self.confirmed = true
end
# Email notifications
def deliver_perishable_email!(email)
reset_perishable_token!
Notifier.send("deliver_#{email}".to_sym, self)
end
def deliver_activation_instructions!
self.deliver_perishable_email!(:activation_instructions)
end
# Helper methods
def active?
active == true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment