l'essentiel est invisible pour les yeux

Tuesday, May 06, 2008

[rails] Running Erubis on Edge Rails

ActionView class was refactoring on Edge Rails, see Ticket #10437 for more details. The template engines are integrated into new ActionView::Template class, but we have to modify some scripts in order to run Erubis on Edge Rails.

0. Install Erubis and Edge Rails

% sudo gem install erubis
% cd /path/to/rails_app/ && rake rails::freeze:edge

1. Create a template handler for Erubis.

Copy and paste below code at #{RAILS_ROOT}/lib/erubis_rails_helper.rb.
require 'erubis'
require 'erubis/preprocessing'

module Erubis
class Eruby
include ErboutEnhancer
end

class FastEruby
include ErboutEnhancer
end
end

module ActionView
module TemplateHandlers
class Erubis < TemplateHandler
include Compilable

# Erubis engine class
cattr_accessor :engine_class
@@engine_class = ::Erubis::Eruby

# Properties
cattr_accessor :init_properties
@@init_properties = {}

# Enable preprocessing if true
cattr_accessor :preprocessing
@@preprocessing = false

def compile(template)
if preprocessing
preprocessor = ::Erubis::PreprocessingEruby.new(template, init_properties)
template = preprocessor.evaluate(@view)
end
engine_class.new(template, init_properties).src
end
end
end
end
ActionView::Template.register_default_template_handler :erb, ActionView::TemplateHandlers::Erubis
ActionView::Template.register_template_handler :rhtml, ActionView::TemplateHandlers::Erubis

2. Configure settings of Erubis in enviroment.rb.

Append below scripts in config/enviroment.rb
require 'erubis_rails_helper'
ActionView::TemplateHandlers::Erubis.engine_class = Erubis::FastEruby
ActionView::TemplateHandlers::Erubis.preprocessing = true
Restart the web server.