Skip to content

Commit

Permalink
Use ActiveSupport::Concern.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Feb 17, 2010
1 parent 8e21373 commit 691f932
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 51 deletions.
17 changes: 8 additions & 9 deletions lib/devise/controllers/helpers.rb
Expand Up @@ -2,17 +2,16 @@ module Devise
module Controllers
# Those helpers are convenience methods added to ApplicationController.
module Helpers
extend ActiveSupport::Concern

def self.included(base)
base.class_eval do
helper_method :warden, :signed_in?, :devise_controller?,
*Devise.mappings.keys.map { |m| [:"current_#{m}", :"#{m}_signed_in?"] }.flatten
included do
helper_method :warden, :signed_in?, :devise_controller?,
*Devise.mappings.keys.map { |m| [:"current_#{m}", :"#{m}_signed_in?"] }.flatten

# Use devise default_url_options. We have to declare it here to overwrite
# default definitions.
def default_url_options(options=nil)
Devise::Mapping.default_url_options
end
# Use devise default_url_options. We have to declare it here to overwrite
# default definitions.
def default_url_options(options=nil)
Devise::Mapping.default_url_options
end
end

Expand Down
10 changes: 4 additions & 6 deletions lib/devise/models/authenticatable.rb
Expand Up @@ -27,13 +27,11 @@ module Models
# User.find(1).valid_password?('password123') # returns true/false
#
module Authenticatable
def self.included(base)
base.class_eval do
extend ClassMethods
extend ActiveSupport::Concern

attr_reader :password, :current_password
attr_accessor :password_confirmation
end
included do
attr_reader :password, :current_password
attr_accessor :password_confirmation
end

# Regenerates password salt and encrypted password each time password is set,
Expand Down
11 changes: 4 additions & 7 deletions lib/devise/models/confirmable.rb
Expand Up @@ -29,15 +29,12 @@ module Models
# User.find(1).send_confirmation_instructions # manually send instructions
# User.find(1).resend_confirmation! # generates a new token and resent it
module Confirmable
extend ActiveSupport::Concern
include Devise::Models::Activatable

def self.included(base)
base.class_eval do
extend ClassMethods

before_create :generate_confirmation_token, :if => :confirmation_required?
after_create :send_confirmation_instructions, :if => :confirmation_required?
end
included do
before_create :generate_confirmation_token, :if => :confirmation_required?
after_create :send_confirmation_instructions, :if => :confirmation_required?
end

# Confirm a user by setting it's confirmed_at to actual time. If the user
Expand Down
4 changes: 1 addition & 3 deletions lib/devise/models/http_authenticatable.rb
Expand Up @@ -6,9 +6,7 @@ module Models
# model class responds to authenticate and authentication_keys methods
# (which for example are defined in authenticatable).
module HttpAuthenticatable
def self.included(base)
base.extend ClassMethods
end
extend ActiveSupport::Concern

module ClassMethods
# Authenticate an user using http.
Expand Down
7 changes: 1 addition & 6 deletions lib/devise/models/lockable.rb
Expand Up @@ -18,14 +18,9 @@ module Models
# available when unlock_strategy is :time or :both.
#
module Lockable
extend ActiveSupport::Concern
include Devise::Models::Activatable

def self.included(base)
base.class_eval do
extend ClassMethods
end
end

# Lock an user setting it's locked_at to actual time.
def lock
self.locked_at = Time.now
Expand Down
6 changes: 1 addition & 5 deletions lib/devise/models/recoverable.rb
Expand Up @@ -14,11 +14,7 @@ module Models
# # creates a new token and send it with instructions about how to reset the password
# User.find(1).send_reset_password_instructions
module Recoverable
def self.included(base)
base.class_eval do
extend ClassMethods
end
end
extend ActiveSupport::Concern

# Update password saving the record and clearing token. Returns true if
# the passwords are valid and the record was saved, false otherwise.
Expand Down
11 changes: 4 additions & 7 deletions lib/devise/models/rememberable.rb
Expand Up @@ -30,14 +30,11 @@ module Models
# # lookup the user based on the incoming cookie information
# User.serialize_from_cookie(cookie_string)
module Rememberable
extend ActiveSupport::Concern

def self.included(base)
base.class_eval do
extend ClassMethods

# Remember me option available in after_authentication hook.
attr_accessor :remember_me
end
included do
# Remember me option available in after_authentication hook.
attr_accessor :remember_me
end

# Generate a new remember token and save the record without validations.
Expand Down
4 changes: 1 addition & 3 deletions lib/devise/models/timeoutable.rb
Expand Up @@ -11,9 +11,7 @@ module Models
#
# timeout_in: the time you want to timeout the user session without activity.
module Timeoutable
def self.included(base)
base.extend ClassMethods
end
extend ActiveSupport::Concern

# Checks whether the user session has expired based on configured time.
def timedout?(last_access)
Expand Down
9 changes: 4 additions & 5 deletions lib/devise/models/token_authenticatable.rb
Expand Up @@ -18,11 +18,10 @@ module Models
# User.find(1).valid_authentication_token?('rI1t6PKQ8yP7VetgwdybB') # returns true/false
#
module TokenAuthenticatable
def self.included(base)
base.class_eval do
extend ClassMethods
before_save :ensure_authentication_token
end
extend ActiveSupport::Concern

included do
before_save :ensure_authentication_token
end

# Generate new authentication token (a.k.a. "single access token").
Expand Down

0 comments on commit 691f932

Please sign in to comment.