Permalink
Browse files

Devise should not attempt to load OmniAuth strategies. Strategies sho…

…uld be loaded before hand by the developer or explicitly given to Devise.
  • Loading branch information...
1 parent 1d2f906 commit 582f00ed95a7a226634d477b9950f5b8fb10451c @josevalim josevalim committed Nov 20, 2011
View
5 CHANGELOG.rdoc
@@ -1,3 +1,8 @@
+== 1.5.1
+
+* bug fix
+ * Devise should not attempt to load OmniAuth strategies. Strategies should be loaded before hand by the developer or explicitly given to Devise.
+
== 1.5.0
* enhancements
View
2 lib/devise/omniauth.rb
@@ -1,6 +1,6 @@
begin
require "omniauth"
- require 'omniauth/version'
+ require "omniauth/version"
rescue LoadError => e
warn "Could not load 'omniauth'. Please ensure you have the omniauth gem >= 1.0.0 installed and listed in your Gemfile."
raise
View
45 lib/devise/omniauth/config.rb
@@ -1,23 +1,28 @@
module Devise
module OmniAuth
+ class StrategyNotFound < NameError
+ def initialize(strategy)
+ @strategy = strategy
+ super("Could not find a strategy with name `#{strategy}'. " \
+ "Please ensure it is required or explicitly set it using the :klass option.")
+ end
+ end
+
class Config
attr_accessor :strategy
- attr_reader :args, :options, :provider
+ attr_reader :args, :options, :provider, :strategy_name
def initialize(provider, args)
- @provider = provider
- @args = args
- @strategy = nil
- @options = @args.last.is_a?(Hash) ? @args.last : {}
- end
-
- # open_id strategy can have configurable name
- def strategy_name
- options[:name] || @provider
+ @provider = provider
+ @args = args
+ @options = @args.last.is_a?(Hash) ? @args.last : {}
+ @strategy = nil
+ @strategy_name = options[:name] || @provider
+ @strategy_class = options.delete(:strategy_class)
end
def strategy_class
- find_strategy || require_strategy
+ @strategy_class ||= find_strategy || autoload_strategy
end
def find_strategy
@@ -27,20 +32,14 @@ def find_strategy
end
end
- def require_strategy
- if [:facebook, :github, :twitter].include?(provider.to_sym)
- require "omniauth/strategies/#{provider}"
- elsif options[:require]
- require options[:require]
+ def autoload_strategy
+ name = ::OmniAuth::Utils.camelize(provider.to_s)
+ if ::OmniAuth::Strategies.const_defined?(name)
+ ::OmniAuth::Strategies.const_get(name)
else
- require "omniauth-#{provider}"
+ raise StrategyNotFound, name
end
- find_strategy || autoload_strategy
- end
-
- def autoload_strategy
- ::OmniAuth::Strategies.const_get(::OmniAuth::Utils.camelize(provider.to_s))
end
end
end
-end
+end
View
17 test/omniauth/config_test.rb
@@ -1,8 +1,8 @@
require 'test_helper'
class OmniAuthConfigTest < ActiveSupport::TestCase
- setup do
- $: << File.dirname(__FILE__)
+ class MyStrategy
+ include OmniAuth::Strategy
end
test 'strategy_name returns provider if no options given' do
@@ -42,15 +42,16 @@ class OmniAuthConfigTest < ActiveSupport::TestCase
assert_equal UnNamedTestStrategy, config.strategy_class
end
- test 'attempts to load an as-yet not loaded plugin' do
- config = Devise::OmniAuth::Config.new :my_strategy, [{}]
- config_class = config.strategy_class
- assert_equal MyStrategy, config_class
+ test 'raises an error if strategy cannot be found' do
+ config = Devise::OmniAuth::Config.new :my_other_strategy, [{}]
+ assert_raise Devise::OmniAuth::StrategyNotFound do
+ config.strategy_class
+ end
end
test 'allows the user to define a custom require path' do
- config = Devise::OmniAuth::Config.new :my_other_strategy, [{:require => 'my_other_strategy'}]
+ config = Devise::OmniAuth::Config.new :my_strategy, [{:strategy_class => MyStrategy}]
config_class = config.strategy_class
- assert_equal MyOtherStrategy, config_class
+ assert_equal MyStrategy, config_class
end
end
View
5 test/omniauth/my_other_strategy.rb
@@ -1,5 +0,0 @@
-require 'omniauth'
-
-class MyOtherStrategy
- include OmniAuth::Strategy
-end
View
5 test/omniauth/omniauth-my_strategy.rb
@@ -1,5 +0,0 @@
-require 'omniauth'
-
-class MyStrategy
- include OmniAuth::Strategy
-end
View
3 test/rails_app/config/initializers/devise.rb
@@ -1,3 +1,6 @@
+require "omniauth-facebook"
+require "omniauth-openid"
+
# Use this hook to configure devise mailer, warden hooks and so forth. The first
# four configuration values can also be set straight in your models.
Devise.setup do |config|

0 comments on commit 582f00e

Please sign in to comment.