Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 57 lines (44 sloc) 1.345 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
module ActionController
  
  module IphoneController
    
    def self.included(base)
      base.extend(ClassMethods)
    end
    
    module ClassMethods
      def acts_as_iphone_controller(iphone_test_mode = nil)
        include ActionController::IphoneController::InstanceMethods
        if iphone_test_mode
          before_filter :force_iphone_format
        else
          before_filter :set_iphone_format
        end
        helper_method :is_iphone_request?
      end
      
      def iphone_test_mode?
        @@iphone_test_mode
      end
    end
    
    module InstanceMethods
      
      def force_iphone_format
        request.format = :iphone
      end
      
      def set_iphone_format
        if is_iphone_request? || is_iphone_format? || is_iphone_subdomain?
          request.format = if cookies["browser"] == "desktop"
                           then :html
                           else :iphone
                           end
        end
      end
      
      def is_iphone_format?
        request.format.to_sym == :iphone
      end

      def is_iphone_request?
        request.user_agent =~ /(Mobile\/.+Safari)/
      end
      
      def is_iphone_subdomain?
        request.subdomains.first == "iphone"
      end
    end
    
  end
  
end

ActionController::Base.send(:include, ActionController::IphoneController)
Something went wrong with that request. Please try again.