From de660957a50227ef71a7c16524ccec2c45b980b5 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 12 Mar 2006 03:39:58 +0000 Subject: Added defaults to respond_to [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3842 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_controller/mime_responds.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/mime_responds.rb b/actionpack/lib/action_controller/mime_responds.rb index 42b6e24205..e8d1ba089b 100644 --- a/actionpack/lib/action_controller/mime_responds.rb +++ b/actionpack/lib/action_controller/mime_responds.rb @@ -13,6 +13,13 @@ module ActionController #:nodoc: end class Responder #:nodoc: + DEFAULT_BLOCKS = { + :html => 'Proc.new { render }', + :js => 'Proc.new { render :action => "#{action_name}.rjs" }', + :xml => 'Proc.new { render :action => "#{action_name}.rxml" }', + :xml_arg => 'Proc.new { render :xml => __mime_responder_arg__ }' + } + def initialize(block_binding) @block_binding = block_binding @mime_type_priority = eval("request.accepts", block_binding) @@ -22,9 +29,19 @@ module ActionController #:nodoc: for mime_type in %w( all html js xml rss atom yaml ) eval <<-EOT - def #{mime_type}(&block) + def #{mime_type}(argument = nil, &block) @order << Mime::#{mime_type.upcase} - @responses[Mime::#{mime_type.upcase}] = block + + if block_given? + @responses[Mime::#{mime_type.upcase}] = block + else + if argument + eval("__mime_responder_arg__ = " + (argument.is_a?(String) ? "'" + argument + "'" : argument), @block_binding) + @responses[Mime::#{mime_type.upcase}] = eval(DEFAULT_BLOCKS[(Mime::#{mime_type.upcase}.to_sym.to_s + "_arg").to_sym], @block_binding) + else + @responses[Mime::#{mime_type.upcase}] = eval(DEFAULT_BLOCKS[Mime::#{mime_type.upcase}.to_sym], @block_binding) + end + end end EOT end -- cgit v1.2.3