Skip to content

Commit

Permalink
Fix installation method as a gem executable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
waiting-for-dev committed Jun 15, 2021
1 parent 5f86542 commit 9e7bfd6
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module AuthViews
protected

def configure_views
prepend_view_path Engine.root.join('lib', 'views', 'auth')
prepend_view_path Engine.join('lib', 'views', 'auth')
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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('**', '*'))
Expand Down
1 change: 0 additions & 1 deletion lib/solidus_starter_frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
4 changes: 4 additions & 0 deletions lib/solidus_starter_frontend/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions lib/solidus_starter_frontend/solidus_support_extensions.rb

This file was deleted.

0 comments on commit 9e7bfd6

Please sign in to comment.