Project

various

0.02
No commit activity in last 3 years
No release in over 3 years
Simple ActionPack::Variant configuration for Rails 4.1
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
>= 0
>= 0

Runtime

 Project Readme

Gem Version Build Status

Various

Simple ActionPack::Variant configuration for Rails 4.1.

Rails 4.1 introduces to Action Pack Variants which make it dramatically easier to tailor your views to serve the most relevant content for specific device categories.

According to the Rails documentation, formats can have different variants, settable in a before_action:

request.variant = :tablet if request.user_agent =~ /iPad/

You respond to variants just as you would to formats:

respond_to do |format|
  format.html               # /app/views/:controller/:action.html.erb
  format.html.tablet        # /app/views/:controller/:action.html+tablet.erb
end

Installation

Add this line to your application's Gemfile:

gem 'various'

And then execute:

$ bundle

Or install it yourself as:

$ gem install various

Usage

Various allows you to easily configure a mapping of user agent regular expressions which will set the request.variant automagically based on whether or not there was a match from request.user_agent.

All you have to do in your controller is the following:

class ApplicationController < ActionController::Base
  include Various::Controller
end

And in an initializer, say /config/initializers/various.rb:

Various.configure do |config|
  config.variant_map = {
    /iPad/   => :tablet,
    /iPhone/ => :mobile
  }
end

But these are the defaults, for now, so this would be unnecessary unless you would want to change the hash map to something other than the above.

It is really that simple!

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request