Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added tablet user agents

  • Loading branch information...
commit 8b9d28c63513220a21f94fb5b0cff2cd59a7f5c9 1 parent 8104205
Zaid M. Said authored February 11, 2012
25  lib/mobile-fu.rb
@@ -25,11 +25,16 @@ class Railtie < Rails::Railtie
25 25
     end
26 26
 
27 27
     Mime::Type.register_alias "text/html", :mobile
  28
+    Mime::Type.register_alias "text/html", :tablet
28 29
   end
29 30
 end
30 31
 
31 32
 module ActionController
32 33
   module MobileFu
  34
+    # These are various strings that can be found in tablet devices.  Please feel free
  35
+    # to add on to this list.
  36
+    TABLET_USER_AGENTS =  'ipad|android 3.0|xoom|sch-i800|playbook|tablet|kindle'
  37
+
33 38
     def self.included(base)
34 39
       base.extend ClassMethods
35 40
     end
@@ -55,11 +60,13 @@ def has_mobile_fu(set_request_format = true)
55 60
         before_filter :set_request_format if set_request_format
56 61
 
57 62
         helper_method :is_mobile_device?
  63
+        helper_method :is_tablet_device?
58 64
         helper_method :in_mobile_view?
  65
+        helper_method :in_tablet_view?
59 66
         helper_method :is_device?
60 67
         helper_method :mobile_device
61 68
       end
62  
-      
  69
+
63 70
       # Add this to your controllers to prevent the mobile format from being set for specific actions
64 71
       #   class AwesomeController < ApplicationController
65 72
       #     has_no_mobile_fu_for :index
@@ -109,12 +116,24 @@ def in_mobile_view?
109 116
         request.format.to_sym == :mobile
110 117
       end
111 118
 
  119
+      # Returns either true or false depending on whether or not the format of the
  120
+      # request is either :tablet or not.
  121
+
  122
+      def in_tablet_view?
  123
+        return false unless request.format
  124
+        request.format.to_sym == :tablet
  125
+      end
  126
+
112 127
       # Returns either true or false depending on whether or not the user agent of
113 128
       # the device making the request is matched to a device in our regex.
114 129
 
115 130
       def is_mobile_device?
116 131
         !!mobile_device
117 132
       end
  133
+      
  134
+      def is_tablet_device?
  135
+        request.user_agent.to_s.downcase =~ Regexp.new(ActionController::MobileFu::TABLET_USER_AGENTS)
  136
+      end
118 137
 
119 138
       def mobile_device
120 139
         request.headers['X_MOBILE_DEVICE']
@@ -126,10 +145,10 @@ def mobile_device
126 145
       def is_device?(type)
127 146
         request.user_agent.to_s.downcase.include? type.to_s.downcase
128 147
       end
129  
-      
  148
+
130 149
       # Returns true if current action isn't supposed to use mobile format
131 150
       # See #has_no_mobile_fu_for
132  
-      
  151
+
133 152
       def mobile_exempt?
134 153
         self.class.instance_variable_get("@mobile_exempt_actions").try(:include?, params[:action].to_sym)
135 154
       end
6  lib/mobile-fu/helper.rb
@@ -26,16 +26,16 @@ def stylesheet_link_tag_with_mobilization(*sources)
26 26
       
27 27
       sources.each do |source|
28 28
         mobilized_sources << source
29  
-
  29
+        
30 30
         device_names.compact.each do |device_name|
31 31
           # support ERB and/or SCSS extensions (e.g., mobile.css.erb, mobile.css.scss.erb)
32 32
           possible_source = source.to_s.sub(/\.css.*$/, '') + "_#{device_name}"
33  
-
  33
+          
34 34
           mobilized_files = Dir.glob(File.join(stylesheets_dir, "#{possible_source}.css*")).map { |f| f.sub(stylesheets_dir, '') }
35 35
           mobilized_sources += mobilized_files.map { |f| f.sub(/\.css.*/, '') }
36 36
         end
37 37
       end
38  
-
  38
+      
39 39
       stylesheet_link_tag_without_mobilization *mobilized_sources
40 40
     end
41 41
   end

0 notes on commit 8b9d28c

Please sign in to comment.
Something went wrong with that request. Please try again.