diff --git a/README.rdoc b/README.rdoc index 36ae110..2c2e167 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,24 @@ 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 => []' 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 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 '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(true) + has_mobile_fu true end diff --git a/lib/mobile_fu.rb b/lib/mobile_fu.rb index dabd611..06d55d3 100644 --- a/lib/mobile_fu.rb +++ b/lib/mobile_fu.rb @@ -26,11 +26,15 @@ 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(test_mode = false, options) include ActionController::MobileFu::InstanceMethods - if test_mode + @@ignored_formats = options.has_key?(:ignore_formats) ? options[:ignore_formats] : [] + + if test_mode before_filter :force_mobile_format else before_filter :set_mobile_format @@ -55,10 +59,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 +81,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 @@ -101,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)