From 5d56830de9a57f99b4ec35445343d1771a27a19f Mon Sep 17 00:00:00 2001 From: Liz Conlan Date: Tue, 28 May 2019 11:38:28 +0100 Subject: [PATCH] Use the Rails config.to_prepare hook to load the theme assets Rails 5 has introduced changes to the load order so now `Rails.application.config.assets.paths` is populated later - as part of the initialization process - so when our original code tries to prepend theme paths from `initializers/theme_loader.rb`, it is adding to an empty array which results in our custom paths being the last to be searched after the Rails and core app paths have been loaded. The `after_initialize` hook is too late as - if caching is in effect - the Sprockets environment has been cached at this point (and the path array is frozen), and changing `Rails.application.config.assets` has no effect. As `Rails.config.to_prepare` runs after the initializers but while changes can still be made and only runs once (at boot-up) in production so it should be a good choice: https://guides.rubyonrails.org/configuring.html#initializers --- lib/alavetelitheme.rb | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/alavetelitheme.rb b/lib/alavetelitheme.rb index 278b2f33..70d59234 100644 --- a/lib/alavetelitheme.rb +++ b/lib/alavetelitheme.rb @@ -15,16 +15,6 @@ def set_whatdotheyknow_view_paths alias :set_view_paths :set_whatdotheyknow_view_paths end -# Prepend the asset directories in this theme to the asset path: -['stylesheets', 'images', 'javascripts'].each do |asset_type| - theme_asset_path = File.join(File.dirname(__FILE__), - '..', - 'app', - 'assets', - asset_type) - Rails.application.config.assets.paths.unshift theme_asset_path -end - # Append individual theme assets to the asset path theme_asset_path = File.join(File.dirname(__FILE__), '..', @@ -50,6 +40,22 @@ def set_whatdotheyknow_view_paths ActiveSupport::Dependencies.autoload_once_paths.delete(path) end +def prepend_theme_assets + # Prepend the asset directories in this theme to the asset path: + ['stylesheets', 'images', 'javascripts'].each do |asset_type| + theme_asset_path = File.join(File.dirname(__FILE__), + '..', + 'app', + 'assets', + asset_type) + Rails.application.config.assets.paths.unshift theme_asset_path + end +end + +Rails.application.config.to_prepare do + prepend_theme_assets +end + # Monkey patch app code for patch in ['patch_mailer_paths.rb', 'controller_patches.rb',