1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Remove any resource logic from respond_to.

This commit is contained in:
José Valim 2009-07-30 22:43:37 +02:00
parent 2c2ca833a5
commit 7a4a679dba

View file

@ -177,19 +177,21 @@ module ActionController #:nodoc:
# Be sure to check respond_with and respond_to documentation for more examples. # Be sure to check respond_with and respond_to documentation for more examples.
# #
def respond_to(*mimes, &block) def respond_to(*mimes, &block)
options = mimes.extract_options!
raise ArgumentError, "respond_to takes either types or a block, never both" if mimes.any? && block_given? raise ArgumentError, "respond_to takes either types or a block, never both" if mimes.any? && block_given?
resource = options.delete(:with)
responder = Responder.new responder = Responder.new
mimes = collect_mimes_from_class_level if mimes.empty? mimes = collect_mimes_from_class_level if mimes.empty?
mimes.each { |mime| responder.send(mime) } mimes.each { |mime| responder.send(mime) }
block.call(responder) if block_given? block.call(responder) if block_given?
if format = request.negotiate_mime(responder.order) if format = request.negotiate_mime(responder.order)
respond_to_block_or_template_or_resource(format, resource, self.formats = [format.to_sym]
options, &responder.response_for(format))
if response = responder.response_for(format)
response.call
else
default_render
end
else else
head :not_acceptable head :not_acceptable
end end
@ -257,26 +259,21 @@ module ActionController #:nodoc:
# end # end
# #
def respond_with(resource, options={}, &block) def respond_with(resource, options={}, &block)
respond_to(options.merge!(:with => resource), &block)
end
protected
def respond_to_block_or_template_or_resource(format, resource, options)
self.formats = [format.to_sym]
return yield if block_given?
begin begin
default_render respond_to(&block)
rescue ActionView::MissingTemplate => e rescue ActionView::MissingTemplate => e
if resource && resource.respond_to?(:"to_#{format.to_sym}") format = self.formats.first
render options.merge(format.to_sym => resource)
if resource.respond_to?(:"to_#{format}")
render options.merge(format => resource)
else else
raise e raise e
end end
end end
end end
protected
# Collect mimes declared in the class method respond_to valid for the # Collect mimes declared in the class method respond_to valid for the
# current action. # current action.
# #