Skip to content

↩️ Consistent fallback to the default layout and partials for Action View

License

Notifications You must be signed in to change notification settings

yasaichi/actionview-consistent_fallback

Repository files navigation

actionview-consistent_fallback

Gem Version Build Status Code Climate Test Coverage

Motivation

Let's say you're making your Rails application mobile-friendly with Action Pack Variants.
The application has the following directory structure:

app/views
├── layouts
│   ├── application.html+mobile.erb
│   └── application.html.erb
└── projects
    ├── index.html+mobile.erb
    ├── index.html.erb
    └── new.html.erb

In this case, when you set request.variant to :mobile, Rails will render templates as follows:

  • projects#index: index.html+mobile.erb with application.html+mobile.erb
  • projects#new: new.html.erb with application.html+mobile.erb

The second behaviour means that you have to prepare mobile versions of every template right away.
However, you couldn't do that when you have so many templates in your application.

Solution

actionview-consistent_fallback is a small, but usuful plugin for Action View.
The plugin allows you to fallback to the default layout and partials when there is no variant template corresponding to each request variant.

In the above case, new.html.erb is rendered with application.html.erb for mobile variant.
By the consistent fallback, you can gradually roll out a mobile-version page.

Installation

Add this line to your application's Gemfile:

gem 'actionview-consistent_fallback'

And then execute:

$ bundle

Or install it yourself as:

$ gem install actionview-consistent_fallback

Usage

All you need to do is to load the gem, and then create as many variant templates as you can now :)

Contributing

You should follow the steps below.

  1. Fork the repository
  2. Create a feature branch: git checkout -b add-new-feature
  3. Commit your changes: git commit -am 'Add new feature'
  4. Push the branch: git push origin add-new-feature
  5. Send us a pull request

License

The gem is available as open source under the terms of the MIT License.

About

↩️ Consistent fallback to the default layout and partials for Action View

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published