Skip to content

kulte/various

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

About

Simple ActionPack::Variant configuration for Rails 4.1

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages