From 380f73f633b83115600e93a0b5335aa3bd482c2a Mon Sep 17 00:00:00 2001 From: lamikae Date: Wed, 14 Mar 2012 21:20:00 +0200 Subject: [PATCH] Compile .coffee.erb files as regular .coffee files. Rails pipeline offers a way to compile a file through multiple targets. A coffeescript source file may be first compiled through erb renderer. Example of an use case would be to compile constants such as API keys into JS. This makes possible to test the functional parts of the source code that is unaffected by the values inserted by erb compiler. --- lib/jasmine/headless.rb | 3 ++- lib/jasmine/headless/coffee_erb_template.rb | 20 ++++++++++++++++++++ lib/jasmine/headless/files_list.rb | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 lib/jasmine/headless/coffee_erb_template.rb diff --git a/lib/jasmine/headless.rb b/lib/jasmine/headless.rb index 0a60efa..e145654 100644 --- a/lib/jasmine/headless.rb +++ b/lib/jasmine/headless.rb @@ -3,7 +3,7 @@ require 'pathname' module Jasmine module Headless - EXCLUDED_FORMATS = %w{less sass scss erb str} + EXCLUDED_FORMATS = %w{less sass scss str} autoload :CommandLine, 'jasmine/headless/command_line' @@ -23,6 +23,7 @@ module Jasmine autoload :FileChecker, 'jasmine/headless/file_checker' autoload :CoffeeTemplate, 'jasmine/headless/coffee_template' + autoload :CoffeeErbTemplate, 'jasmine/headless/coffee_erb_template' autoload :JSTemplate, 'jasmine/headless/js_template' autoload :JSTTemplate, 'jasmine/headless/jst_template' autoload :CSSTemplate, 'jasmine/headless/css_template' diff --git a/lib/jasmine/headless/coffee_erb_template.rb b/lib/jasmine/headless/coffee_erb_template.rb new file mode 100644 index 0000000..6fd1ab5 --- /dev/null +++ b/lib/jasmine/headless/coffee_erb_template.rb @@ -0,0 +1,20 @@ +require 'tilt/template' +require 'rainbow' + +module Jasmine::Headless + # This template flattens .coffee.erb files that may be present on + # the Rails asset pipeline. The file is rendered as CoffeeScript; + # erb template will not be rendered. + class CoffeeErbTemplate < Tilt::Template + + def prepare ; end + + def evaluate(scope, locals, &block) + if file[/coffee.erb$/] + Jasmine::Headless.warn("[%s] %s: %s" % [ "Erb File".color(:magenta), file.color(:yellow), "flatten template".color(:white) ]) + end + return '' + end + end +end + diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index 5e1acc9..05dc0bb 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -45,6 +45,7 @@ module Jasmine::Headless register_engine '.js', Jasmine::Headless::JSTemplate register_engine '.css', Jasmine::Headless::CSSTemplate register_engine '.jst', Jasmine::Headless::JSTTemplate + register_engine '.erb', Jasmine::Headless::CoffeeErbTemplate end end