Theme Support in Rails and a wild community!
It's amazing the stuff you find in the Rails community.
I was thinking: "It would be nice to add the ability for themes in my Helpdesk app, so the user can suit the app to their needs..."
My first thought was: "well, I'm on my own". Finding Matt Mccray's Theme Support Plugin was amazing!
Not only it gives me what I wanted, but it's install is very easy and to use it all you have to do is call:
script/generate theme <<themename>>
Then in your controllers place a call to:
theme "<<themename>>"
If you'd like to define a method that defines the theme name just use:
theme :Method_Name
Well, since I'm dealing with open source projects I'm familiar with the concept of having to do some tweaks on the software to get it working.
I learned upon install that the plug-in is only tested against version 1.1 of Rails. As I'm using 1.2.3 I thought "Oh, Oh...". But don't despair, I endured and I got it to work.
First problem is with the modification to the routing system that Theme Support performs. You can find the routeset_ex.rb file in \vendor\plugins\theme_support\lib\patches. Just change it to read like this:
# Extends ActionController::Routing::RouteSet to automatically add the theme routes
class ActionController::Routing::RouteSet
alias_method :__draw, :draw
# Overrides the default RouteSet#draw to automatically
# include the routes needed by the ThemeController
def draw
begin
clear!
mapper = Mapper.new(self)
create_theme_routes(mapper)
yield mapper
named_routes.install
rescue
raise
end
end
# Creates the required routes for the ThemeController...
def create_theme_routes(map)
# Added patch from D.J. Vogel that changes :filename to *filename... allowing sub-folders
map.named_route 'theme_images', "/themes/:theme/images/*filename", :controller=>'theme', :action=>'images'
map.named_route 'theme_stylesheets', "/themes/:theme/stylesheets/*filename", :controller=>'theme', :action=>'stylesheets'
map.named_route 'theme_javascript', "/themes/:theme/javascript/*filename", :controller=>'theme', :action=>'javascript'
map.connect "/themes/*whatever", :controller=>'theme', :action=>'error'
end
end
That's step one. Now onto step two. Getting the Rails framework to recognize the correct template file.
As pointed out by Ian Duggan in Matt's post, all you have to do is edit the file actioncontroller_ex.rb in \vendor\plugins\theme_support\lib\patches and add the following to the end of the file (after force_liquid_template):
alias_method :__assert_existence_of_template_file, :assert_existence_of_template_file
def assert_existence_of_template_file(template_name)
unless template_exists?(File.join("../../themes/#{current_theme}", template_name))
__assert_existence_of_template_file(template_name)
end
end
Just reset your server and things should be working seamlessly now. I just created a default and a blue theme and switched between them back and forth. You gotta try it out! It's absolutely amazing. Rails really is a cool beast. With 2 weeks experience I just fixed (with some help :)) a Templating system. I'm a proud programmer today!
UPDATE!
Here's how it looks:
Default Theme

Blue Theme

#125