Posts Tagged with "rails"

Rails Subdomain Caching

Oct 02, 2009  -  Comments

A while ago, I was working on a site that had dynamic subdomains based on cities in the database. For example, if an admin created a record for Nashville, it would create http://nashville.sitename.com. The site was pretty content heavy, but didn't change a whole lot, so I wanted to cache as much as I could. The problem with caching was that the content of each page depended on the subdomain. I couldn't use the normal caching strategy because of this.

To handle the subdomain routing and identification, I used the awesome subdomain-fu plugin (which worked great). Subdomain-fu, however, does not do the work of putting your cached files into folders named for your subdomain. Luckily, fixing this was as easy as adding a before filter to my ApplicationController.

class ApplicationController < ActionController::Base
  before_filter :update_cache_location
  def update_cache_location
    if current_subdomain.nil?
      ActionController::Base.page_cache_directory = "#{RAILS_ROOT}/public/cache/"
      ActionController::Base.page_cache_directory = "#{RAILS_ROOT}/public/cache/#{current_subdomain}/"

That method will update the page_cache_directory value to be your current subdomain (or default if you are on the main domain). Now, this takes care of one problem, but how the hell do we retrieve the cached files? I had a little bit of trouble with this, mainly because I'm not a mod_rewrite specialist. After some tinkering and testing, I finally came up with the following rules to put in my conf file to properly retrieve the cached files based on the subdomain.

# Check for subdomain cached index
RewriteCond %{HTTP_HOST} !^www\.sitename\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.sitename\.com
RewriteRule ^/$ /cache/%1/index.html [QSA]

# Check for subdomain cached page
RewriteCond %{HTTP_HOST} !^www\.sitename\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.sitename\.com
RewriteRule ^([^.]+)$ /cache/%1/$1.html [QSA]

# Check for regular non-subdomain index
RewriteRule ^/$ /cache/index.html [QSA] 

# Check for regular non-subdomain page
RewriteRule ^([^.]+)$ /cache/$1.html [QSA]

Those first two entries check to make sure the subdomain isn't www, then checks the cache folder for a cached version of the page. The last two entries check for the regular www subdomain.

These entries worked great for my project, but, like I said, I'm no expert with mod_rewrite. If you see anything that can be simplified or a better way of doing something, please let me know in the comments.

Tagged: railstutorialcaching

Create your own custom rails generator

Feb 22, 2009  -  Comments

It's really easy to add a custom generator to your Rails application. Say you have a component you want to include in multiple projects, but you don't want to manually copy ALL of the files from project to project. At Plexus, we have an empty Rails project with basic styling and structure that we use for all new applications. We have several components that we wanted to simplify adding to new projects. So, we created a few custom generators that we can use to create the components with very little effort.

The first thing you need to do is add a generators folder inside the lib folder. In there you can add the files and folders for each custom generator. In this example, I'll use a Blog as the component I'm building a generator for.

Inside the generators folder, I created a blog folder (hint: whatever you name the folder will be how you call your custom generator). All of my files for the blog functionality will be in this folder. The two most important things in this folder are the actual generator file that will do all of the work and the templates folder which contains all the files to be copied. My blog generator file, blog_generator.rb looks like this:

class BlogGenerator < Rails::Generator::Base
  def manifest
    record do |m|

      # Controllers
      m.file "controllers/blog_controller.rb", "app/controllers/blog_controller.rb"

      # Models
      m.file "models/blog_post.rb", "app/models/blog_post.rb"

      # Helpers
      m.file "helpers/blog_helper.rb", "app/helpers/blog_helper.rb"

      # Views
      m.directory "app/views/blog"
      m.file "views/index.html.erb", "app/views/blog/index.html.erb"
      m.file "views/details.html.erb", "app/views/blog/details.html.erb"
      m.file "views/feed.rss.builder", "app/views/blog/feed.rss.builder"

      # Migration
      m.migration_template "migrate/create_blog.rb", "db/migrate"

      # Tests
      m.file "test/fixtures/blog_posts.yml", "test/fixtures/blog_posts.yml"
      m.file "test/functional/blog_controller_test.rb", "test/functional/blog_controller_test.rb"
      m.file "test/unit/blog_post_test.rb", "test/unit/blog_post_test.rb"

      # CSS and images
      m.file "assets/blog_styles.css", "public/stylesheets/px_blogger.css"
      m.file "assets/comment_add.gif", "public/images/comment_add.gif"
      m.file "assets/comment.gif", "public/images/comment.gif"

      m.readme "INSTALL"

  def file_name


Here is a breakdown of what is going on:

  • The directory method will create the specified directory if it doesn't exist already.
  • The file method will copy the specified file to the given directory.
  • The migration_template file will copy the given migration file into the db/migrations folder using the file_name method defined at the bottom of the generator to name the file.
  • The readme function prints out the contents of the INSTALL file after the generator script is called. You can use this file to put any extra instructions for the generator.

This is what the file structure looks like for the generator:

\- generators
   \- blog
      \- blog_generator.rb
         \- assets
            \- blog_styles.css
            \- blog_controller.rb
            \- blog_helper.rb
            \- create_blog.rb
            \- blog_post.rb
            \- fixtures
               \- blog_posts.yml
               \- blog_controller_test.rb
               \- blog_post_test.rb
            \- index.html.erb

All we need to do to run this generator is call script/generate blog.

Tagged: railsgeneratorstutorial