• Stars
    star
    218
  • Rank 181,805 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Adds multi themes support to your Rails 3/4/5 application

ThemesOnRails Build Status Dependency Status Code Climate Coverage Status Gem Version

Installation

The simplest way to install is to use Bundler.

Add this gem to your Gemfile:

gem 'themes_on_rails'

If you want to use themes_on_rails with liquid template, add one more gem to your Gemfile:

gem 'liquid-rails'

Then, use Bundler to install the gem and its dependencies:

$ bundle install

Usage

A theme is composed of three things:

  1. Assets: images, javascripts, stylesheets
  2. Views: templates and layouts (erb, haml, or other template engines)
  3. Locales: locales files if any

Generator

To generate theme inside your app:

$ rails g themes_on_rails:theme theme_name
app/
  themes/
    [theme_name]/
      assets/
        images/
          [theme_name]/
        stylesheets/
          [theme_name]/
            all.css
        javascripts/
          [theme_name]/
            all.js
      views/
        layouts/
          [theme_name].html.erb
      locales/

After you invoke the above command, make sure you restart your rails process.

It's best advisable to namespace your assets directory so that it won't conflict with assets in other themes.

image_tag              'theme_a/logo.png' # => app/themes/theme_a/assets/images/theme_a/logo.png
javascript_include_tag 'theme_a/all'      # => app/themes/theme_a/assets/javascripts/theme_a/all.js
stylesheet_link_tag    'theme_a/all'      # => app/themes/theme_a/assets/stylesheets/theme_a/all.css

There is an example app at https://github.com/chamnap/themes_on_rails_example.

Controller

You can set theme in your controllers by using the theme declaration. For example:

class HomeController < ApplicationController
  theme 'basic'

  def index
    ...
  end
end

With this declaration, all of the views rendered by the home controller will use app/themes/basic/views/home/index.html.erb as its templates and use app/themes/basic/views/layouts/basic.html.erb.

You can use a symbol to defer the choice of theme until a request is processed:

class HomeController < ApplicationController
  theme :theme_resolver

  def index
    ...
  end

  private

    def theme_resolver
      params[:theme].presence || 'professional'
    end
end

Now, if there is a params[:theme], it will use that theme. Otherwise, it will use professional theme.

You can even use an inline method, such as a Proc, to determine the theme. For example, if you pass a Proc object, the block you give the Proc will be given the controller instance, so the theme can be determined based on the current request:

class HomeController < ApplicationController
  theme Proc.new { |controller| controller.params[:theme].presence || 'professional' }
end

Theme specified at the controller level support the :only and :except options. These options take either a method name, or an array of method names, corresponding to method names within the controller:

class HomeController < ApplicationController
  theme 'basic', except: [:rss]
end

With this declaration, the basic theme would be used for everything but the rss index methods.

Authors

More Repositories

1

liquid-rails

Renders liquid templates with layout and partial support
Ruby
116
star
2

tenancy

A simple multitenancy with activerecord/mongoid through scoping
Ruby
22
star
3

spree-multi-vendor

This extension allows a single Spree instance to run multi stores for different vendors (i.e. multi-store, mulit-vendor).
Ruby
17
star
4

smtp_checker

SMTP Email Verification
JavaScript
13
star
5

javascript_style_guide

JavaScript Style Guide
11
star
6

active_record_uuid

A full-featured gem for adding uuid support to your active record models
Ruby
9
star
7

toy_robot

ToyRobot is a simulator of a robot that moves on a square tabletop.
Ruby
8
star
8

draper-extensions

Extends Draper by adding pagination and scoping methods
Ruby
4
star
9

newrelic_report

Ruby
3
star
10

mongoid-socialization

Socialize your app with Likes, Follows, WishLists, Mentions, and Private Messages
Ruby
3
star
11

khmer_unicode_converter

Khmer Unicode Converter
JavaScript
3
star
12

themes_on_rails_example

Example application of themes_on_rails
Ruby
3
star
13

chamnap.github.com

A blog from Chamnap Chhorn
JavaScript
3
star
14

tweetstream_heroku_example

Example of running tweetstream daemon on heroku
Ruby
2
star
15

notes

My personal notes
Ruby
2
star
16

prn2xls

An npm module to convert PRN file into xlsx/pdf file
JavaScript
2
star
17

installer

A collection of installation rake scripts
Shell
1
star
18

html_compressor_middleware

html output compression middleware
Ruby
1
star
19

prn2xls_electron

An electron desktop application with Angular Material + NodeJS.
JavaScript
1
star
20

todo_backbone

An example todo app using backbone+rails
JavaScript
1
star