lib/stache/mustache/handler.rb in stache-0.9.1 vs lib/stache/mustache/handler.rb in stache-1.0.0.rc

- old
+ new

@@ -18,13 +18,21 @@ # Return a string that will be eval'd in the context of the ActionView, ugly, but it works. <<-MUSTACHE mustache = ::#{mustache_class}.new mustache.view = self - mustache.template = '#{template.source.gsub(/'/, "\\\\'")}' + + # If we are rendering an abstract Stache::View class, don't render any template. + if #{mustache_class} == Stache::Mustache::View + template_source = '#{template.source.gsub(/'/, "\\\\'")}' + else + template_name = mustache.template_name+".#{template.formats.first.to_s}."+mustache.template_extension + template_source = File.read(File.join(::Stache.template_base_path, template_name)) + end + + mustache.template = template_source mustache.virtual_path = '#{template.virtual_path.to_s}' - mustache[:yield] = content_for(:layout) mustache.context.update(local_assigns) variables = controller.instance_variable_names variables -= %w[@template] if controller.respond_to?(:protected_instance_variables) @@ -50,10 +58,17 @@ new.compile(template) end # suss out a constant name for the given template def mustache_class_from_template(template) + # If we don't have a source template to render, return an abstract view class. + # This is normally used with rspec-rails. You probably never want to normally + # render a bare Stache::View + if template.source.empty? + return Stache::Mustache::View + end + const_name = ActiveSupport::Inflector.camelize(template.virtual_path.to_s) begin const_name.constantize rescue NameError, LoadError => e # Only rescue NameError/LoadError concerning our mustache_class @@ -65,6 +80,6 @@ end end end end -end \ No newline at end of file +end