Rails 3: serving static assets from different locations depending on the topic

I am creating a simple theme system for a rails 3 application. A theme consists of a folder placed in Rails.root / themes containing

  • Manifest file .yml
  • some liquid template files
  • Static Assets Subfolder

Now for a specific controller / action, I would like to visualize the views from the current topic and, accordingly, use static assets.

Therefore, I need a way to tell the rails to rewrite

  • http://example.com/theme1/* ----> #{Rails.root}/themes/theme1/assets/*
  • http://example.com/theme2/* ----> #{Rails.root}/themes/theme2/assets/*
  • ...

Currently, I cannot figure out how to do this, since I would like to avoid using different engines for each topic or copying asset files in the public subfolder.

How can I solve this problem?

edit: other requirements

I was looking for something that did not violate rail defaults, so later I could take advantage of the new pipeline function (planned for rails 3.1).

Currently, I have found only this:

 config.asset_path = proc { |asset_path| "assets/#{asset_path}" } 

which would completely fulfill my requirements, unfortunately, it will not be applied when the resource pipeline is turned on.

+4
source share
1 answer

Take a look at the themes_for_rails plugin.

The following excerpts from readme show how you can change the theme used based on any required logic.

In controller action:

 class MyController < ApplicationController def show theme "purple" end end 

At the class level of your controller:

 class MyController < ApplicationController theme "purple" # all actions will use this theme def show ... end end 

Or using the function "resolver" lambda / function:

 class MyController < ApplicationController theme :theme_resolver # ... private def theme_resolver current_user.theme # or anything else that return a string. end end 

There are also helper functions for your views and mailers.

0
source

All Articles