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

Changeset 7520

Show
Ignore:
Timestamp:
09/20/07 23:34:07 (10 months ago)
Author:
david
Message:

Added that render :json will automatically call .to_json unless its being passed a string [DHH] Added Mime::Type.register_alias for dealing with different formats using the same mime type [DHH]

Files:

Legend:

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

    r7516 r7520  
    11*SVN* 
     2 
     3* Added Mime::Type.register_alias for dealing with different formats using the same mime type [DHH]. Example: 
     4 
     5    class PostsController < ApplicationController 
     6      before_filter :adjust_format_for_iphone 
     7 
     8      def index 
     9        @posts = Post.find(:all) 
     10         
     11        respond_to do |format| 
     12          format.html   # => renders index.html.erb and uses "text/html" as the content type 
     13          format.iphone # => renders index.iphone.erb and uses "text/html" as the content type 
     14        end 
     15      end 
     16 
     17 
     18      private 
     19        def adjust_format_for_iphone 
     20          if request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/iPhone/] 
     21            request.format = :iphone 
     22          end 
     23        end 
     24    end 
     25 
     26* Added that render :json will automatically call .to_json unless it's being passed a string [DHH]. 
    227 
    328* Autolink behaves well with emails embedded in URLs.  #7313 [Jeremy McAnally, tarmo] 
  • trunk/actionpack/lib/action_controller/base.rb

    r7473 r7520  
    827827 
    828828          elsif json = options[:json] 
     829            json = json.to_json unless json.is_a?(String) 
    829830            json = "#{options[:callback]}(#{json})" unless options[:callback].blank? 
    830831            response.content_type = Mime::JSON 
  • trunk/actionpack/lib/action_controller/mime_type.rb

    r7438 r7520  
    5353      end 
    5454 
    55       def register(string, symbol, mime_type_synonyms = [], extension_synonyms = []) 
     55      # Registers an alias that's not usd on mime type lookup, but can be referenced directly. Especially useful for 
     56      # rendering different HTML versions depending on the user agent, like an iPhone. 
     57      def register_alias(string, symbol, extension_synonyms = []) 
     58        register(string, symbol, [], extension_synonyms, true) 
     59      end 
     60 
     61      def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false) 
    5662        Mime.send :const_set, symbol.to_s.upcase, Type.new(string, symbol, mime_type_synonyms) 
    5763 
    5864        SET << Mime.send(:const_get, symbol.to_s.upcase) 
    5965 
    60         ([string] + mime_type_synonyms).each { |string| LOOKUP[string] = SET.last } 
     66        ([string] + mime_type_synonyms).each { |string| LOOKUP[string] = SET.last } unless skip_lookup 
    6167        ([symbol.to_s] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext] = SET.last } 
    6268      end