Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Renamed Clearance::App::Models::User to Clearance::User

  • Loading branch information...
commit 3b1049bb0c9a40789bc9bea78484e333e28c5dc3 1 parent ca927e9
Joe Ferris jferris authored
4 generators/clearance/templates/app/models/user.rb
View
@@ -1,3 +1,3 @@
class User < ActiveRecord::Base
- include Clearance::App::Models::User
-end
+ include Clearance::User
+end
2  lib/clearance.rb
View
@@ -1,7 +1,7 @@
require 'clearance/lib/extensions/errors'
require 'clearance/lib/extensions/rescue'
require 'clearance/authentication'
-require 'clearance/app/models/user'
+require 'clearance/user'
require 'clearance/test/functional/confirmations_controller_test'
require 'clearance/test/functional/passwords_controller_test'
require 'clearance/test/functional/sessions_controller_test'
118 lib/clearance/app/models/user.rb
View
@@ -1,118 +0,0 @@
-require 'digest/sha1'
-
-module Clearance
- module App
- module Models
- module User
-
- def self.included(model)
- model.extend ClassMethods
- model.send(:include, InstanceMethods)
-
- model.class_eval do
- attr_accessible :email, :password, :password_confirmation
- attr_accessor :password, :password_confirmation
-
- validates_presence_of :email
- validates_presence_of :password, :if => :password_required?
- validates_confirmation_of :password, :if => :password_required?
- validates_uniqueness_of :email, :case_sensitive => false
- validates_format_of :email, :with => %r{.+@.+\..+}
-
- before_save :initialize_salt, :encrypt_password, :initialize_token
- end
- end
-
- module InstanceMethods
- def authenticated?(password)
- encrypted_password == encrypt(password)
- end
-
- def encrypt(string)
- generate_hash("--#{salt}--#{string}--")
- end
-
- def remember?
- token_expires_at && Time.now.utc < token_expires_at
- end
-
- def remember_me!
- remember_me_until! 2.weeks.from_now.utc
- end
-
- def forget_me!
- clear_token
- save(false)
- end
-
- def confirm_email!
- self.email_confirmed = true
- self.token = nil
- save(false)
- end
-
- def forgot_password!
- generate_token
- save(false)
- end
-
- def update_password(new_password, new_password_confirmation)
- self.password = new_password
- self.password_confirmation = new_password_confirmation
- clear_token if valid?
- save
- end
-
- protected
-
- def generate_hash(string)
- Digest::SHA1.hexdigest(string)
- end
-
- def initialize_salt
- if new_record?
- self.salt = generate_hash("--#{Time.now.utc.to_s}--#{password}--")
- end
- end
-
- def encrypt_password
- return if password.blank?
- self.encrypted_password = encrypt(password)
- end
-
- def generate_token
- self.token = encrypt("--#{Time.now.utc.to_s}--#{password}--")
- self.token_expires_at = nil
- end
-
- def clear_token
- self.token = nil
- self.token_expires_at = nil
- end
-
- def initialize_token
- generate_token if new_record?
- end
-
- def password_required?
- encrypted_password.blank? || !password.blank?
- end
-
- def remember_me_until!(time)
- self.token_expires_at = time
- self.token = encrypt("--#{token_expires_at}--#{password}--")
- save(false)
- end
- end
-
- module ClassMethods
- def authenticate(email, password)
- return nil unless user = find_by_email(email)
- return user if user.authenticated?(password)
- end
- end
-
- end
- end
- end
-end
4 lib/clearance/authentication.rb
View
@@ -29,14 +29,14 @@ def authenticate
def user_from_session
if session[:user_id]
- return nil unless user = User.find_by_id(session[:user_id])
+ return nil unless user = ::User.find_by_id(session[:user_id])
return user if user.email_confirmed?
end
end
def user_from_cookie
if token = cookies[:remember_token]
- return nil unless user = User.find_by_token(token)
+ return nil unless user = ::User.find_by_token(token)
return user if user.remember?
end
end
2  lib/clearance/test/functional/confirmations_controller_test.rb
View
@@ -56,7 +56,7 @@ def self.included(controller_test)
end
context "no users" do
- setup { assert_equal 0, User.count }
+ setup { assert_equal 0, ::User.count }
should_forbid "on GET to #new with nonexistent id and token" do
get :new, :user_id => '123', :token => '123'
2  lib/clearance/test/functional/passwords_controller_test.rb
View
@@ -45,7 +45,7 @@ def self.included(controller_test)
context "with incorrect email address" do
setup do
email = "user1@example.com"
- assert ! User.exists?(['email = ?', email])
+ assert ! ::User.exists?(['email = ?', email])
ActionMailer::Base.deliveries.clear
assert_equal @user.token, @user.reload.token
4 lib/clearance/test/unit/user_test.rb
View
@@ -95,12 +95,12 @@ def @user.initialize_salt; end
end
should "authenticate with good credentials" do
- assert User.authenticate(@user.email, @password)
+ assert ::User.authenticate(@user.email, @password)
assert @user.authenticated?(@password)
end
should "not authenticate with bad credentials" do
- assert ! User.authenticate(@user.email, 'bad_password')
+ assert ! ::User.authenticate(@user.email, 'bad_password')
assert ! @user.authenticated?('bad_password')
end
end
114 lib/clearance/user.rb
View
@@ -0,0 +1,114 @@
+require 'digest/sha1'
+
+module Clearance
+ module User
+
+ def self.included(model)
+ model.extend ClassMethods
+ model.send(:include, InstanceMethods)
+
+ model.class_eval do
+ attr_accessible :email, :password, :password_confirmation
+ attr_accessor :password, :password_confirmation
+
+ validates_presence_of :email
+ validates_presence_of :password, :if => :password_required?
+ validates_confirmation_of :password, :if => :password_required?
+ validates_uniqueness_of :email, :case_sensitive => false
+ validates_format_of :email, :with => %r{.+@.+\..+}
+
+ before_save :initialize_salt, :encrypt_password, :initialize_token
+ end
+ end
+
+ module InstanceMethods
+ def authenticated?(password)
+ encrypted_password == encrypt(password)
+ end
+
+ def encrypt(string)
+ generate_hash("--#{salt}--#{string}--")
+ end
+
+ def remember?
+ token_expires_at && Time.now.utc < token_expires_at
+ end
+
+ def remember_me!
+ remember_me_until! 2.weeks.from_now.utc
+ end
+
+ def forget_me!
+ clear_token
+ save(false)
+ end
+
+ def confirm_email!
+ self.email_confirmed = true
+ self.token = nil
+ save(false)
+ end
+
+ def forgot_password!
+ generate_token
+ save(false)
+ end
+
+ def update_password(new_password, new_password_confirmation)
+ self.password = new_password
+ self.password_confirmation = new_password_confirmation
+ clear_token if valid?
+ save
+ end
+
+ protected
+
+ def generate_hash(string)
+ Digest::SHA1.hexdigest(string)
+ end
+
+ def initialize_salt
+ if new_record?
+ self.salt = generate_hash("--#{Time.now.utc.to_s}--#{password}--")
+ end
+ end
+
+ def encrypt_password
+ return if password.blank?
+ self.encrypted_password = encrypt(password)
+ end
+
+ def generate_token
+ self.token = encrypt("--#{Time.now.utc.to_s}--#{password}--")
+ self.token_expires_at = nil
+ end
+
+ def clear_token
+ self.token = nil
+ self.token_expires_at = nil
+ end
+
+ def initialize_token
+ generate_token if new_record?
+ end
+
+ def password_required?
+ encrypted_password.blank? || !password.blank?
+ end
+
+ def remember_me_until!(time)
+ self.token_expires_at = time
+ self.token = encrypt("--#{token_expires_at}--#{password}--")
+ save(false)
+ end
+ end
+
+ module ClassMethods
+ def authenticate(email, password)
+ return nil unless user = find_by_email(email)
+ return user if user.authenticated?(password)
+ end
+ end
+
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.