From c455a2d53a0102fb86c8fe8150b9ad83001b5725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Busqu=C3=A9?= Date: Fri, 11 Jun 2021 05:43:41 +0200 Subject: [PATCH] Fix installation method as a gem executable To support `solidus_auth_devise`, it needs to rely on a new version of `solidus_support` that changes how it detects if a Solidus frontend is available and the moment it adds the extensions' controller & view directories to the Rails known paths. The previous `solidus_support` version added the extensions' paths on the engine's (in this case, `solidus_auth_devise`'s engine) load time and only when `SolidusSupport#frontend_available?` returned `true`. Engines are loaded before the application code, so at that time, the monkey patching of `#frontend_available?` that we were generating was not at hand. The solution is two-fold: we add the paths on an `initializer` on one side. However, at that moment, the application code is neither available, so on the other side, we change `#frontend_available?` to look for a `Rails.configuration.x.solidius.frontend_available` setting that we add to `application.rb` using the generator code. To understand the whole picture, keep in mind that `Rails.configuration` is available on an initializer, but it's not on an engine's load time. On top of that, we need to update a couple of things on the generated code: - We need to reference `Rails.root` instead of `Engine.root`, as `SolidusStarterFrontend::Engine` is unavailable on this installation method. - We need to copy to `application.rb` the custom code on `SolidusStarterFrontend::Engine` for the same reason as above. --- .../solidus_starter_frontend_generator.rb | 31 +++++++++++++------ .../views/override_generator.rb | 2 +- lib/solidus_starter_frontend.rb | 1 - lib/solidus_starter_frontend/engine.rb | 4 +++ .../solidus_support_extensions.rb | 9 ------ 5 files changed, 27 insertions(+), 20 deletions(-) delete mode 100644 lib/solidus_starter_frontend/solidus_support_extensions.rb diff --git a/lib/generators/solidus_starter_frontend/solidus_starter_frontend_generator.rb b/lib/generators/solidus_starter_frontend/solidus_starter_frontend_generator.rb index d9c63a469..3826f7d5a 100644 --- a/lib/generators/solidus_starter_frontend/solidus_starter_frontend_generator.rb +++ b/lib/generators/solidus_starter_frontend/solidus_starter_frontend_generator.rb @@ -5,26 +5,39 @@ class SolidusStarterFrontendGenerator < Rails::Generators::Base def install # Copy directories - directory 'app', 'app' - directory 'lib/views', 'lib/views' + directory 'app', 'app', exclude_pattern: /auth_views/ + directory 'lib/views', 'app/views' # Copy files copy_file 'lib/solidus_starter_frontend_configuration.rb', 'lib/solidus_starter_frontend_configuration.rb' copy_file 'lib/solidus_starter_frontend/config.rb', 'lib/solidus_starter_frontend/config.rb' - copy_file 'lib/solidus_starter_frontend/solidus_support_extensions.rb', 'lib/solidus_starter_frontend/solidus_support_extensions.rb' - - # Initializer - initializer 'solidus_starter_frontend.rb' do - "require 'solidus_starter_frontend/solidus_support_extensions'" - end # Routes copy_file 'config/routes.rb', 'tmp/routes.rb' prepend_file 'config/routes.rb', File.read('tmp/routes.rb') + # Enable Solidus frontend + application do + <<~RUBY + config.x.solidus.frontend_available = true + config.to_prepare do + if defined?(Spree::Auth::Engine) + [ + Spree::UserConfirmationsController, + Spree::UserPasswordsController, + Spree::UserRegistrationsController, + Spree::UserSessionsController, + Spree::UsersController + ].each do |auth_controller| + auth_controller.include SolidusStarterFrontend::Taxonomies + end + end + end + RUBY + end + # Gems gem 'canonical-rails' - gem 'solidus_support' gem 'truncate_html' # Text updates diff --git a/lib/generators/solidus_starter_frontend/views/override_generator.rb b/lib/generators/solidus_starter_frontend/views/override_generator.rb index 80c97d52e..5544909fe 100644 --- a/lib/generators/solidus_starter_frontend/views/override_generator.rb +++ b/lib/generators/solidus_starter_frontend/views/override_generator.rb @@ -7,7 +7,7 @@ module SolidusStarterFrontend module Views class OverrideGenerator < ::Rails::Generators::Base def self.views_folder - Engine.root.join('app', 'views', 'spree') + Rails.root.join('app', 'views', 'spree') end VIEWS = Dir.glob(views_folder.join('**', '*')) diff --git a/lib/solidus_starter_frontend.rb b/lib/solidus_starter_frontend.rb index 4d0efed4c..616c68c9a 100644 --- a/lib/solidus_starter_frontend.rb +++ b/lib/solidus_starter_frontend.rb @@ -6,7 +6,6 @@ require 'solidus_core' require 'solidus_support' -require 'solidus_starter_frontend/solidus_support_extensions' require 'solidus_starter_frontend/version' require 'solidus_starter_frontend/config' require 'solidus_starter_frontend/engine' diff --git a/lib/solidus_starter_frontend/engine.rb b/lib/solidus_starter_frontend/engine.rb index deacfb8b9..a2cefa40b 100644 --- a/lib/solidus_starter_frontend/engine.rb +++ b/lib/solidus_starter_frontend/engine.rb @@ -9,6 +9,10 @@ class Engine < Rails::Engine engine_name 'solidus_starter_frontend' + initializer 'solidus_starter_frontend', before: 'solidus_support_frontend_paths' do + Rails.configuration.x.solidus.frontend_available = true + end + # use rspec for tests config.generators do |g| g.test_framework :rspec diff --git a/lib/solidus_starter_frontend/solidus_support_extensions.rb b/lib/solidus_starter_frontend/solidus_support_extensions.rb deleted file mode 100644 index d145f0608..000000000 --- a/lib/solidus_starter_frontend/solidus_support_extensions.rb +++ /dev/null @@ -1,9 +0,0 @@ -# frozen_string_literal: true - -require 'solidus_support' - -module SolidusSupport - def self.frontend_available? - true - end -end