Created
          November 16, 2010 16:11 
        
      - 
      
- 
        Save anolson/701986 to your computer and use it in GitHub Desktop. 
    Rails set mobile format.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | class ApplicationController < ActionController::Base | |
| protect_from_forgery | |
| before_filter :redirect_mobile_device | |
| before_filter :set_mobile_format | |
| private | |
| def redirect_mobile_device | |
| if is_mobile_device? | |
| redirect_to_mobile_subdomain | |
| end | |
| end | |
| def is_mobile_device? | |
| if session[:preferred_format] | |
| session[:preferred_format] != "html" | |
| else | |
| request.user_agent =~ /iPod|iPhone|iOS/ | |
| end | |
| end | |
| def redirect_to_mobile_subdomain | |
| redirect_to request.protocol + "mobile." + request.host_with_port + request.fullpath | |
| end | |
| def set_mobile_format | |
| if has_mobile_subdomain? | |
| request.format = :mobile | |
| end | |
| end | |
| def has_mobile_subdomain? | |
| request.subdomains.first == "mobile" | |
| end | |
| end | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | Mime::Type.register_alias "text/html", :mobile | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | require 'test_helper' | |
| class PagesControllerTest < ActionController::TestCase | |
| # Replace this with your real tests. | |
| test "should set mobile format for requests to mobile subdomain" do | |
| @request.host = "mobile.test.host" | |
| get :index | |
| assert_equal :mobile, @request.format.to_sym | |
| end | |
| test "should not set mobile format for requests without mobile subdomain" do | |
| get :index | |
| assert_equal :html, @request.format.to_sym | |
| end | |
| test "should not redirect to mobile subdomain for a non mobile device" do | |
| get :index | |
| assert_response :success | |
| end | |
| test "should redirect to mobile subdomain for a mobile device" do | |
| @request.user_agent = "iPhone" | |
| get :index | |
| assert_response :redirect | |
| assert_redirected_to :host => "mobile.test.host" | |
| end | |
| test "should not redirect to mobile subdomain for mobile device with preference for full site" do | |
| @request.user_agent = "iPhone" | |
| @request.session[:preferred_format] = "html" | |
| get :index | |
| assert_response :success | |
| end | |
| end | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | class SessionController < ApplicationController | |
| def update_preferred_format | |
| session[:preferred_format] = params[:preferred_format] | |
| redirect_to :root | |
| end | |
| end | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment