diff --git a/lib/devise.rb b/lib/devise.rb index 34dc0ef6..fec35b59 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -382,13 +382,10 @@ module Devise # Include helpers in the given scope to AC and AV. def self.include_helpers(scope) + Rails.application.routes.url_helpers.send :include, scope::UrlHelpers + ActiveSupport.on_load(:action_controller) do include scope::Helpers if defined?(scope::Helpers) - include scope::UrlHelpers - end - - ActiveSupport.on_load(:action_view) do - include scope::UrlHelpers end end diff --git a/lib/devise/controllers/url_helpers.rb b/lib/devise/controllers/url_helpers.rb index af1f19b2..20bca1bc 100644 --- a/lib/devise/controllers/url_helpers.rb +++ b/lib/devise/controllers/url_helpers.rb @@ -24,25 +24,31 @@ module Devise end end - def self.generate_helpers! - mappings = Devise.mappings.values.map(&:used_helpers).flatten.uniq - routes = Devise::URL_HELPERS.slice(*mappings) + def self.generate_helpers!(routes=nil) + routes ||= begin + mappings = Devise.mappings.values.map(&:used_helpers).flatten.uniq + Devise::URL_HELPERS.slice(*mappings) + end routes.each do |module_name, actions| [:path, :url].each do |path_or_url| actions.each do |action| action = action ? "#{action}_" : "" + method = "#{action}#{module_name}_#{path_or_url}" class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1 - def #{action}#{module_name}_#{path_or_url}(resource_or_scope, *args) + def #{method}(resource_or_scope, *args) scope = Devise::Mapping.find_scope!(resource_or_scope) send("#{action}\#{scope}_#{module_name}_#{path_or_url}", *args) end + protected :#{method} URL_HELPERS end end end end + + generate_helpers!(Devise::URL_HELPERS) end end end diff --git a/lib/devise/omniauth/url_helpers.rb b/lib/devise/omniauth/url_helpers.rb index 58cc1392..4bbf763b 100644 --- a/lib/devise/omniauth/url_helpers.rb +++ b/lib/devise/omniauth/url_helpers.rb @@ -3,9 +3,10 @@ module Devise module UrlHelpers def self.define_helpers(mapping) return unless mapping.omniauthable? + method = "#{mapping.name}_omniauth_authorize_path" class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1 - def #{mapping.name}_omniauth_authorize_path(provider, params = {}) + def #{method}(provider, params = {}) if Devise.omniauth_configs[provider.to_sym] script_name = request.env["SCRIPT_NAME"] @@ -16,9 +17,12 @@ module Devise raise ArgumentError, "Could not find omniauth provider \#{provider.inspect}" end end + protected :#{method} URL_HELPERS end + protected + def omniauth_authorize_path(resource_or_scope, *args) scope = Devise::Mapping.find_scope!(resource_or_scope) send("#{scope}_omniauth_authorize_path", *args) diff --git a/test/controllers/internal_helpers_test.rb b/test/controllers/internal_helpers_test.rb index 30f45609..3771278c 100644 --- a/test/controllers/internal_helpers_test.rb +++ b/test/controllers/internal_helpers_test.rb @@ -35,7 +35,7 @@ class HelpersTest < ActionController::TestCase end test 'resources methods are not controller actions' do - assert @controller.class.action_methods.empty? + assert @controller.class.action_methods.empty?, "Expected empty, got #{@controller.class.action_methods.inspect}" end test 'require no authentication tests current mapping' do diff --git a/test/omniauth/url_helpers_test.rb b/test/omniauth/url_helpers_test.rb index a9c6a966..f981e8ca 100644 --- a/test/omniauth/url_helpers_test.rb +++ b/test/omniauth/url_helpers_test.rb @@ -28,31 +28,31 @@ class OmniAuthRoutesTest < ActionController::TestCase end test 'should generate authorization path' do - assert_match "/users/auth/facebook", @controller.omniauth_authorize_path(:user, :facebook) + assert_match "/users/auth/facebook", @controller.send(:omniauth_authorize_path, :user, :facebook) assert_raise ArgumentError do - @controller.omniauth_authorize_path(:user, :github) + @controller.send :omniauth_authorize_path, :user, :github end end test 'should generate authorization path for named open_id omniauth' do - assert_match "/users/auth/google", @controller.omniauth_authorize_path(:user, :google) + assert_match "/users/auth/google", @controller.send(:omniauth_authorize_path, :user, :google) end test 'should generate authorization path with params' do assert_match "/users/auth/open_id?openid_url=http%3A%2F%2Fyahoo.com", - @controller.omniauth_authorize_path(:user, :open_id, :openid_url => "http://yahoo.com") + @controller.send(:omniauth_authorize_path, :user, :open_id, :openid_url => "http://yahoo.com") end test 'should not add a "?" if no param was sent' do assert_equal "/users/auth/open_id", - @controller.omniauth_authorize_path(:user, :open_id) + @controller.send(:omniauth_authorize_path, :user, :open_id) end test 'should set script name in the path if present' do @request.env['SCRIPT_NAME'] = '/q' assert_equal "/q/users/auth/facebook", - @controller.omniauth_authorize_path(:user, :facebook) + @controller.send(:omniauth_authorize_path, :user, :facebook) end end