From 5808e9003760227b595444a5eb688d76954540e4 Mon Sep 17 00:00:00 2001 From: Refael Dakar Date: Sun, 11 Dec 2011 16:21:16 +0200 Subject: [PATCH 1/4] you can now select formats that will not be mobilized. useful for cases where you have a mobile phone that should get a json return type instead of a mobile website. also, fixed a bug in "set_mobile_format" - setting request.format instead of :html on line 88 --- README.rdoc | 20 ++++++++++++++++++-- lib/mobile_fu.rb | 25 ++++++++++++++++++++----- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/README.rdoc b/README.rdoc index 36ae110..b11742b 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1,5 +1,11 @@ = Mobile Fu + +This fork adds the ability to ignore curtain request formats from being mobilized. You might want to return JSON for :json or :iphone mime types and not let mobile_fu convert your format to :mobile. + +Usage example at the end of this file. + + 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 @@ -86,13 +92,23 @@ http://blogs.pathf.com/agileajax/2008/05/rails-developme.html Hopefully this should help you create some awesome mobile applications. +== Ignoring specific request formats + +If you want to ignore specific request formats you can just pass ':ignore_formats => []' to has_mobile_fu. + +example: + class ApplicationController < ActionController::Base + has_mobile_fu :ignore_formats => [:iphone, :json] + end + == Testing Mobile Interface If you want to force the mobile interface for testing, you can either use a -mobile device emulator, or you can pass 'true' to has_mobile_fu. +mobile device emulator, or you can pass ':test_mode => true' to has_mobile_fu. +example: class ApplicationController < ActionController::Base - has_mobile_fu(true) + has_mobile_fu :test_mode => true end diff --git a/lib/mobile_fu.rb b/lib/mobile_fu.rb index dabd611..5549576 100644 --- a/lib/mobile_fu.rb +++ b/lib/mobile_fu.rb @@ -26,11 +26,16 @@ module ClassMethods # class ApplicationController < ActionController::Base # has_mobile_fu(true) # end - - def has_mobile_fu(test_mode = false) + + def ignored_formats; @@ignored_formats; end + + def has_mobile_fu(options = {}) include ActionController::MobileFu::InstanceMethods - if test_mode + @@ignored_formats = options.has_key?(:ignore_formats) ? options[:ignore_formats] : [] + test_mode = options.has_key?(:test_mode) ? options[:test_mode] : false + + if test_mode before_filter :force_mobile_format else before_filter :set_mobile_format @@ -55,10 +60,18 @@ def is_device?(type) end module InstanceMethods - + + # Check if the current format needs to be ignored + + def should_ignore_format + return self.class.ignored_formats.include?(request.format.to_sym) + end + # Forces the request format to be :mobile def force_mobile_format + return if should_ignore_format + if !request.xhr? request.format = :mobile session[:mobile_view] = true if session[:mobile_view].nil? @@ -69,8 +82,10 @@ def force_mobile_format # the user has opted to use either the 'Standard' view or 'Mobile' view. def set_mobile_format + return if should_ignore_format + if is_mobile_device? && !request.xhr? - request.format = session[:mobile_view] == false ? :html : :mobile + request.format = session[:mobile_view] == false ? request.format : :mobile session[:mobile_view] = true if session[:mobile_view].nil? end end From 0c0563dcfa9e92092e8a527066a80ce7fbb7367e Mon Sep 17 00:00:00 2001 From: Refael Dakar Date: Tue, 24 Jan 2012 22:58:10 +0200 Subject: [PATCH 2/4] changed has_mobile_fu and README to support backward compatibility --- lib/mobile_fu.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/mobile_fu.rb b/lib/mobile_fu.rb index 5549576..761f909 100644 --- a/lib/mobile_fu.rb +++ b/lib/mobile_fu.rb @@ -29,11 +29,10 @@ module ClassMethods def ignored_formats; @@ignored_formats; end - def has_mobile_fu(options = {}) + def has_mobile_fu(test_mode = false, *args) include ActionController::MobileFu::InstanceMethods - @@ignored_formats = options.has_key?(:ignore_formats) ? options[:ignore_formats] : [] - test_mode = options.has_key?(:test_mode) ? options[:test_mode] : false + @@ignored_formats = args.has_key?(:ignore_formats) ? args[:ignore_formats] : [] if test_mode before_filter :force_mobile_format @@ -116,4 +115,4 @@ def is_device?(type) end -ActionController::Base.send(:include, ActionController::MobileFu) \ No newline at end of file +ActionController::Base.send(:include, ActionController::MobileFu) From c01def7752cfbc809a6504adf00c7aa82c4e9aa2 Mon Sep 17 00:00:00 2001 From: Refael Dakar Date: Tue, 24 Jan 2012 23:17:51 +0200 Subject: [PATCH 3/4] changed has_mobile_fu and README to support backward compatibility --- lib/mobile_fu.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mobile_fu.rb b/lib/mobile_fu.rb index 761f909..06d55d3 100644 --- a/lib/mobile_fu.rb +++ b/lib/mobile_fu.rb @@ -29,10 +29,10 @@ module ClassMethods def ignored_formats; @@ignored_formats; end - def has_mobile_fu(test_mode = false, *args) + def has_mobile_fu(test_mode = false, options) include ActionController::MobileFu::InstanceMethods - @@ignored_formats = args.has_key?(:ignore_formats) ? args[:ignore_formats] : [] + @@ignored_formats = options.has_key?(:ignore_formats) ? options[:ignore_formats] : [] if test_mode before_filter :force_mobile_format From 2e93fdc868167cdde933de42284939117aa8d216 Mon Sep 17 00:00:00 2001 From: Refael Dakar Date: Tue, 24 Jan 2012 23:21:27 +0200 Subject: [PATCH 4/4] changed has_mobile_fu and README to support backward compatibility --- README.rdoc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.rdoc b/README.rdoc index b11742b..2c2e167 100644 --- a/README.rdoc +++ b/README.rdoc @@ -94,21 +94,22 @@ Hopefully this should help you create some awesome mobile applications. == Ignoring specific request formats -If you want to ignore specific request formats you can just pass ':ignore_formats => []' to has_mobile_fu. +If you want to ignore specific request formats you can just pass ':ignore_formats => []' as second argument to has_mobile_fu. +NOTE: If you want to pass :ignore_formats you'll have to pass the 1st argument (test_mode). If you don't know what to put in just pass "false". example: class ApplicationController < ActionController::Base - has_mobile_fu :ignore_formats => [:iphone, :json] + has_mobile_fu false, :ignore_formats => [:iphone, :json] end == Testing Mobile Interface If you want to force the mobile interface for testing, you can either use a -mobile device emulator, or you can pass ':test_mode => true' to has_mobile_fu. +mobile device emulator, or you can pass 'true' as first argument to has_mobile_fu. example: class ApplicationController < ActionController::Base - has_mobile_fu :test_mode => true + has_mobile_fu true end