I finally got it working.
The key seems to be an initializer after sprockets.environment
:
initializer 'custom-rails.sprockets_engine', after: 'sprockets.environment', group: :all do |app|
app.assets.register_engine '.custom', CustomerTemplate
end
Here's the final code for the ones who find themselves in similar situation. I also added Tilt template to make it work well.
# lib/custom/custom_template.rb
require 'tilt'
module Custom
module Rails
class CustomTemplate < ::Tilt::Template
self.default_mime_type = 'text/css'
def prepare
end
def evaluate(scope, locals, &block)
# Code for processing
end
def allows_script?
false
end
end
end
end
# lib/custom/engine.rb
module Custom
module Rails
class Engine < ::Rails::Engine
initializer 'custom-rails.sprockets_engine', after: 'sprockets.environment', group: :all do |app|
app.assets.register_engine '.custom', CustomerTemplate
end
end
end
# lib/custom/template_handler.rb
module Custom
module Rails
class TemplateHandler
def self.erb_handler
@@erb_handler ||= ActionView::Template.registered_template_handler(:erb)
end
def self.call(template)
compiled_source = erb_handler.call(template)
# Code for processing
end
end
end
end
ActiveSupport.on_load(:action_view) do
ActionView::Template.register_template_handler :custom, Custom::Rails::TemplateHandler
end
The first step of the debugging was that the .custom
extension was not handled correctly. I could see that they were not handled correctly because the CSS was served in the stylesheets
uri instead of the assets
uri.