Skip to content

Commit

Permalink
Add custom resolver to prevent Rails from rendering Haml Coffee templ…
Browse files Browse the repository at this point in the history
…ates for non-HTML formats. Fixes #80.
  • Loading branch information
jimmycuadra committed Jan 20, 2013
1 parent c566a25 commit c8991d3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
19 changes: 19 additions & 0 deletions lib/haml_coffee_assets/action_view/resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "action_view"

module HamlCoffeeAssets
module ActionView
# Custom resolver to prevent Haml Coffee templates from being rendered by
# Rails for non-HTML formats, since a template name without a MIME type
# in it would normally be a fallback for all formats.
#
class Resolver < ::ActionView::FileSystemResolver
def find_templates(name, prefix, partial, details)
if details[:formats].include?(:html)
super
else
[]
end
end
end
end
end
14 changes: 9 additions & 5 deletions lib/haml_coffee_assets/rails/engine.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# coding: UTF-8

require 'haml_coffee_assets/action_view/resolver'

module HamlCoffeeAssets
module Rails

Expand All @@ -10,11 +12,6 @@ class Engine < ::Rails::Engine

config.hamlcoffee = ::HamlCoffeeAssets.config

# Add shared template path to ActionView's load path
config.before_configuration do |app|
app.paths['app/views'] << config.hamlcoffee.shared_template_path
end

# Initialize Haml Coffee Assets after Sprockets
#
initializer 'sprockets.hamlcoffeeassets', :group => :all, :after => 'sprockets.environment' do |app|
Expand All @@ -25,6 +22,13 @@ class Engine < ::Rails::Engine
::ActionView::Template.register_template_handler(:hamlc, ::HamlCoffeeAssets::ActionView::TemplateHandler)
end

# Add template path to ActionController's view paths.
ActiveSupport.on_load(:action_controller) do
path = ::HamlCoffeeAssets.config.shared_template_path
resolver = ::HamlCoffeeAssets::ActionView::Resolver.new(path)
::ActionController::Base.append_view_path(resolver)
end

next unless app.assets

# Register Tilt template (for Sprockets)
Expand Down

0 comments on commit c8991d3

Please sign in to comment.