Theme For Rails (3 and hopefully later)
Features list / Wish list
Add themes_for_rails to your Gemfile.
Add themes_for_rails to your config/routes.rb
MySuperDuperApp::Application.routes.draw do # ... themes_for_rails # ... end
Now you’ll be able to use themes like this:
Inside method, for some explicit action:
class MyController < ApplicationController def show theme "purple" end end
Or at class level definition, in order to set a theme for more than one action. I think this is is prettier, and less invasive.
class MyController < ApplicationController theme "purple" # all actions will use this theme def show ... end end
You could also enable a theme for some actions only
class MyController < ApplicationController theme "purple", :only => :show def show # with theme end def edit # no theme end end
As a plus, you could do this to defer theme name resolution to a method:
class MyController < ApplicationController theme :theme_resolver # ... private def theme_resolver current_user.theme # or anything else that return a string. end end
As a general rule, when passing a String, that becomes the theme name, but when a Symbol is sent, it gets treated as method message.
In your views you should be able to access your assets like this (given the theme ‘default’ is set):
Or a given theme:
current_theme_image_path('logo.png', 'purple') # => /themes/purple/images/logo.png
In your application views, there are theme specific helper tags
available to you. For ERb templates they are:
For now, it only creates the theme folder.
rails generate theme_for_rails:install
Inside the themes folder, it create a structure for my_theme.
rails generate theme_for_rails:theme my_theme
At least for now, you can change the ThemesForRails base dir in your app, in the corresponding environment file, or in your application.rb file. Do it like this:
KillerApp::Application.configure do # config.themes_for_rails.base_dir = File.join(Rails.root, "skins") #... end
Notes and Warnings.
If you are running an app in production mode, and you get the static files with no content, is because you don’t have X-senfile enabled at your web server.
You can do two things:
comment out this line in your production.rb file:
config.action_dispatch.x_sendfile_header = “X-Sendfile”
configure your web server to use it. :)
- Add ThemesForRails::Railtie for configuration, so we selectively set the plugin on or off. Also to be able to change several settings.
Add routes to allow access to the theme’s static resources (js and cs), unless cached on public folder by capistrano / rake. Extend Action View path in order to make the views accessible. Same for the layouts.
- More tests ford edge cases. Now I am only testing the happy paths.
Things to remember.
Final version should be a gem. Initialization hooks doesn’t work when using this as a plugin (vendor/plugins).
Research about testing this kind of gem. I really don’t have a clue.Testing in place!
- I should probably load the theme list at start time, to be able to consult it as needed. I am gonna need that when dealing with runtime theme selection. Many themes are going to be used simultaneously, so I have to be able to switch view paths as fast as I can.
Rails 2 Support
This gem only works with Rails 3 (duh). If you want the same exactly behavior, but for Rails 2.x, go here .
gem install bundler bundle install rake
Authors and contributors