From 2524ccc35407ce2f1e0153560a8ff70457105e4b Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Tue, 22 Feb 2011 14:24:26 -0600 Subject: [PATCH 1/2] allow for multiple template paths --- lib/mustache_rails.rb | 27 ++++++++++++++++++++------- spec/mustache_rails_spec.rb | 37 +++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 12 ++++++++++++ 3 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 spec/mustache_rails_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/lib/mustache_rails.rb b/lib/mustache_rails.rb index 8be4a9d..1c6c807 100644 --- a/lib/mustache_rails.rb +++ b/lib/mustache_rails.rb @@ -39,12 +39,21 @@ def partial(name) # # Mustache::Rails::Config.template_base_path = Rails.root.join('app', 'templates') module Config + + def self.default! + @template_paths = [::Rails.root.join('app/templates')] + end + + def self.template_paths + @template_paths || default! + end + def self.template_base_path - @template_base_path ||= ::Rails.root.join('app', 'templates') + @template_paths.first end def self.template_base_path=(value) - @template_base_path = value + @template_paths[0] = value end def self.template_extension @@ -77,7 +86,7 @@ class TemplateHandler < ActionView::Template::Handler def compile(template) mustache_class = mustache_class_from_template(template) mustache_class.template_file = mustache_template_file(template) - + <<-MUSTACHE mustache = ::#{mustache_class}.new mustache.view = self @@ -85,15 +94,15 @@ def compile(template) mustache.context.update(local_assigns) variables = controller.instance_variable_names variables -= %w[@template] - + if controller.respond_to?(:protected_instance_variables) variables -= controller.protected_instance_variables end - + variables.each do |name| mustache.instance_variable_set(name, controller.instance_variable_get(name)) end - + # Declaring an +attr_reader+ for each instance variable in the # Mustache::Rails subclass makes them available to your templates. mustache.class.class_eval do @@ -112,7 +121,11 @@ def mustache_class_from_template(template) end def mustache_template_file(template) - "#{Config.template_base_path}/#{template.virtual_path}.#{Config.template_extension}" + for template_path in Config.template_paths do + path = ::Rails.root.join(template_path, "#{template.virtual_path}.#{Config.template_extension}") + return path.to_s if File.exists?(path) + end + raise ActionView::MissingTemplate.new(Config.template_paths, template.virtual_path, {}, false) end end diff --git a/spec/mustache_rails_spec.rb b/spec/mustache_rails_spec.rb new file mode 100644 index 0000000..bac88b1 --- /dev/null +++ b/spec/mustache_rails_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper' + +describe Mustache::Rails do + + before do + @handler = Mustache::Rails::TemplateHandler.new + @template = mock(ActionView::Template, :virtual_path => "foo/action") + Mustache::Rails::Config.default! + end + + it "looks for templates by default in app/templates for templates with a .mustache extension" do + File.stub(:exists?).and_return(true) + @handler.send(:mustache_template_file, @template).should == "#{Rails.root}/app/templates/foo/action.html.mustache" + end + + it "raises an error if it can't find the template that you're looking for" do + File.stub(:exists?).and_return(false) + expect {@handler.send(:mustache_template_file, @template)}.to raise_error(ActionView::MissingTemplate) + end + + it "can find mustache templates from multiple configured places" do + Mustache::Rails::Config.template_paths << "my/other/template/place" + File.should_receive(:exists?).with(Rails.root.join("app/templates/foo/action.html.mustache")).and_return(false) + File.should_receive(:exists?).with(Rails.root.join("my/other/template/place/foo/action.html.mustache")).and_return(true) + @handler.send(:mustache_template_file, @template).should == "#{Rails.root}/my/other/template/place/foo/action.html.mustache" + end + + describe "backwards compatibility" do + it "supports using the template_base_path as the first entry in the template paths array" do + Mustache::Rails::Config.template_base_path.should == Rails.root.join("app/templates") + Mustache::Rails::Config.template_paths << "another/path" + Mustache::Rails::Config.template_base_path.should == Rails.root.join("app","templates") + Mustache::Rails::Config.template_base_path = "foo/bar" + Mustache::Rails::Config.template_paths.should == ["foo/bar", "another/path"] + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..43bcd7f --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,12 @@ + +$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) + +require 'active_support/dependencies' + + +module Rails + def self.root + Pathname("/fake/rails/root") + end +end +require 'mustache_rails' \ No newline at end of file From 46ef0daa338ca44fac824196c11e0b0088b5dccb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20de=20Burgt?= Date: Tue, 29 Mar 2011 15:21:25 -0500 Subject: [PATCH 2/2] register template handler for ".mustache" extension --- lib/mustache_rails.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/mustache_rails.rb b/lib/mustache_rails.rb index 1c6c807..78271e9 100644 --- a/lib/mustache_rails.rb +++ b/lib/mustache_rails.rb @@ -134,3 +134,4 @@ def mustache_template_file(template) ::ActiveSupport::Dependencies.autoload_paths << Rails.root.join("app", "views") ::ActionView::Template.register_template_handler(:rb, Mustache::Rails::TemplateHandler) +::ActionView::Template.register_template_handler(:mustache, Mustache::Rails::TemplateHandler)