1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

New convenient helper method for extending Devise with additional modules: Devise::add_module.

This commit is contained in:
Jonas Grimfelt 2010-01-21 08:19:36 +08:00 committed by José Valim
parent 6d80418fd1
commit f50ec773b2
2 changed files with 55 additions and 1 deletions

View file

@ -180,6 +180,38 @@ module Devise
def friendly_token
ActiveSupport::SecureRandom.base64(15).tr('+/=', '-_ ').strip.delete("\n")
end
# Make Devise aware of an 3rd party Devise-module. For convenience.
#
# == Options:
#
# +strategy+ - Boolean value representing if this module got a custom *strategy*.
# Default is +false+. Note: Devise will auto-detect this in such case if this is true.
# +model+ - String representing a load path to a custom *model* for this module (to autoload).
# Default is +nil+ (i.e. +false+).
# +controller+ - Symbol representing a name of an exisiting or custom *controller* for this module.
# Default is +nil+ (i.e. +false+).
#
# == Examples:
#
# Devise.add_module(:party_module)
# Devise.add_module(:party_module, :strategy => true, :controller => :sessions)
# Devise.add_module(:party_module, :model => 'party_module/model')
#
def add_module(module_name, options = {})
Devise::ALL.unshift module_name unless Devise::ALL.include?(module_name)
Devise::STRATEGIES.unshift module_name if options[:strategy] && !Devise::STRATEGIES.include?(module_name)
if options[:controller].present?
controller = options[:controller].to_sym
Devise::CONTROLLERS[controller] ||= []
Devise::CONTROLLERS[controller].unshift module_name unless Devise::CONTROLLERS[controller].include?(module_name)
end
if options[:model].present?
Devise::Models.module_eval do
autoload :"#{module_name.to_s.classify}", options[:model]
end
end
end
end
end

View file

@ -2,7 +2,7 @@ require 'test/test_helper'
module Devise
def self.clean_warden_config!
@warden_config = nil
@warden_config = nil
end
end
@ -44,4 +44,26 @@ class DeviseTest < ActiveSupport::TestCase
Devise.clean_warden_config!
end
end
test 'add new module using the helper method' do
assert_nothing_raised(Exception) { Devise.add_module(:coconut) }
assert_equal 1, Devise::ALL.select { |v| v == :coconut }.size
assert_not Devise::STRATEGIES.include?(:coconut)
assert_not defined?(Devise::Models::Coconut)
Devise::ALL.delete(:coconut)
assert_nothing_raised(Exception) { Devise.add_module(:banana, :strategy => true) }
assert_equal 1, Devise::STRATEGIES.select { |v| v == :banana }.size
Devise::ALL.delete(:banana)
Devise::STRATEGIES.delete(:banana)
assert_nothing_raised(Exception) { Devise.add_module(:kivi, :controller => :fruits) }
assert_not_nil Devise::CONTROLLERS[:fruits]
assert_equal 1, Devise::CONTROLLERS[:fruits].select { |v| v == :kivi }.size
Devise::ALL.delete(:kivi)
Devise::CONTROLLERS.delete(:fruits)
assert_nothing_raised(Exception) { Devise.add_module(:authenticatable_again, :model => 'devise/model/authenticatable') }
assert defined?(Devise::Models::AuthenticatableAgain)
end
end