Skip to content
This repository
Browse code

Refactor a bit navigational format responses, to not call to_sym and …

…reuse the request_format logic from FailureApp.
  • Loading branch information...
commit 5b94d9b803fdbd5086edb35b0690ff38b88e0f57 1 parent b6652ab
José Valim josevalim authored
2  Gemfile.lock
... ... @@ -1,7 +1,7 @@
1 1 PATH
2 2 remote: .
3 3 specs:
4   - devise (1.2.0)
  4 + devise (1.3.0.dev)
5 5 bcrypt-ruby (~> 2.1.2)
6 6 orm_adapter (~> 0.0.3)
7 7 warden (~> 1.0.3)
12 app/controllers/devise/confirmations_controller.rb
@@ -15,9 +15,7 @@ def create
15 15 set_flash_message(:notice, :send_instructions) if is_navigational_format?
16 16 respond_with resource, :location => new_session_path(resource_name)
17 17 else
18   - respond_with(resource) do |format|
19   - format.any(*navigational_formats) { render_with_scope :new }
20   - end
  18 + respond_with_navigational(resource){ render_with_scope :new }
21 19 end
22 20 end
23 21
@@ -28,13 +26,9 @@ def show
28 26 if resource.errors.empty?
29 27 set_flash_message(:notice, :confirmed) if is_navigational_format?
30 28 sign_in(resource_name, resource)
31   - respond_with(resource) do |format|
32   - format.any(*navigational_formats) { redirect_to redirect_location(resource_name, resource) }
33   - end
  29 + respond_with_navigational(resource){ redirect_to redirect_location(resource_name, resource) }
34 30 else
35   - respond_with(resource.errors, :status => :unprocessable_entity) do |format|
36   - format.any(*navigational_formats) { render_with_scope :new }
37   - end
  31 + respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render_with_scope :new }
38 32 end
39 33 end
40 34 end
8 app/controllers/devise/passwords_controller.rb
@@ -16,9 +16,7 @@ def create
16 16 set_flash_message(:notice, :send_instructions) if is_navigational_format?
17 17 respond_with resource, :location => new_session_path(resource_name)
18 18 else
19   - respond_with(resource) do |format|
20   - format.any(*navigational_formats) { render_with_scope :new }
21   - end
  19 + respond_with_navigational(resource){ render_with_scope :new }
22 20 end
23 21 end
24 22
@@ -38,9 +36,7 @@ def update
38 36 sign_in(resource_name, resource)
39 37 respond_with resource, :location => redirect_location(resource_name, resource)
40 38 else
41   - respond_with(resource) do |format|
42   - format.any(*navigational_formats) { render_with_scope :edit }
43   - end
  39 + respond_with_navigational(resource){ render_with_scope :edit }
44 40 end
45 41 end
46 42 end
12 app/controllers/devise/registrations_controller.rb
@@ -25,9 +25,7 @@ def create
25 25 end
26 26 else
27 27 clean_up_passwords(resource)
28   - respond_with(resource) do |format|
29   - format.any(*navigational_formats) { render_with_scope :new }
30   - end
  28 + respond_with_navigational(resource) { render_with_scope :new }
31 29 end
32 30 end
33 31
@@ -44,9 +42,7 @@ def update
44 42 respond_with resource, :location => after_update_path_for(resource)
45 43 else
46 44 clean_up_passwords(resource)
47   - respond_with(resource) do |format|
48   - format.any(*navigational_formats) { render_with_scope :edit }
49   - end
  45 + respond_with_navigational(resource){ render_with_scope :edit }
50 46 end
51 47 end
52 48
@@ -55,9 +51,7 @@ def destroy
55 51 resource.destroy
56 52 Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
57 53 set_flash_message :notice, :destroyed if is_navigational_format?
58   - respond_with(resource) do |format|
59   - format.any(*navigational_formats) { redirect_to after_sign_out_path_for(resource_name) }
60   - end
  54 + respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name) }
61 55 end
62 56
63 57 # GET /resource/cancel
9 app/controllers/devise/sessions_controller.rb
@@ -22,12 +22,15 @@ def destroy
22 22 Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
23 23 set_flash_message :notice, :signed_out if signed_in
24 24
25   - # We actually need to hard coded this, as Rails default responder doesn't
  25 + # We actually need to hardcode this, as Rails default responder doesn't
26 26 # support returning empty response on GET request
27 27 respond_to do |format|
28 28 format.any(*navigational_formats) { redirect_to after_sign_out_path_for(resource_name) }
29   - format.xml { head :ok }
30   - format.json { render :text => '{}', :status => :ok }
  29 + format.all do
  30 + method = "to_#{request_format}"
  31 + text = {}.respond_to?(method) ? {}.send(method) : ""
  32 + render :text => text, :status => :ok
  33 + end
