Refactor a bit navigational format responses, to not call to_sym and reuse the request_format logic from FailureApp.

This commit is contained in:
José Valim 2011-03-30 14:09:12 +02:00
parent b6652abc7c
commit 5b94d9b803
8 changed files with 44 additions and 48 deletions

View File

@ -1,7 +1,7 @@
PATH
remote: .
specs:
devise (1.2.0)
devise (1.3.0.dev)
bcrypt-ruby (~> 2.1.2)
orm_adapter (~> 0.0.3)
warden (~> 1.0.3)

View File

@ -15,9 +15,7 @@ class Devise::ConfirmationsController < ApplicationController
set_flash_message(:notice, :send_instructions) if is_navigational_format?
respond_with resource, :location => new_session_path(resource_name)
else
respond_with(resource) do |format|
format.any(*navigational_formats) { render_with_scope :new }
end
respond_with_navigational(resource){ render_with_scope :new }
end
end
@ -28,13 +26,9 @@ class Devise::ConfirmationsController < ApplicationController
if resource.errors.empty?
set_flash_message(:notice, :confirmed) if is_navigational_format?
sign_in(resource_name, resource)
respond_with(resource) do |format|
format.any(*navigational_formats) { redirect_to redirect_location(resource_name, resource) }
end
respond_with_navigational(resource){ redirect_to redirect_location(resource_name, resource) }
else
respond_with(resource.errors, :status => :unprocessable_entity) do |format|
format.any(*navigational_formats) { render_with_scope :new }
end
respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render_with_scope :new }
end
end
end

View File

@ -16,9 +16,7 @@ class Devise::PasswordsController < ApplicationController
set_flash_message(:notice, :send_instructions) if is_navigational_format?
respond_with resource, :location => new_session_path(resource_name)
else
respond_with(resource) do |format|
format.any(*navigational_formats) { render_with_scope :new }
end
respond_with_navigational(resource){ render_with_scope :new }
end
end
@ -38,9 +36,7 @@ class Devise::PasswordsController < ApplicationController
sign_in(resource_name, resource)
respond_with resource, :location => redirect_location(resource_name, resource)
else
respond_with(resource) do |format|
format.any(*navigational_formats) { render_with_scope :edit }
end
respond_with_navigational(resource){ render_with_scope :edit }
end
end
end

View File

@ -25,9 +25,7 @@ class Devise::RegistrationsController < ApplicationController
end
else
clean_up_passwords(resource)
respond_with(resource) do |format|
format.any(*navigational_formats) { render_with_scope :new }
end
respond_with_navigational(resource) { render_with_scope :new }
end
end
@ -44,9 +42,7 @@ class Devise::RegistrationsController < ApplicationController
respond_with resource, :location => after_update_path_for(resource)
else
clean_up_passwords(resource)
respond_with(resource) do |format|
format.any(*navigational_formats) { render_with_scope :edit }
end
respond_with_navigational(resource){ render_with_scope :edit }
end
end
@ -55,9 +51,7 @@ class Devise::RegistrationsController < ApplicationController
resource.destroy
Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
set_flash_message :notice, :destroyed if is_navigational_format?
respond_with(resource) do |format|
format.any(*navigational_formats) { redirect_to after_sign_out_path_for(resource_name) }
end
respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name) }
end
# GET /resource/cancel

View File

@ -22,12 +22,15 @@ class Devise::SessionsController < ApplicationController
Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
set_flash_message :notice, :signed_out if signed_in
# We actually need to hard coded this, as Rails default responder doesn't
# We actually need to hardcode this, as Rails default responder doesn't
# support returning empty response on GET request
respond_to do |format|
format.any(*navigational_formats) { redirect_to after_sign_out_path_for(resource_name) }
format.xml { head :ok }
format.json { render :text => '{}', :status => :ok }
format.all do
method = "to_#{request_format}"
text = {}.respond_to?(method) ? {}.send(method) : ""
render :text => text, :status => :ok
end
end
end
end

View File

@ -16,9 +16,7 @@ class Devise::UnlocksController < ApplicationController
set_flash_message :notice, :send_instructions if is_navigational_format?
respond_with resource, :location => new_session_path(resource_name)
else
respond_with(resource) do |format|
format.any(*navigational_formats) { render_with_scope :new }
end
respond_with_navigational(resource){ render_with_scope :new }
end
end
@ -29,13 +27,9 @@ class Devise::UnlocksController < ApplicationController
if resource.errors.empty?
set_flash_message :notice, :unlocked if is_navigational_format?
sign_in(resource_name, resource)
respond_with(resource) do |format|
format.any(*navigational_formats) { redirect_to redirect_location(resource_name, resource) }
end
respond_with_navigational(resource){ redirect_to redirect_location(resource_name, resource) }
else
respond_with(resource.errors, :status => :unprocessable_entity) do |format|
format.any(*navigational_formats) { render_with_scope :new }
end
respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render_with_scope :new }
end
end
end

View File

@ -7,6 +7,19 @@ module Devise
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
else # Rails < 3.0.4
request.format.to_sym
end
end
included do
helper DeviseHelper
@ -52,6 +65,10 @@ 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!("Could not find devise mapping for path #{request.fullpath.inspect}") unless devise_mapping
@ -59,10 +76,10 @@ module Devise
# Check whether it's navigational format, such as :html or :iphone, or not.
def is_navigational_format?
navigational_formats.include?(request.format.to_sym)
Devise.navigational_formats.include?(request_format)
end
# Returns real navigational formats which supported by Rails
# 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] }
end
@ -119,6 +136,12 @@ module Devise
def clean_up_passwords(object) #:nodoc:
object.clean_up_passwords if object.respond_to?(:clean_up_passwords)
end
def respond_with_navigational(*args, &block)
respond_with(*args) do |format|
format.any(*navigational_formats, &block)
end
end
end
end
end

View File

@ -128,16 +128,8 @@ module Devise
session["#{scope}_return_to"] = attempted_path if request.get? && !http_auth?
end
MIME_REFERENCES = Mime::HTML.respond_to?(:ref)
def request_format
@request_format ||= if request.format.respond_to?(:ref)
request.format.ref
elsif MIME_REFERENCES
request.format
else # Rails < 3.0.4
request.format.to_sym
end
@request_format ||= Devise::Controllers::InternalHelpers.request_format(request)
end
end
end