Project

mobu

0.01
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Rails User Agent Dependent View Paths
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
~> 0.14
~> 10.1
~> 2.5

Runtime

~> 1.4
 Project Readme

Mobu

Gem Version Build Status Code Climate Coverage Status

Mobu provides a Rails controller concern called DetectMobile. Mobu does server-side User Agent detection to categorize requests as mobile, tablet, or default.

Mobu modifies your rails view paths based on the request type. It does not require custom MIME types or separate subdomains.

Disclaimer: It is rarely a good idea to use User Agent detection to render different HTML content. Please read this first:

https://developer.mozilla.org/en-US/docs/Browser_detection_using_the_user_agent

Install

Add this line to your Gemfile:

gem 'mobu'

Include the module in your ApplicationController:

class ApplicationController
  include Mobu::DetectMobile

Create directories for views_mobile and views_tablet:

mkdir app/views_mobile
mkdir app/views_tablet

Usage

Put the view/partial files that you want to override in the appropriate directories.

When you receive a mobile request, your app will first look for view files in app/views_mobile directory, then in app/views.

Alternately, you can switch your rendering logic using the mobile_request? and tablet_request? helper methods in your view files or helpers:

- if mobile_request?
  .small-thing Short Text
- else
  .regular-thing Much Longer Text

To allow mobile users to switch to the full site view, add a link to a mobile view. For example:

app/views_mobile/_footer.haml
= link_to("View Full Site", prefer_full_site_url)

To allow full site users to switch to the mobile view, add a link to a default view. For example:

app/views_mobile/_footer.haml
- if mobile_browser?
  = link_to("View Mobile Site", prefer_mobile_site_url)

Credits

The view path modification technique was taken from this post by Scott W. Bradley.

The user agent regex came from a similar project, Brendan Lim's mobile-fu.