Skip to content

Commit

Permalink
resolve load order dependencies with Rails
Browse files Browse the repository at this point in the history
Using `if defined?(::ActionView)` can cause complications in
environments where Bundler gems are preloaded before a Rails
application boots. This will lead to a scenario where Slim just
silently fails to register a Rails template handler, which will lead
to missing template errors later.

The patch uses a `Rails::Railtie` instead to define a lazy
initialization path using an `on_load` hook. This will make the slim
gem independent of the gem and rails app loading order. The only
constraint is that `gem rails` appears before `gem slim` in the
Gemfile. However, this applies for pretty much any Rails gem.
  • Loading branch information
senny committed May 28, 2020
1 parent bfc1d42 commit 00dc52a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/slim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
require 'slim/engine'
require 'slim/template'
require 'slim/version'
require 'slim/railtie' if defined?(Rails::Railtie)
17 changes: 17 additions & 0 deletions lib/slim/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Slim
class Railtie < Rails::Railtie
initializer "initialize slim template handler" do
ActiveSupport.on_load(:action_view) do
Slim::RailsTemplate = Temple::Templates::Rails(Slim::Engine,
register_as: :slim,
# Use rails-specific generator. This is necessary
# to support block capturing and streaming.
generator: Temple::Generators::RailsOutputBuffer,
# Disable the internal slim capturing.
# Rails takes care of the capturing by itself.
disable_capture: true,
streaming: true)
end
end
end
end
14 changes: 0 additions & 14 deletions lib/slim/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,4 @@ module Slim
# Tilt template implementation for Slim
# @api public
Template = Temple::Templates::Tilt(Slim::Engine, register_as: :slim)

if defined?(::ActionView)
# Rails template implementation for Slim
# @api public
RailsTemplate = Temple::Templates::Rails(Slim::Engine,
register_as: :slim,
# Use rails-specific generator. This is necessary
# to support block capturing and streaming.
generator: Temple::Generators::RailsOutputBuffer,
# Disable the internal slim capturing.
# Rails takes care of the capturing by itself.
disable_capture: true,
streaming: true)
end
end

0 comments on commit 00dc52a

Please sign in to comment.