1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Create shared_helpers for duplicated logic in Devise controllers and failure app.

This commit is contained in:
José Valim 2011-04-17 19:43:54 +02:00
parent c6dd846718
commit 2cfa58b433
4 changed files with 30 additions and 27 deletions

View file

@ -16,6 +16,7 @@ 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

View file

@ -6,19 +6,7 @@ module Devise
module InternalHelpers #:nodoc:
extend ActiveSupport::Concern
include Devise::Controllers::ScopedViews
MIME_REFERENCES = Mime::HTML.respond_to?(:ref)
# Helper used by FailureApp and Devise controllers to retrieve proper formats.
def self.request_format(request)
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
include Devise::Controllers::SharedHelpers
included do
helper DeviseHelper
@ -65,10 +53,6 @@ module Devise
protected
def request_format
@request_format ||= Devise::Controllers::InternalHelpers.request_format(request)
end
# Checks whether it's a devise mapped resource or not.
def is_devise_resource? #:nodoc:
unknown_action! <<-MESSAGE unless devise_mapping
@ -81,11 +65,6 @@ Maybe you forgot to wrap your route inside the scope block? For example:
MESSAGE
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
# Returns real navigational formats which are supported by Rails
def navigational_formats
@navigational_formats ||= Devise.navigational_formats.select{ |format| Mime::EXTENSION_LOOKUP[format.to_s] }

View file

@ -0,0 +1,26 @@
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

View file

@ -10,6 +10,7 @@ module Devise
include ActionController::UrlFor
include ActionController::Redirecting
include Rails.application.routes.url_helpers
include Devise::Controllers::SharedHelpers
delegate :flash, :to => :request
@ -83,7 +84,7 @@ module Devise
if request.xhr?
Devise.http_authenticatable_on_xhr
else
!(request_format && Devise.navigational_formats.include?(request_format))
!(request_format && is_navigational_format?)
end
end
@ -135,9 +136,5 @@ module Devise
def store_location!
session["#{scope}_return_to"] = attempted_path if request.get? && !http_auth?
end
def request_format
@request_format ||= Devise::Controllers::InternalHelpers.request_format(request)
end
end
end