diff --git a/lib/haml/railtie.rb b/lib/haml/railtie.rb index 42001a37ca..977f5e2c63 100644 --- a/lib/haml/railtie.rb +++ b/lib/haml/railtie.rb @@ -26,7 +26,15 @@ class Railtie < ::Rails::Railtie require "haml/sass_rails_filter" end - if defined?(::Erubi) && const_defined?('ActionView::Template::Handlers::ERB::Erubi') + # Any object under ActionView::Template will be defined as the root constant with the same + # name if it exists. If Erubi is loaded at all, ActionView::Template::Handlers::ERB::Erubi + # will turn out to be a reference to the ::Erubi module. + # In Rails 4.2, calling const_defined? results in odd exceptions, which seems to be + # solved by looking for ::Erubi first. + # However, in JRuby, the const_defined? finds it anyway, so we must make sure that it's + # not just a reference to ::Erubi. + if defined?(::Erubi) && const_defined?('ActionView::Template::Handlers::ERB::Erubi') && + ActionView::Template::Handlers::ERB::Erubi != ::Erubi require "haml/helpers/safe_erubi_template" Haml::Filters::RailsErb.template_class = Haml::SafeErubiTemplate else