public
Description: Automatically detect mobile requests from mobile devices in your Rails application.
Homepage: http://www.intridea.com/2008/7/21/mobilize-your-rails-application-with-mobile-fu
Clone URL: git://github.com/brendanlim/mobile-fu.git
Click here to lend your support to: mobile-fu and make a donation at www.pledgie.com !
brendanlim (author)
Sat Dec 20 18:00:04 -0800 2008
commit  086ffa48c23e1d7ef46e7203ae133efa624ac3f7
tree    ae75e0329430525fba66952e0553f5a5f3458d90
parent  a46e0d5ff0a95407bbd8f3437a372ced53d90c9b
name age message
file CHANGELOG Sat Dec 20 18:00:04 -0800 2008 Updating the CHANGELOG [brendanlim]
file MIT-LICENSE Thu Jul 17 21:59:36 -0700 2008 Releasing Mobile Fu to the world [brendanlim]
file README.rdoc Sat Dec 20 17:59:49 -0800 2008 Added a helper method to determine if a device ... [brendanlim]
file Rakefile Thu Jul 17 21:59:36 -0700 2008 Releasing Mobile Fu to the world [brendanlim]
file init.rb Sat Dec 20 17:59:49 -0800 2008 Added a helper method to determine if a device ... [brendanlim]
file install.rb Thu Jul 17 21:59:36 -0700 2008 Releasing Mobile Fu to the world [brendanlim]
directory lib/ Sat Dec 20 17:59:49 -0800 2008 Added a helper method to determine if a device ... [brendanlim]
file uninstall.rb Thu Jul 17 21:59:36 -0700 2008 Releasing Mobile Fu to the world [brendanlim]
README.rdoc

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, Nokia, etc. and it will automatically adjust the format of the request from :html to :mobile.

Usage

Add this this one line to the controller.

    class ApplicationController < ActionController::Base
      has_mobile_fu
    end

Once this is in place, any request that comes from a mobile device will be be 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

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

You can also determine which format is currently set in by calling the following:

  is_mobile_view? # => Returns true or false depending on current req. format

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 in a custom controller action.

  session[:mobile_view] # => Set to true if request format is :mobile and false
                             if set to :html

So, different devices need different styling. Don’t worry, we’ve got this baked in to Mobile Fu.

If you are including a css or sass file via stylesheet_link_tag, all you have to do is add _device to the name of one of your files to override your styling for a certain device. The stylesheet that is loaded is dependant on which device is making the request.

  e.g., Accessing a page from a Blackberry.

  ...  stylesheet_link_tag 'mobile.css' ...

  This loads mobile.css, and mobile_blackberry.css if the file exists.

Supported stylesheet override device extensions at the moment are:

  blackberry
  webkit (android,iphone,ipod)
  mobileexplorer
  nokia
  palm

The stylesheet awesomeness was derived from Michael Bleigh’s browserized styles: www.intridea.com/2007/12/9/announcing-browserized-styles

Inspiration for Mobile Fu came from Noel Rappin’s rails_iui: blogs.pathf.com/agileajax/2008/05/rails-developme.html

Hopefully this should help you create some awesome mobile applications.

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.

  class ApplicationController < ActionController::Base
    has_mobile_fu(true)
  end

Copyright © 2008 Brendan G. Lim, Intridea, Inc., released under the MIT license