From f50ec773b2d56e8a9d59e65e8442a9c404ca7613 Mon Sep 17 00:00:00 2001 From: Jonas Grimfelt Date: Thu, 21 Jan 2010 08:19:36 +0800 Subject: [PATCH] New convenient helper method for extending Devise with additional modules: Devise::add_module. --- lib/devise.rb | 32 ++++++++++++++++++++++++++++++++ test/devise_test.rb | 24 +++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lib/devise.rb b/lib/devise.rb index df02255f..f3dbaf08 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -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 diff --git a/test/devise_test.rb b/test/devise_test.rb index 45234d17..9cd20b5c 100644 --- a/test/devise_test.rb +++ b/test/devise_test.rb @@ -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