Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Mobile request handling for Ruby on Rails
tag: v0.0.2

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
config/initializers
lib
spec
.gitignore
.rvmrc
.travis.yml
CHANGELOG.rdoc
Gemfile
Gemfile.lock
MIT-LICENSE
README.rdoc
Rakefile
TODO.rdoc
mobylette.gemspec

README.rdoc

Not ready yet!!!

Mobylette

This gem works by adding the 'mobile' format to your rails application. Whenever a request come from a mobile device, if you have your controller mobile enabled, it shall render the view.mobile.erb instead of the view.html.erb (or haml, or whatever).

This gem is based on the mobile_fu plugin (github.com/brendanlim/mobile-fu). Also these pages were very usefull for me:

* http://stackoverflow.com/questions/5126085/ruby-on-rails-mobile-application
* http://www.arctickiwi.com/blog/mobile-enable-your-ruby-on-rails-site-for-small-screens
* http://metautonomo.us/2011/01/05/mobile-devices-and-rails-maintaining-your-sanity/

How does it work?

By adding “respond_to_mobile_requests” in your application_controller (or any other controller), your controllers (or that controller) will understand mobile requests as a new mime type alias “mobile”. This will make the controller to search for the .mobile.erb file instead of the .html.erb. Also you will be able to do:

respond_to do |format|
  format.html   { ... }
  format.mobile { ... }
end

Instalation

Add the gem to your gemfile:

gem 'mobylette', :git => "git://github.com/tscolari/mobylette.git"

And add to your ApplicationController.rb (for enabling it to all your controllers) or to the controllers you want this functionality on:

respond_to_mobile_requests

After that, you may start adding your .mobile. views.

Helpers

is_mobile_request?

This helper returns true if the request comes from a mobile device, false if it does not.

mobylette_stylesheet_link_tag(*sources)

This works like the stylesheet_link_tag helper, but when the request comes from a mobile device, it adds “_mobile” to the stylesheets before calling stylesheet_link_tag

mobylette_javascript_include_tag(*soruces)

Same as mobylette_stylesheet_link_tag, but for javascript files and javascript_include_tag

Fall Backs - not implemented yet

Fall back still a problem. In my POV .mobile should fall back to .html, it's a desirable behavior. But it's not working this way. In this post - metautonomo.us/2011/01/05/mobile-devices-and-rails-maintaining-your-sanity/ - it is an idea that will work (not with this gem), but that could be considered. I still think that it would be a lot better making the .mobile fall back into .html. But as I haven't tryed it yet, I've no more to say, yet.

Testing

Don't drive your mobylette without your Helmet! It's always safer do tests!

For testing, include the Mobylette::Helmet module to your test/test_helpers.rb:

include Mobylette::Helmet

For RSpec: add to your spec/spec_helpers.rb or create a spec/support/mobylette.rb with the following:

RSpec.configure do |config|
  config.include Mobylette::Helmet, :type => :controller
end

This will add 2 methods to your test scope:

force_mobile_request_agent(device = "Android")

this one will force your user_agent to the one specified, allowing you to test mobile requests.

reset_test_request_agent

and this one will reset your user_agent to the test default “Rails Testing”. You don't need to call this everytime, all your requests by default are “Rails Testing” in your test env.

Friendly note: on your tests, call these functions BEFORE you make the request, otherwise they are useless =p

Something went wrong with that request. Please try again.