From 8b9d28c63513220a21f94fb5b0cff2cd59a7f5c9 Mon Sep 17 00:00:00 2001 From: Mohd Zaid Date: Sat, 11 Feb 2012 14:18:34 +0800 Subject: [PATCH 1/8] added tablet user agents --- lib/mobile-fu.rb | 25 ++++++++++++++++++++++--- lib/mobile-fu/helper.rb | 6 +++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/mobile-fu.rb b/lib/mobile-fu.rb index c7408b7..3c8e2d8 100644 --- a/lib/mobile-fu.rb +++ b/lib/mobile-fu.rb @@ -25,11 +25,16 @@ class Railtie < Rails::Railtie end Mime::Type.register_alias "text/html", :mobile + Mime::Type.register_alias "text/html", :tablet end end module ActionController module MobileFu + # These are various strings that can be found in tablet devices. Please feel free + # to add on to this list. + TABLET_USER_AGENTS = 'ipad|android 3.0|xoom|sch-i800|playbook|tablet|kindle' + def self.included(base) base.extend ClassMethods end @@ -55,11 +60,13 @@ def has_mobile_fu(set_request_format = true) before_filter :set_request_format if set_request_format helper_method :is_mobile_device? + helper_method :is_tablet_device? helper_method :in_mobile_view? + helper_method :in_tablet_view? helper_method :is_device? helper_method :mobile_device end - + # Add this to your controllers to prevent the mobile format from being set for specific actions # class AwesomeController < ApplicationController # has_no_mobile_fu_for :index @@ -109,12 +116,24 @@ def in_mobile_view? request.format.to_sym == :mobile end + # Returns either true or false depending on whether or not the format of the + # request is either :tablet or not. + + def in_tablet_view? + return false unless request.format + request.format.to_sym == :tablet + end + # Returns either true or false depending on whether or not the user agent of # the device making the request is matched to a device in our regex. def is_mobile_device? !!mobile_device end + + def is_tablet_device? + request.user_agent.to_s.downcase =~ Regexp.new(ActionController::MobileFu::TABLET_USER_AGENTS) + end def mobile_device request.headers['X_MOBILE_DEVICE'] @@ -126,10 +145,10 @@ def mobile_device def is_device?(type) request.user_agent.to_s.downcase.include? type.to_s.downcase end - + # Returns true if current action isn't supposed to use mobile format # See #has_no_mobile_fu_for - + def mobile_exempt? self.class.instance_variable_get("@mobile_exempt_actions").try(:include?, params[:action].to_sym) end diff --git a/lib/mobile-fu/helper.rb b/lib/mobile-fu/helper.rb index e314454..033ff82 100644 --- a/lib/mobile-fu/helper.rb +++ b/lib/mobile-fu/helper.rb @@ -26,16 +26,16 @@ def stylesheet_link_tag_with_mobilization(*sources) sources.each do |source| mobilized_sources << source - + device_names.compact.each do |device_name| # support ERB and/or SCSS extensions (e.g., mobile.css.erb, mobile.css.scss.erb) possible_source = source.to_s.sub(/\.css.*$/, '') + "_#{device_name}" - + mobilized_files = Dir.glob(File.join(stylesheets_dir, "#{possible_source}.css*")).map { |f| f.sub(stylesheets_dir, '') } mobilized_sources += mobilized_files.map { |f| f.sub(/\.css.*/, '') } end end - + stylesheet_link_tag_without_mobilization *mobilized_sources end end From 191f0a2592139fa41f6f1bc29cde54396216402f Mon Sep 17 00:00:00 2001 From: Mohd Zaid Date: Sat, 11 Feb 2012 14:30:01 +0800 Subject: [PATCH 2/8] updated readme --- README.md | 1 + lib/mobile-fu.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e0d0aec..0228701 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ your views that are to be requested. Then add the line below to config/initializers/mime_types.rb Mime::Type.register_alias "text/html", :mobile + Mime::Type.register_alias "text/html", :tablet I recommend that you setup a before_filter that will redirect to a specific page depending on whether or not it is a mobile request. How can you check this? diff --git a/lib/mobile-fu.rb b/lib/mobile-fu.rb index 3c8e2d8..234afda 100644 --- a/lib/mobile-fu.rb +++ b/lib/mobile-fu.rb @@ -130,7 +130,7 @@ def in_tablet_view? def is_mobile_device? !!mobile_device end - + def is_tablet_device? request.user_agent.to_s.downcase =~ Regexp.new(ActionController::MobileFu::TABLET_USER_AGENTS) end From 4a67d298349be2c741992d40d6bcf8aa10a2e42d Mon Sep 17 00:00:00 2001 From: Mohd Zaid Date: Sat, 11 Feb 2012 16:34:55 +0800 Subject: [PATCH 3/8] added tablet view --- README.md | 14 ++++++++++++-- lib/mobile-fu.rb | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0228701..c250d7e 100644 --- a/README.md +++ b/README.md @@ -33,11 +33,15 @@ Then add the line below to config/initializers/mime_types.rb I recommend that you setup a before_filter that will redirect to a specific page depending on whether or not it is a mobile request. How can you check this? - is_mobile_device? # => Returns true or false depending on the device + is_mobile_device? # => Returns true or false depending on the device or + + is_tablet_device? # => Returns true if the device is a tablet You can also determine which format is currently set in by calling the following: - in_mobile_view? # => Returns true or false depending on current req. format + in_mobile_view? # => Returns true or false depending on current req. format or + + in_tablet_view? # => Returns true if the current req. format is for tablet view Also, if you want the ability to allow a user to switch between 'mobile' and 'standard' format (:html), you can just adjust the mobile_view session variable @@ -87,5 +91,11 @@ mobile device emulator, or you can call `force_mobile_format` in a before filter before_filter :force_mobile_format end +You can also force the tablet view by calling `force_tablet_format` instead + + class ApplicationController < ActionController::Base + has_mobile_fu + before_filter :force_tablet_format + end Copyright (c) 2008 Brendan G. Lim, Intridea, Inc., released under the MIT license diff --git a/lib/mobile-fu.rb b/lib/mobile-fu.rb index 234afda..b25b89a 100644 --- a/lib/mobile-fu.rb +++ b/lib/mobile-fu.rb @@ -97,6 +97,14 @@ def force_mobile_format session[:mobile_view] = true if session[:mobile_view].nil? end end + + # Forces the request format to be :tablet + def force_tablet_format + unless request.xhr? + request.format = :tablet + session[:tablet_view] = true if session[:tablet_view].nil? + end + end # Determines the request format based on whether the device is mobile or if # the user has opted to use either the 'Standard' view or 'Mobile' view. @@ -108,6 +116,16 @@ def set_mobile_format end end + # Determines the request format based on whether the device is tablet or if + # the user has opted to use either the 'Standard' view or 'Tablet' view. + + def set_tablet_format + if !mobile_exempt? && is_tablet_device? && !request.xhr? + request.format = session[:tablet_view] == false ? :html : :tablet + session[:tablet_view] = true if session[:tablet_view].nil? + end + end + # Returns either true or false depending on whether or not the format of the # request is either :mobile or not. From 5415490e313224a078f625c3976a5e7d24b849b7 Mon Sep 17 00:00:00 2001 From: Mohd Zaid Date: Sat, 11 Feb 2012 17:01:52 +0800 Subject: [PATCH 4/8] updated readme --- README.md | 5 ++++- lib/mobile-fu.rb | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c250d7e..acbbe39 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Mobile Fu Want to automatically detect mobile devices that access your Rails application? Mobile Fu allows you to do just that. People can access your site from a Palm, Blackberry, iPhone, iPad, Nokia, etc. and it will automatically adjust the format -of the request from :html to :mobile. +of the request from :html to :mobile or :tablet. Installation ------------ @@ -50,6 +50,9 @@ in a custom controller action. session[:mobile_view] # => Set to true if request format is :mobile and false if set to :html + session[:tablet_view] # => Set to true if request format is :tablet and false + if set to :html + So, different devices need different styling. Don't worry, we've got this baked in to Mobile Fu. diff --git a/lib/mobile-fu.rb b/lib/mobile-fu.rb index b25b89a..0849c57 100644 --- a/lib/mobile-fu.rb +++ b/lib/mobile-fu.rb @@ -97,7 +97,7 @@ def force_mobile_format session[:mobile_view] = true if session[:mobile_view].nil? end end - + # Forces the request format to be :tablet def force_tablet_format unless request.xhr? From b8079779b5e85dae2d259516231d863dea90d663 Mon Sep 17 00:00:00 2001 From: Mohd Zaid Date: Sat, 11 Feb 2012 19:50:01 +0800 Subject: [PATCH 5/8] merge set mobile format --- .DS_Store | Bin 0 -> 6148 bytes lib/.DS_Store | Bin 0 -> 6148 bytes lib/mobile-fu.rb | 21 ++++++++++++--------- lib/mobile-fu/.DS_Store | Bin 0 -> 6148 bytes 4 files changed, 12 insertions(+), 9 deletions(-) create mode 100644 .DS_Store create mode 100644 lib/.DS_Store create mode 100644 lib/mobile-fu/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..9524ba092aaa5b8496ea6b2442aafc3acfe03556 GIT binary patch literal 6148 zcmeHK%Wl&^6g|@fYEwXAfz%xq>rkaFs35TjAyylSmq?^82m-rK8w(41?0Qy;x+d03h$76f7637NkfjxrJ+lIG*`w5SS7QE zV;mwzf?g}hv&H>O>=HRK!b_}k$)bz-oGoBj;bb^4Gye_;Njx0YYJbE^seJ49s^hxO z-R^#T5~Y(yIzEYppW6L&(vG8E7@U4q@hBLLhmGJ=jeH13eU7NOlcovdu3_)T(Ww8W zvOb+|Zau4dTQ9a}Rd2epQ>%JUx1Y~uuH(3sU(UnF8!z`d!P)Hlk4%*b!KF^wJ%bnU zlffF^qJ<8ga2ae-R-dA_;TLaR$$6HO^8uwy5o$4+dx$YMaHJ$2Vn947UO`Emzk-90 z2=I-}s~^cZYs>Rp@_jz?ChPS%*D<2v464Szxf;v3zLGreLJf;cuYma@J8Mf{sV&ed zU=_G&1?2kR(IwUmW*XJf!A2bch-G#wV=O-yWR7RBZZOlx5t`7cM4c+k5ku&7_Irj` zH<)SE=@4pWUdPNV%n3!P+1c+YIz(NgZLI=UfuaH>{j(zXfB*XPe=*7avIAwDQTbwVO*$h1si;*OCv=I7AR4$WqfemivCSc z{hR)d`*wC6TM{c}Zk6j4HJ-!Hq2(k#NL*&0LWAyh4wuv;sl+2qbu{EF_< zWRf=;zg20k{OtJ)&-cB`aFE=_*{q#SZ{x|Q-YA>(lK3hL$Di5H!+bhvhhrZ380I64 zu;0(J6#md<50W?^eW@PI=gpHhHGOj4TGaIX?5t7Kr>(b(h3|QO^{4mxsQ&IlKm5A* z_PyXGlfo8ddFgNkKj2h0DWE!yDe9){M5tHDJzTP^$_0f1VL%wzJqFx?=$LH1uD5QyrRlXAdI2?J@(PRhC2+VEBiFRz11JmP+Bm?#VPO#wi2MjJ4Pu0WCuQId D{J~l( literal 0 HcmV?d00001 diff --git a/lib/mobile-fu.rb b/lib/mobile-fu.rb index 0849c57..6d25917 100644 --- a/lib/mobile-fu.rb +++ b/lib/mobile-fu.rb @@ -31,6 +31,15 @@ class Railtie < Rails::Railtie module ActionController module MobileFu + # These are various strings that can be found in mobile devices. Please feel free + # to add on to this list. + MOBILE_USER_AGENTS = 'palm|blackberry|nokia|phone|midp|mobi|symbian|chtml|ericsson|minimo|' + + 'audiovox|motorola|samsung|telit|upg1|windows ce|ucweb|astel|plucker|' + + 'x320|x240|j2me|sgh|portable|sprint|docomo|kddi|softbank|android|mmp|' + + 'pdxgw|netfront|xiino|vodafone|portalmmm|sagem|mot-|sie-|ipod|up\\.b|' + + 'webos|amoi|novarra|cdm|alcatel|pocket|ipad|iphone|mobileexplorer|' + + 'mobile' + # These are various strings that can be found in tablet devices. Please feel free # to add on to this list. TABLET_USER_AGENTS = 'ipad|android 3.0|xoom|sch-i800|playbook|tablet|kindle' @@ -107,20 +116,14 @@ def force_tablet_format end # Determines the request format based on whether the device is mobile or if - # the user has opted to use either the 'Standard' view or 'Mobile' view. + # the user has opted to use either the 'Standard' view or 'Mobile' view or + # 'Tablet' view. def set_mobile_format if !mobile_exempt? && is_mobile_device? && !request.xhr? request.format = session[:mobile_view] == false ? :html : :mobile session[:mobile_view] = true if session[:mobile_view].nil? - end - end - - # Determines the request format based on whether the device is tablet or if - # the user has opted to use either the 'Standard' view or 'Tablet' view. - - def set_tablet_format - if !mobile_exempt? && is_tablet_device? && !request.xhr? + elsif !mobile_exempt? && is_tablet_device? && !request.xhr? request.format = session[:tablet_view] == false ? :html : :tablet session[:tablet_view] = true if session[:tablet_view].nil? end diff --git a/lib/mobile-fu/.DS_Store b/lib/mobile-fu/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..99e4211c90bcebd95aa9b9db3f3a423fc65e9df4 GIT binary patch literal 6148 zcmeHKyH3ME5S)b+0i-A;9rp!D{J|*-MQU0KWQYWkg`*&--SOEl`$!aHXenS<+MT<- z9iKgg_YQz8ULUT2Ie;l$5$_ttrt9h>JBo~=*fZAH;*#F1+EMlQ3FV&P5o@&AFz)%? zebaUgSM}9y=@%bn908|zv)UItp~LfT&qI#PzHp3~Ar(jkQh`(;75MiGuxG1H&m1$R z0;xbMuvb9;4~4E+1A9mNbuj4hUDqdyY8=~Lf>>f+1A9lV(8Q@kr%GHg#OchJsH=g! zqthX_GoKSXOWaVzc4xj=IixyfOa)Sbp#sM~oooNUrvGsNAChvG3Zw#mN&%THZWYq8R|9)Tqcd-GVjcukm$X#iHx&2;e+DQ8 literal 0 HcmV?d00001 From 86e8f086f5062ada2af17fdab33d174d71b708f3 Mon Sep 17 00:00:00 2001 From: Mohd Zaid Date: Sat, 11 Feb 2012 19:53:12 +0800 Subject: [PATCH 6/8] removed ds store --- .DS_Store | Bin 6148 -> 0 bytes lib/.DS_Store | Bin 6148 -> 0 bytes lib/mobile-fu/.DS_Store | Bin 6148 -> 0 bytes 3 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store delete mode 100644 lib/.DS_Store delete mode 100644 lib/mobile-fu/.DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 9524ba092aaa5b8496ea6b2442aafc3acfe03556..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%Wl&^6g|@fYEwXAfz%xq>rkaFs35TjAyylSmq?^82m-rK8w(41?0Qy;x+d03h$76f7637NkfjxrJ+lIG*`w5SS7QE zV;mwzf?g}hv&H>O>=HRK!b_}k$)bz-oGoBj;bb^4Gye_;Njx0YYJbE^seJ49s^hxO z-R^#T5~Y(yIzEYppW6L&(vG8E7@U4q@hBLLhmGJ=jeH13eU7NOlcovdu3_)T(Ww8W zvOb+|Zau4dTQ9a}Rd2epQ>%JUx1Y~uuH(3sU(UnF8!z`d!P)Hlk4%*b!KF^wJ%bnU zlffF^qJ<8ga2ae-R-dA_;TLaR$$6HO^8uwy5o$4+dx$YMaHJ$2Vn947UO`Emzk-90 z2=I-}s~^cZYs>Rp@_jz?ChPS%*D<2v464Szxf;v3zLGreLJf;cuYma@J8Mf{sV&ed zU=_G&1?2kR(IwUmW*XJf!A2bch-G#wV=O-yWR7RBZZOlx5t`7cM4c+k5ku&7_Irj` zH<)SE=@4pWUdPNV%n3!P+1c+YIz(NgZLI=UfuaH>{j(zXfB*XPe=*7avIAwDQTbwVO*$h1si;*OCv=I7AR4$WqfemivCSc z{hR)d`*wC6TM{c}Zk6j4HJ-!Hq2(k#NL*&0LWAyh4wuv;sl+2qbu{EF_< zWRf=;zg20k{OtJ)&-cB`aFE=_*{q#SZ{x|Q-YA>(lK3hL$Di5H!+bhvhhrZ380I64 zu;0(J6#md<50W?^eW@PI=gpHhHGOj4TGaIX?5t7Kr>(b(h3|QO^{4mxsQ&IlKm5A* z_PyXGlfo8ddFgNkKj2h0DWE!yDe9){M5tHDJzTP^$_0f1VL%wzJqFx?=$LH1uD5QyrRlXAdI2?J@(PRhC2+VEBiFRz11JmP+Bm?#VPO#wi2MjJ4Pu0WCuQId D{J~l( diff --git a/lib/mobile-fu/.DS_Store b/lib/mobile-fu/.DS_Store deleted file mode 100644 index 99e4211c90bcebd95aa9b9db3f3a423fc65e9df4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKyH3ME5S)b+0i-A;9rp!D{J|*-MQU0KWQYWkg`*&--SOEl`$!aHXenS<+MT<- z9iKgg_YQz8ULUT2Ie;l$5$_ttrt9h>JBo~=*fZAH;*#F1+EMlQ3FV&P5o@&AFz)%? zebaUgSM}9y=@%bn908|zv)UItp~LfT&qI#PzHp3~Ar(jkQh`(;75MiGuxG1H&m1$R z0;xbMuvb9;4~4E+1A9mNbuj4hUDqdyY8=~Lf>>f+1A9lV(8Q@kr%GHg#OchJsH=g! zqthX_GoKSXOWaVzc4xj=IixyfOa)Sbp#sM~oooNUrvGsNAChvG3Zw#mN&%THZWYq8R|9)Tqcd-GVjcukm$X#iHx&2;e+DQ8 From 411a492d2bd74a008e65b3f921c0b9eb5f3d5cc9 Mon Sep 17 00:00:00 2001 From: Mohd Zaid Date: Sat, 11 Feb 2012 19:59:21 +0800 Subject: [PATCH 7/8] added osx noise to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 51538ad..b8dec80 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ Gemfile.lock pkg/* vendor/ruby +*.DS_Store \ No newline at end of file From 6e91e94888aa6f81b00b181757fc7e224503e23e Mon Sep 17 00:00:00 2001 From: Mohd Zaid Date: Wed, 15 Feb 2012 07:22:57 +0800 Subject: [PATCH 8/8] fixed #9 #11 removed mobile user agent and fixed readme --- README.md | 7 +++---- lib/mobile-fu.rb | 11 +---------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index acbbe39..eb73d8f 100644 --- a/README.md +++ b/README.md @@ -25,10 +25,9 @@ set as :mobile format. It is up to you to determine how you want to handle these requests. It is also up to you to create the .mobile.erb versions of your views that are to be requested. -Then add the line below to config/initializers/mime_types.rb - - Mime::Type.register_alias "text/html", :mobile - Mime::Type.register_alias "text/html", :tablet +Mobile Fu automatically adds a new `:mobile` and `:tablet` to `text/html` mime type +alias for Rails apps. If you already have a custom `:mobile` alias registered in +`config/initializers/mime_types.rb`, you can remove that. I recommend that you setup a before_filter that will redirect to a specific page depending on whether or not it is a mobile request. How can you check this? diff --git a/lib/mobile-fu.rb b/lib/mobile-fu.rb index 6d25917..d96a09a 100644 --- a/lib/mobile-fu.rb +++ b/lib/mobile-fu.rb @@ -31,18 +31,9 @@ class Railtie < Rails::Railtie module ActionController module MobileFu - # These are various strings that can be found in mobile devices. Please feel free - # to add on to this list. - MOBILE_USER_AGENTS = 'palm|blackberry|nokia|phone|midp|mobi|symbian|chtml|ericsson|minimo|' + - 'audiovox|motorola|samsung|telit|upg1|windows ce|ucweb|astel|plucker|' + - 'x320|x240|j2me|sgh|portable|sprint|docomo|kddi|softbank|android|mmp|' + - 'pdxgw|netfront|xiino|vodafone|portalmmm|sagem|mot-|sie-|ipod|up\\.b|' + - 'webos|amoi|novarra|cdm|alcatel|pocket|ipad|iphone|mobileexplorer|' + - 'mobile' - # These are various strings that can be found in tablet devices. Please feel free # to add on to this list. - TABLET_USER_AGENTS = 'ipad|android 3.0|xoom|sch-i800|playbook|tablet|kindle' + TABLET_USER_AGENTS = 'ipad|android 3.0|xoom|sch-i800|playbook|tablet|kindle|honeycomb' def self.included(base) base.extend ClassMethods