Changeset 3844
- Timestamp:
- 03/12/06 06:02:44 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/lib/action_controller/mime_responds.rb
r3842 r3844 27 27 @responses = {} 28 28 end 29 30 def custom(mime_type, *args, &block) 31 mime_type = mime_type.is_a?(Mime::Type) ? mime_type : Mime::Type.lookup(mime_type.to_s) 32 33 @order << mime_type 34 35 if block_given? 36 @responses[mime_type] = block 37 else 38 if argument = args.first 39 eval("__mime_responder_arg__ = " + (argument.is_a?(String) ? "'" + argument + "'" : argument), @block_binding) 40 @responses[mime_type] = eval(DEFAULT_BLOCKS[(mime_type.to_sym.to_s + "_arg").to_sym], @block_binding) 41 else 42 @responses[mime_type] = eval(DEFAULT_BLOCKS[mime_type.to_sym], @block_binding) 43 end 44 end 45 end 29 46 30 47 for mime_type in %w( all html js xml rss atom yaml ) 31 48 eval <<-EOT 32 49 def #{mime_type}(argument = nil, &block) 33 @order << Mime::#{mime_type.upcase} 34 35 if block_given? 36 @responses[Mime::#{mime_type.upcase}] = block 37 else 38 if argument 39 eval("__mime_responder_arg__ = " + (argument.is_a?(String) ? "'" + argument + "'" : argument), @block_binding) 40 @responses[Mime::#{mime_type.upcase}] = eval(DEFAULT_BLOCKS[(Mime::#{mime_type.upcase}.to_sym.to_s + "_arg").to_sym], @block_binding) 41 else 42 @responses[Mime::#{mime_type.upcase}] = eval(DEFAULT_BLOCKS[Mime::#{mime_type.upcase}.to_sym], @block_binding) 43 end 44 end 50 custom(Mime::#{mime_type.upcase}, argument, &block) 45 51 end 46 52 EOT … … 53 59 return 54 60 else 55 if @order.include?(priority)61 if priority === @order 56 62 @responses[priority].call 57 63 return # mime type match found, be happy and return trunk/actionpack/lib/action_controller/mime_type.rb
r3843 r3844 1 1 module Mime 2 2 class Type < String 3 def initialize(string, part_of_all = true) 4 @part_of_all = part_of_all 3 def self.lookup(string) 4 LOOKUP[string] 5 end 6 7 def initialize(string, symbol = nil, synonyms = []) 8 @symbol, @synonyms = symbol, synonyms 5 9 super(string) 6 10 end 7 11 8 12 def to_sym 9 SYMBOLIZED_MIME_TYPES[self] ? SYMBOLIZED_MIME_TYPES[self] :to_sym13 @symbol || to_sym 10 14 end 11 15 12 16 def ===(list) 13 17 if list.is_a?(Array) 14 list.include?(self)18 (@synonyms + [ self ]).any? { |synonym| list.include?(synonym) } 15 19 else 16 20 super … … 19 23 end 20 24 21 SYMBOLIZED_MIME_TYPES = { 22 "" => :unspecified, 23 "*/*" => :all, 24 "text/html" => :html, 25 "application/javascript" => :js, 26 "application/x-javascript" => :js, 27 "text/javascript" => :js, 28 "text/xml" => :xml, 29 "application/xml" => :xml, 30 "application/rss+xml" => :rss, 31 "application/rss+atom" => :atom, 32 "application/x-xml" => :xml, 33 "application/x-yaml" => :yaml 34 } 25 ALL = Type.new "*/*", :all 26 HTML = Type.new "text/html", :html 27 JS = Type.new "text/javascript", :js, %w( application/javascript application/x-javascript ) 28 XML = Type.new "text/xml", :xml, %w( application/xml application/x-xml ) 29 RSS = Type.new "application/rss+xml", :rss 30 ATOM = Type.new "application/atom+xml", :atom 31 YAML = Type.new "application/x-yaml", :yaml 35 32 36 ALL = Type.new "*/*" 37 HTML = Type.new "text/html" 38 JS = Type.new "text/javascript" 39 JAVASCRIPT = Type.new "text/javascript" 40 XML = Type.new "application/xml" 41 RSS = Type.new "application/rss+xml" 42 ATOM = Type.new "application/atom+xml" 43 YAML = Type.new "application/x-yaml" 33 LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) } 34 35 LOOKUP["*/*"] = ALL 36 LOOKUP["text/html"] = HTML 37 LOOKUP["application/rss+xml"] = RSS 38 LOOKUP["application/atom+xml"] = ATOM 39 LOOKUP["application/x-yaml"] = YAML 40 41 LOOKUP["text/javascript"] = JS 42 LOOKUP["application/javascript"] = JS 43 LOOKUP["application/x-javascript"] = JS 44 45 LOOKUP["text/xml"] = XML 46 LOOKUP["application/xml"] = XML 47 LOOKUP["application/x-xml"] = XML 44 48 end trunk/actionpack/lib/action_controller/request.rb
r3843 r3844 62 62 end 63 63 64 @content_type = Mime::Type. new(@content_type)64 @content_type = Mime::Type.lookup(@content_type) 65 65 end 66 66 … … 72 72 else 73 73 @env['HTTP_ACCEPT'].split(";").collect! do |mime_type| 74 Mime::Type. new(mime_type.strip)74 Mime::Type.lookup(mime_type.strip) 75 75 end 76 76 end trunk/actionpack/test/controller/mime_responds_test.rb
r3843 r3844 53 53 type.rss { render :text => "RSS" } 54 54 type.atom { render :text => "ATOM" } 55 type.all { render :text => "Nothing" } 56 end 57 end 58 59 def custom_type_handling 60 respond_to do |type| 61 type.html { render :text => "HTML" } 62 type.custom("application/crazy-xml") { render :text => "Crazy XML" } 55 63 type.all { render :text => "Nothing" } 56 64 end … … 156 164 assert_equal "RSS", @response.body 157 165 end 166 167 def test_synonyms 168 @request.env["HTTP_ACCEPT"] = "application/javascript" 169 get :js_or_html 170 assert_equal 'JS', @response.body 171 172 @request.env["HTTP_ACCEPT"] = "application/x-xml" 173 get :using_argument_defaults 174 assert_equal "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n <name>David</name>\n</person>\n", @response.body 175 end 176 177 def test_custom_types 178 @request.env["HTTP_ACCEPT"] = "application/crazy-xml" 179 get :custom_type_handling 180 assert_equal 'Crazy XML', @response.body 181 182 @request.env["HTTP_ACCEPT"] = "text/html" 183 get :custom_type_handling 184 assert_equal 'HTML', @response.body 185 end 158 186 end