diff --git a/lib/devise.rb b/lib/devise.rb index 2383629c..19a26cd8 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -19,7 +19,6 @@ module Devise autoload :InternalHelpers, 'devise/controllers/internal_helpers' autoload :Rememberable, 'devise/controllers/rememberable' autoload :ScopedViews, 'devise/controllers/scoped_views' - autoload :SharedHelpers, 'devise/controllers/shared_helpers' autoload :UrlHelpers, 'devise/controllers/url_helpers' end @@ -202,9 +201,8 @@ module Devise @@skip_session_storage = [] # Which formats should be treated as navigational. - # We need both :"*/*" and "*/*" to work on different Rails versions. mattr_accessor :navigational_formats - @@navigational_formats = [:"*/*", "*/*", :html] + @@navigational_formats = ["*/*", :html] # When set to true, signing out a user signs out all other scopes. mattr_accessor :sign_out_all_scopes @@ -215,7 +213,8 @@ module Devise @@sign_out_via = :get # The parent controller all Devise controllers inherits from. - # Defaults to ApplicationController. + # Defaults to ApplicationController. This should be set early + # in the initialization process and should be set to a string. mattr_accessor :parent_controller @@parent_controller = "ApplicationController" diff --git a/lib/devise/controllers/internal_helpers.rb b/lib/devise/controllers/internal_helpers.rb index adb9c9b0..bc88b727 100644 --- a/lib/devise/controllers/internal_helpers.rb +++ b/lib/devise/controllers/internal_helpers.rb @@ -6,7 +6,6 @@ module Devise module InternalHelpers #:nodoc: extend ActiveSupport::Concern include Devise::Controllers::ScopedViews - include Devise::Controllers::SharedHelpers included do helper DeviseHelper @@ -157,6 +156,14 @@ MESSAGE format.any(*navigational_formats, &block) end end + + def request_format + @request_format ||= request.format.try(:ref) + end + + def is_navigational_format? + Devise.navigational_formats.include?(request.format.try(:ref)) + end end end end diff --git a/lib/devise/controllers/shared_helpers.rb b/lib/devise/controllers/shared_helpers.rb deleted file mode 100644 index cf0954b8..00000000 --- a/lib/devise/controllers/shared_helpers.rb +++ /dev/null @@ -1,26 +0,0 @@ -module Devise - module Controllers - # Helpers used in both FailureApp and Devise controllers. - module SharedHelpers - MIME_REFERENCES = Mime::HTML.respond_to?(:ref) - - protected - - # Helper used by FailureApp and Devise controllers to retrieve proper formats. - def request_format - @request_format ||= if request.format.respond_to?(:ref) - request.format.ref - elsif MIME_REFERENCES - request.format - elsif request.format # Rails < 3.0.4 - request.format.to_sym - end - end - - # Check whether it's navigational format, such as :html or :iphone, or not. - def is_navigational_format? - Devise.navigational_formats.include?(request_format) - end - end - end -end \ No newline at end of file diff --git a/lib/devise/failure_app.rb b/lib/devise/failure_app.rb index aea1fe27..e923a5be 100644 --- a/lib/devise/failure_app.rb +++ b/lib/devise/failure_app.rb @@ -10,7 +10,6 @@ module Devise include ActionController::UrlFor include ActionController::Redirecting include Rails.application.routes.url_helpers - include Devise::Controllers::SharedHelpers delegate :flash, :to => :request @@ -149,5 +148,13 @@ module Devise def store_location! session["#{scope}_return_to"] = attempted_path if request.get? && !http_auth? end + + def is_navigational_format? + Devise.navigational_formats.include?(request_format) + end + + def request_format + @request_format ||= request.format.try(:ref) + end end end diff --git a/lib/generators/templates/devise.rb b/lib/generators/templates/devise.rb index f66258b7..69728d9e 100644 --- a/lib/generators/templates/devise.rb +++ b/lib/generators/templates/devise.rb @@ -201,9 +201,8 @@ Devise.setup do |config| # If you have any extra navigational formats, like :iphone or :mobile, you # should add them to the navigational formats lists. # - # The :"*/*" and "*/*" formats below is required to match Internet - # Explorer requests. - # config.navigational_formats = [:"*/*", "*/*", :html] + # The "*/*" below is required to match Internet Explorer requests. + # config.navigational_formats = ["*/*", :html] # The default HTTP method used to sign out a resource. Default is :delete. config.sign_out_via = :delete diff --git a/test/failure_app_test.rb b/test/failure_app_test.rb index ddf77fd9..1108218c 100644 --- a/test/failure_app_test.rb +++ b/test/failure_app_test.rb @@ -73,14 +73,14 @@ class FailureTest < ActiveSupport::TestCase test 'works for any navigational format' do swap Devise, :navigational_formats => [:xml] do - call_failure('formats' => :xml) + call_failure('formats' => Mime::XML) assert_equal 302, @response.first end end test 'redirects the correct format if it is a non-html format request' do swap Devise, :navigational_formats => [:js] do - call_failure('formats' => :js) + call_failure('formats' => Mime::JS) assert_equal 'http://test.host/users/sign_in.js', @response.second["Location"] end end @@ -88,18 +88,18 @@ class FailureTest < ActiveSupport::TestCase context 'For HTTP request' do test 'return 401 status' do - call_failure('formats' => :xml) + call_failure('formats' => Mime::XML) assert_equal 401, @response.first end test 'return appropriate body for xml' do - call_failure('formats' => :xml) + call_failure('formats' => Mime::XML) result = %(\n\n You need to sign in or sign up before continuing.\n\n) assert_equal result, @response.last.body end test 'return appropriate body for json' do - call_failure('formats' => :json) + call_failure('formats' => Mime::JSON) result = %({"error":"You need to sign in or sign up before continuing."}) assert_equal result, @response.last.body end @@ -110,26 +110,26 @@ class FailureTest < ActiveSupport::TestCase end test 'return WWW-authenticate headers if model allows' do - call_failure('formats' => :xml) + call_failure('formats' => Mime::XML) assert_equal 'Basic realm="Application"', @response.second["WWW-Authenticate"] end test 'does not return WWW-authenticate headers if model does not allow' do swap Devise, :http_authenticatable => false do - call_failure('formats' => :xml) + call_failure('formats' => Mime::XML) assert_nil @response.second["WWW-Authenticate"] end end test 'works for any non navigational format' do swap Devise, :navigational_formats => [] do - call_failure('formats' => :html) + call_failure('formats' => Mime::HTML) assert_equal 401, @response.first end end test 'uses the failure message as response body' do - call_failure('formats' => :xml, 'warden' => OpenStruct.new(:message => :invalid)) + call_failure('formats' => Mime::XML, 'warden' => OpenStruct.new(:message => :invalid)) assert_match 'Invalid email or password.', @response.third.body end @@ -137,7 +137,7 @@ class FailureTest < ActiveSupport::TestCase context 'when http_authenticatable_on_xhr is false' do test 'dont return 401 with navigational formats' do swap Devise, :http_authenticatable_on_xhr => false do - call_failure('formats' => :html, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest') + call_failure('formats' => Mime::HTML, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest') assert_equal 302, @response.first assert_equal 'http://test.host/users/sign_in', @response.second["Location"] end @@ -145,7 +145,7 @@ class FailureTest < ActiveSupport::TestCase test 'dont return 401 with non navigational formats' do swap Devise, :http_authenticatable_on_xhr => false do - call_failure('formats' => :json, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest') + call_failure('formats' => Mime::JSON, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest') assert_equal 302, @response.first assert_equal 'http://test.host/users/sign_in.json', @response.second["Location"] end @@ -155,14 +155,14 @@ class FailureTest < ActiveSupport::TestCase context 'when http_authenticatable_on_xhr is true' do test 'return 401' do swap Devise, :http_authenticatable_on_xhr => true do - call_failure('formats' => :html, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest') + call_failure('formats' => Mime::HTML, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest') assert_equal 401, @response.first end end test 'skip WWW-Authenticate header' do swap Devise, :http_authenticatable_on_xhr => true do - call_failure('formats' => :html, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest') + call_failure('formats' => Mime::HTML, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest') assert_nil @response.second['WWW-Authenticate'] end end