Chaining Rails Template (view) Handlers

I have been looking for a way to get rid of all the ERB markup in the file and simply use plain HTML, albeit with my custom set of HTML tags. I don’t want to learn a new markup language just to do something simple but on the same token don’t want <%= … do %> … <% end %> splattered all over the page either. For example, I want to use “<row>…</row>” to invoke/substitute <%= row do %>…<% end %> automatically when the page is processed.

Rails 3 makes it easy to chain preprocessors to assets that will perform manipulations on the file prior to storing it, as the guides talk about here. However, this method limits you to only the assests files – scripts, css, images, for the most part.

I’ve been searching for a way to chain template handlers to action views in the same manner. For example, if you have a file called index.html.erb.something the framework would first process the Something handler, followed by the ERB handler. So far I’ve not been able to find a way to handle it quite in this form, however I have found a way to inject your preprocessing without changing extensions at all.

To do this I created the following initializer, with a simple but functional example:

module CustomMarkupHandler
`Chaining` =&gt; `Awesome`
details = {
:locals =&gt; template.locals,
:virtual_path =&gt; template.virtual_path,
:updated_at =&gt; template.updated_at
updated_source = template.source
MAPPINGS.each_pair {|find, replace|
updated_source = updated_source.gsub(find, replace)
new_template =, template.identifier, template.handler, details)
new_template = template
# Custom Markup Language registration
ActionView::Template.register_template_handler :erb, CustomMarkupHandler

Note that if you copy and paste this code you’ll need to replace the backwards quote with a normal one – my syntax highlighter doesn’t like single or double quotes.

Let’s walk through what this is doing:

  1. It defines a custom markup handler module
    • Typically you’d want to look up the registered handler for a given extension, however since we are overriding the handler for ERB, we’ve hard coded it here to use ActionView::Template::Handlers::ERB.
    • The code here simply does a gsub BEFORE allowing ERB to process the source. Create any *.erb page with the word “Chaining” in it and it will be replaced at render time with “Awesome”.
      • Want a guru to validate this part – I believe that by adding this before the ERB process it should not interfere with the way that the file is cached and served back up later, thus retaining roughly the same performance that we had prior to this injection.
    • The source attribute of the ActionView::Template is read only so we copied out the attributes we needed (details, identifier, handler) and simply use them, along with our updated source, to create a new template.
    • We then can send our modified source template through the ERB handler for traditional markup. Since we ran our modifications before ERB we can easily gsub in method calls that will be handled as if they were in the file natively.
    • Finally, just in case something unexpected happens, the whole thing is wrapped in begin/rescue that will simply process the unadulterated template, hopefully preventing a 500 error.
  2. It registers the handler as the default handler for *.erb files
    • This means that ALL files ending in *.erb will be routed through this new handler instead.

Rails Data Migrations Made Easy!

Introducing ActiveDataMigrations!

From the README file:

ActiveDataMigrations is a Ruby gem that allows developers to set up multiple migration locations, each of which can be run independent of one another. This library sits on top of ActiveRecord so all standard migration features remain available.

This is particularly useful in cases where you want to separate your data migrations from your schema migrations or where you have multiple steps in your migration process that must have other steps invoked throughout.

I’ve always felt that Rails migrations came up short in the area of data seeding and data migration. While you can use the standard schema migrations to manage your data it is a recipe for disaster.

For example, I’ve got my Tanglewood Turnings site that is now in production. I’ve got live customer and order data in the system so I can no longer simply apply a seed or global data update when I push updates out to production. Yet I still have a need to enter large amounts of new data.

In researching the problem on the interweb, the common answer in the Rails community seems to be to either add the data to your migrations files or to create a sql file and apply the changes directly. I personally make it a practice to avoid direct interaction with the database so I explored the migrations route. The problem was that my tests were failing due to the extra and unexpected data. So I added ” unless Rails.env == ‘test’ ” to the end of the data blocks. That stinks. As in bad code smell. Not a good option. The other alternative was to update my tests to accommodate the new data. That would have me updating test cases every time I updated production data. That also stinks.

So I decided to write a solution. I started out with this grandiose idea of somehow abducting the ActiveRecord::Migrator class, while still having it work within the Rails context, and twisting it to meet my own needs. Oh yeah, I also wanted to figure out how to do this without monkey patching. Figuring out how exactly to do this seemingly mundane task took me relatively deep into the Rails and ActiveRecord rabbit hole. While down there I realized that I was vastly over-complicating things and that I could achieve what I wanted with a couple lines of code and a new Rake task. The result was a Ruby Gem that is easy to use, easy to integrate, easy to maintain, and delegates 99.999% of its functionality right back to ActiveRecord, leaving it with the power and reducing my support overhead.

You can install it using:

gem install active_data_migrations

You can read the full write up, view the source, and get more information by visiting the git repository, here: