Installing the Theme support plugin with Rails 2.0

written by damien on January 9th, 2008 @ 10:13 PM

I have already written an article about the Theme Support Plugin and Rails 1.2. Now I will talk about using it with Ruby on Rails 2.0. We consequently need to update the routing system like we did for the Rails 1.2 version. But you will also encounter a problem within the extension of Actionview::Base. Indeed, it will crash when trying to render any template. I have consequently patched the ActionView::Base#render_file.

alias_method :theme_support_old_render_file, :render_file

# Overrides the default Base#render_file to allow theme-specific views
def render_file(template_path, use_full_path = true, local_assigns = {})
   search_path = [
      "../themes/#{controller.current_theme}/views",
      "../../themes/#{controller.current_theme}/views",
      "../../themes/#{controller.current_theme}",
      "../../../themes/#{controller.current_theme}/views",
      ".",
      ".."
   ]

   if use_full_path
      search_path.each do |prefix|
         theme_path = prefix +'/'+ template_path
         begin
            template_extension = pick_template_extension(theme_path)

            # Prevent .rhtml (or any other template type) if force_liquid == true
            if force_liquid? and
               template_extension.to_s != 'liquid' and 
               prefix != '.'
               raise ThemeError.new("Template '#{template_path}' must be a liquid document")
            end

            local_assigns['active_theme'] = get_current_theme(local_assigns)

            rendered_template = theme_support_old_render_file(theme_path, use_full_path, local_assigns)
         rescue ActionView::TemplateError => err
            raise err
         rescue ActionView::ActionViewError => err
            next
         rescue ThemeError => err
           raise err
         end
         return rendered_template
      end
      raise ActionViewError, "No #{template_handler_preferences.to_sentence} template found for #{template_path} in #{view_paths.inspect}"
   else
      theme_support_old_render_file(template_path, use_full_path, local_assigns)
   end
end

In fact the plugin was awaiting an exception during the call to pick_template_extension when the path was not valid. But now Rails 2.0 only raise this exception while trying to render the file. I consequently render the template into a variable which may raise an exception.

You may apply the patch manually or if you want you can download the full Theme Support plugin for Rails 2.0 using the following links:

Comments are closed

Options:

Size

Colors