Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Clean up a bit default_response handling and cache format negotiation.

  • Loading branch information...
commit 20bc3d4aaa2ee13667b14cc20e2c31c32ef7489e 1 parent 776a373
@josevalim josevalim authored
View
33 actionpack/lib/action_controller/metal/mime_responds.rb
@@ -192,8 +192,8 @@ def respond_to(*mimes, &block)
raise ArgumentError, "respond_to takes either types or a block, never both" if mimes.any? && block_given?
if collector = retrieve_collector_from_mimes(mimes, &block)
- response = collector.response_for(negotiated_format(collector)) || collector.default_response
- response.call(nil)
+ response = collector.response
+ response ? response.call : default_render({})
end
end
@@ -236,14 +236,13 @@ def respond_with(*resources, &block)
if collector = retrieve_collector_from_mimes(&block)
options = resources.size == 1 ? {} : resources.extract_options!
- if defined_response = collector.response_for(negotiated_format(collector))
+ if defined_response = collector.response
if action = options.delete(:action)
render :action => action
else
- defined_response.call(nil)
+ defined_response.call
end
else
- options.merge!(:default_response => collector.default_response)
(options.delete(:responder) || self.class.responder).call(self, resources, options)
end
end
@@ -275,10 +274,11 @@ def collect_mimes_from_class_level #:nodoc:
#
def retrieve_collector_from_mimes(mimes=nil, &block) #:nodoc:
mimes ||= collect_mimes_from_class_level
- collector = Collector.new(mimes) { |options| default_render(options || {}) }
+ collector = Collector.new(mimes)
block.call(collector) if block_given?
+ format = collector.negotiate_format(request)
- if format = negotiated_format(collector)
+ if format
self.content_type ||= format.to_s
lookup_context.freeze_formats([format.to_sym])
collector
@@ -288,17 +288,12 @@ def retrieve_collector_from_mimes(mimes=nil, &block) #:nodoc:
end
end
- def negotiated_format(collector)
- request.negotiate_mime(collector.order)
- end
-
class Collector #:nodoc:
include AbstractController::Collector
- attr_accessor :order
- attr_reader :default_response
+ attr_accessor :order, :format
- def initialize(mimes, &block)
- @order, @responses, @default_response = [], {}, block
+ def initialize(mimes)
+ @order, @responses = [], {}
mimes.each { |mime| send(mime) }
end
@@ -317,8 +312,12 @@ def custom(mime_type, &block)
@responses[mime_type] ||= block
end
- def response_for(mime)
- @responses[mime] || @responses[Mime::ALL]
+ def response
+ @responses[format] || @responses[Mime::ALL]
+ end
+
+ def negotiate_format(request)
+ @format = request.negotiate_mime(order)
end
end
end
View
3  actionpack/lib/action_controller/metal/responder.rb
@@ -129,7 +129,6 @@ def initialize(controller, resources, options={})
@resources = resources
@options = options
@action = options.delete(:action)
- @default_response = options.delete(:default_response)
end
delegate :head, :render, :redirect_to, :to => :controller
@@ -226,7 +225,7 @@ def resource_location
# controller.
#
def default_render
- @default_response.call(options)
+ controller.default_render(options)
end
# Display is just a shortcut to render a resource with the current format.

1 comment on commit 20bc3d4

@sikachu
Collaborator

HAHA thx! I need you to help me clean up again.

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