31 34 end
32 35 end
33 36 end
12 app/controllers/devise/unlocks_controller.rb
@@ -16,9 +16,7 @@ def create
16 16 set_flash_message :notice, :send_instructions if is_navigational_format?
17 17 respond_with resource, :location => new_session_path(resource_name)
18 18 else
19   - respond_with(resource) do |format|
20   - format.any(*navigational_formats) { render_with_scope :new }
21   - end
  19 + respond_with_navigational(resource){ render_with_scope :new }
22 20 end
23 21 end
24 22
@@ -29,13 +27,9 @@ def show
29 27 if resource.errors.empty?
30 28 set_flash_message :notice, :unlocked if is_navigational_format?
31 29 sign_in(resource_name, resource)
32   - respond_with(resource) do |format|
33   - format.any(*navigational_formats) { redirect_to redirect_location(resource_name, resource) }
34   - end
  30 + respond_with_navigational(resource){ redirect_to redirect_location(resource_name, resource) }
35 31 else
36   - respond_with(resource.errors, :status => :unprocessable_entity) do |format|
37   - format.any(*navigational_formats) { render_with_scope :new }
38   - end
  32 + respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render_with_scope :new }
39 33 end
40 34 end
41 35 end
27 lib/devise/controllers/internal_helpers.rb
@@ -7,6 +7,19 @@ module InternalHelpers #:nodoc:
7 7 extend ActiveSupport::Concern
8 8 include Devise::Controllers::ScopedViews
9 9
  10 + MIME_REFERENCES = Mime::HTML.respond_to?(:ref)
  11 +
  12 + # Helper used by FailureApp and Devise controllers to retrieve proper formats.
  13 + def self.request_format(request)
  14 + if request.format.respond_to?(:ref)
  15 + request.format.ref
  16 + elsif MIME_REFERENCES
  17 + request.format
  18 + else # Rails < 3.0.4
  19 + request.format.to_sym
  20 + end
  21 + end
  22 +
10 23 included do
11 24 helper DeviseHelper
12 25
@@ -52,6 +65,10 @@ def devise_controller?
52 65
53 66 protected
54 67
  68 + def request_format
  69 + @request_format ||= Devise::Controllers::InternalHelpers.request_format(request)
  70 + end
  71 +
55 72 # Checks whether it's a devise mapped resource or not.
56 73 def is_devise_resource? #:nodoc:
57 74 unknown_action!("Could not find devise mapping for path #{request.fullpath.inspect}") unless devise_mapping
@@ -59,10 +76,10 @@ def is_devise_resource? #:nodoc:
59 76
60 77 # Check whether it's navigational format, such as :html or :iphone, or not.
61 78 def is_navigational_format?
62   - navigational_formats.include?(request.format.to_sym)
  79 + Devise.navigational_formats.include?(request_format)
63 80 end
64 81
65   - # Returns real navigational formats which supported by Rails
  82 + # Returns real navigational formats which are supported by Rails
66 83 def navigational_formats
67 84 @navigational_formats ||= Devise.navigational_formats.select{ |format| Mime::EXTENSION_LOOKUP[format.to_s] }
68 85 end
@@ -119,6 +136,12 @@ def set_flash_message(key, kind, options={}) #:nodoc:
119 136 def clean_up_passwords(object) #:nodoc:
120 137 object.clean_up_passwords if object.respond_to?(:clean_up_passwords)
121 138 end
  139 +
  140 + def respond_with_navigational(*args, &block)
  141 + respond_with(*args) do |format|
  142 + format.any(*navigational_formats, &block)
  143 + end
  144 + end
122 145 end
123 146 end
124 147 end
10 lib/devise/failure_app.rb
@@ -128,16 +128,8 @@ def store_location!
128 128 session["#{scope}_return_to"] = attempted_path if request.get? && !http_auth?
129 129 end
130 130
131   - MIME_REFERENCES = Mime::HTML.respond_to?(:ref)
132   -
133 131 def request_format
134   - @request_format ||= if request.format.respond_to?(:ref)
135   - request.format.ref
136   - elsif MIME_REFERENCES
137   - request.format
138   - else # Rails < 3.0.4
139   - request.format.to_sym
140   - end
  132 + @request_format ||= Devise::Controllers::InternalHelpers.request_format(request)
141 133 end
142 134 end
143 135 end

1 comment on commit 5b94d9b

Prem Sichanugrist

Awwww, you fixed a lot of mess!

Please sign in to comment.
Something went wrong with that request. Please try again.