Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

New convenient helper method for extending Devise with additional mod…

…ules: Devise::add_module.
  • Loading branch information...
commit f50ec773b2d56e8a9d59e65e8442a9c404ca7613 1 parent 6d80418
@grimen grimen authored josevalim committed
Showing with 55 additions and 1 deletion.
  1. +32 −0 lib/devise.rb
  2. +23 −1 test/devise_test.rb
View
32 lib/devise.rb
@@ -180,6 +180,38 @@ def orm_class
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
24 test/devise_test.rb
@@ -2,7 +2,7 @@
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
Please sign in to comment.
Something went wrong with that request. Please try again.