Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 3852

Show
Ignore:
Timestamp:
03/13/06 01:33:57 (3 years ago)
Author:
david
Message:

Rendering xml shouldnt happen inside any layout. Added class proxying to RJS, so you can call page.field.clear("my_field") to generate Field.clear("my_field");. Added :content_type option to render, so you can change the content type on the fly. Do type/subtype reordering of Accept header preferences for xml types (aka make Firefox work with respond_to). CHANGED DEFAULT: The default content type for .rxml is now application/xml instead of type/xml, see http://www.xml.com/pub/a/2004/07/21/dive.html for reason

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r3842 r3852  
    11*SVN* 
     2 
     3* Added :content_type option to render, so you can change the content type on the fly [DHH]. Example: render :action => "atom.rxml", :content_type => "application/atom+xml" 
     4 
     5* CHANGED DEFAULT: The default content type for .rxml is now application/xml instead of type/xml, see http://www.xml.com/pub/a/2004/07/21/dive.html for reason [DHH] 
    26 
    37* Added option to render action/template/file of a specific extension (and here by template type). This means you can have multiple templates with the same name but a different extension [DHH]. Example: 
  • trunk/actionpack/lib/action_controller/base.rb

    r3847 r3852  
    635635        end 
    636636 
     637        if content_type = options[:content_type] 
     638          headers["Content-Type"] = content_type 
     639        end 
     640 
    637641        if text = options[:text] 
    638642          render_text(text, options[:status]) 
  • trunk/actionpack/lib/action_controller/layout.rb

    r3551 r3852  
    246246      def candidate_for_layout?(options) 
    247247        (options.has_key?(:layout) && options[:layout] != false) ||  
    248         options.values_at(:text, :file, :inline, :partial, :nothing).compact.empty? && 
     248        options.values_at(:text, :xml, :file, :inline, :partial, :nothing).compact.empty? && 
    249249        !template_exempt_from_layout?(default_template_name(options[:action] || options[:template])) 
    250250      end 
  • trunk/actionpack/lib/action_controller/mime_type.rb

    r3850 r3852  
    11module Mime 
    22  class Type 
    3     def self.lookup(string) 
    4       LOOKUP[string] 
    5     end 
     3    class << self 
     4      def lookup(string) 
     5        LOOKUP[string] 
     6      end 
    67 
    7     def self.parse(accept_header) 
    8       accept_header.split(",").collect! do |mime_type| 
    9         Mime::Type.lookup(mime_type.split(";").first.strip) 
     8      def parse(accept_header) 
     9        mime_types = accept_header.split(",").collect! do |mime_type| 
     10          mime_type.split(";").first.strip 
     11        end 
     12 
     13        reorder_xml_types!(mime_types) 
     14        mime_types.collect! { |mime_type| Mime::Type.lookup(mime_type) } 
    1015      end 
     16       
     17      private 
     18        def reorder_xml_types!(mime_types) 
     19          mime_types.delete("text/xml") if mime_types.include?("application/xml") 
     20 
     21          if index_for_generic_xml = mime_types.index("application/xml") 
     22            specific_xml_types = mime_types[index_for_generic_xml..-1].grep(/application\/[a-z]*\+xml/) 
     23 
     24            for specific_xml_type in specific_xml_types.reverse 
     25              mime_types.insert(index_for_generic_xml, mime_types.delete(specific_xml_type)) 
     26            end 
     27          end 
     28        end 
    1129    end 
    1230     
     
    1836    def to_s 
    1937      @string 
     38    end 
     39     
     40    def to_str 
     41      to_s 
    2042    end 
    2143     
     
    4062  HTML  = Type.new "text/html", :html, %w( application/xhtml+xml ) 
    4163  JS    = Type.new "text/javascript", :js, %w( application/javascript application/x-javascript ) 
    42   XML   = Type.new "application/xml", :xml, %w( application/x-xml ) 
     64  XML   = Type.new "application/xml", :xml, %w( text/xml application/x-xml ) 
    4365  RSS   = Type.new "application/rss+xml", :rss 
    4466  ATOM  = Type.new "application/atom+xml", :atom 
  • trunk/actionpack/lib/action_view/base.rb

    r3841 r3852  
    407407            when :rxml 
    408408              "xml = Builder::XmlMarkup.new(:indent => 2)\n" + 
    409               "@controller.headers['Content-Type'] ||= 'text/xml'\n" + 
     409              "@controller.headers['Content-Type'] ||= 'application/xml'\n" + 
    410410              template 
    411411            when :rjs 
  • trunk/actionpack/lib/action_view/helpers/prototype_helper.rb

    r3814 r3852  
    619619           
    620620          private 
    621           def page 
    622             self 
    623           end 
    624            
    625           def record(line) 
    626             returning line = "#{line.to_s.chomp.gsub /\;$/, ''};" do 
    627               self << line 
     621            def page 
     622              self 
    628623            end 
    629           end 
    630            
    631           def render(*options_for_render) 
    632             Hash === options_for_render.first ?  
    633               @context.render(*options_for_render) :  
    634                 options_for_render.first.to_s 
    635           end 
    636            
    637           def javascript_object_for(object) 
    638             object.respond_to?(:to_json) ? object.to_json : object.inspect 
    639           end 
    640            
    641           def arguments_for_call(arguments) 
    642             arguments.map { |argument| javascript_object_for(argument) }.join ', ' 
    643           end 
     624           
     625            def record(line) 
     626              returning line = "#{line.to_s.chomp.gsub /\;$/, ''};" do 
     627                self << line 
     628              end 
     629            end 
     630           
     631            def render(*options_for_render) 
     632              Hash === options_for_render.first ?  
     633                @context.render(*options_for_render) :  
     634                  options_for_render.first.to_s 
     635            end 
     636           
     637            def javascript_object_for(object) 
     638              object.respond_to?(:to_json) ? object.to_json : object.inspect 
     639            end 
     640           
     641            def arguments_for_call(arguments) 
     642              arguments.map { |argument| javascript_object_for(argument) }.join ', ' 
     643            end 
     644             
     645            def method_missing(method, *arguments) 
     646              JavaScriptProxy.new(self, method.to_s.camelize) 
     647            end 
    644648        end 
    645649      end 
     
    719723            assign($1, arguments.first) 
    720724          else 
    721             call("#{method.to_s.first}#{method.to_s.classify[1..-1]}", *arguments) 
     725            call("#{method.to_s.first}#{method.to_s.camelize[1..-1]}", *arguments) 
    722726          end 
    723727        end 
  • trunk/actionpack/test/controller/action_pack_assertions_test.rb

    r3563 r3852  
    480480    @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new 
    481481  end 
     482 
    482483  def test_rendering_xml_sets_content_type 
    483484    process :hello_xml_world 
    484     assert_equal('text/xml', @controller.headers['Content-Type']) 
    485   end 
     485    assert_equal('application/xml', @controller.headers['Content-Type']) 
     486  end 
     487 
    486488  def test_rendering_xml_respects_content_type 
    487489    @response.headers['Content-Type'] = 'application/pdf' 
  • trunk/actionpack/test/template/prototype_helper_test.rb

    r3814 r3852  
    381381    EOS 
    382382  end 
     383 
     384  def test_class_proxy 
     385    @generator.form.focus('my_field') 
     386    assert_equal "Form.focus(\"my_field\");", @generator.to_s 
     387  end 
    383388end