Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for ignored_formats #26

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 => [<array of your 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


Expand Down
26 changes: 20 additions & 6 deletions lib/mobile_fu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand All @@ -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
Expand Down Expand Up @@ -101,4 +115,4 @@ def is_device?(type)

end

ActionController::Base.send(:include, ActionController::MobileFu)
ActionController::Base.send(:include, ActionController::MobileFu)