Skip to content

Commit

Permalink
Allow to choose a custom routes' mount point during install
Browse files Browse the repository at this point in the history
Since we moved the frontend as an injectable templating system,
there are some manual steps to do to make the application work
when a user wants to change the mount point of where Solidus is
installed.

With this change, along with solidusio/solidus_starter_frontend#359,
we allow the user to select the mount point during the install, and
all the changes required are handled by the install script.
  • Loading branch information
kennyadsl authored and elia committed Dec 5, 2023
1 parent 946a49e commit f4f3950
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 17 deletions.
12 changes: 10 additions & 2 deletions admin/lib/generators/solidus_admin/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class InstallGenerator < Rails::Generators::Base

def install_solidus_core_support
route <<~RUBY
mount SolidusAdmin::Engine, at: '/admin', constraints: ->(req) {
mount SolidusAdmin::Engine, at: '#{solidus_mount_point}admin', constraints: ->(req) {
req.cookies['solidus_admin'] != 'false' &&
req.params['solidus_admin'] != 'false'
}
Expand Down Expand Up @@ -37,7 +37,15 @@ def install_lookbook
gem "actioncable"
end

route "mount Lookbook::Engine, at: '/lookbook' if Rails.env.development?"
route "mount Lookbook::Engine, at: '#{solidus_mount_point}lookbook' if Rails.env.development?"
end

private

def solidus_mount_point
mount_point = Spree::Core::Engine.routes.find_script_name({})
mount_point += "/" unless mount_point.end_with?("/")
mount_point
end
end
end
Expand Down
67 changes: 52 additions & 15 deletions core/lib/generators/solidus/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class InstallGenerator < Rails::Generators::AppBase
class_option :user_class, type: :string
class_option :admin_email, type: :string
class_option :admin_password, type: :string
class_option :mount_point, type: :string, desc: "Indicates where Solidus should be mounted. Defaults to '/'"

class_option :frontend, type: :string, enum: FRONTENDS.map { _1[:name] }, default: nil, desc: "Indicates which frontend to install."
class_option :authentication, type: :string, enum: AUTHENTICATIONS.map { _1[:name] }, default: nil, desc: "Indicates which authentication system to install."
Expand Down Expand Up @@ -87,6 +88,36 @@ def prepare_options
ENV['SOLIDUS_SKIP_MIGRATIONS_CHECK'] = 'true'
end

def install_routes
if Pathname(app_path).join('config', 'routes.rb').read.include? CORE_MOUNT_ROUTE
say_status :route_exist, CORE_MOUNT_ROUTE, :blue
else
say_status :installing, "solidus routes"
mount_point = options[:mount_point] || ask_with_default(
desc: 'Where would you like to mount Solidus? (E.g. "/store" or "/shop")',
default: '/',
)

shell.mute do
route <<~RUBY
# This line mounts Solidus's routes at the root of your application.
#
# Unless you manually picked only a subset of Solidus components, this will mount routes for:
# - solidus_backend
# - solidus_api
# This means, any requests to URLs such as /admin/products, will go to Spree::Admin::ProductsController.
#
# If you are using the Starter Frontend as your frontend, be aware that all the storefront routes are defined
# separately in this file and are not part of the Solidus::Core::Engine engine.
#
# If you would like to change where this engine is mounted, simply change the :at option to something different.
# We ask that you don't use the :as option here, as Solidus relies on it being the default of "spree"
#{CORE_MOUNT_ROUTE}, at: '#{mount_point}'
RUBY
end
end
end

def add_files
template 'config/initializers/spree.rb.tt', 'config/initializers/spree.rb'
end
Expand Down Expand Up @@ -134,21 +165,6 @@ def create_database
rake 'db:create'
end

def install_routes
if Pathname(app_path).join('config', 'routes.rb').read.include? CORE_MOUNT_ROUTE
say_status :route_exist, CORE_MOUNT_ROUTE, :blue
else
route <<~RUBY
# This line mounts Solidus's routes at the root of your application.
# This means, any requests to URLs such as /products, will go to Spree::ProductsController.
# If you would like to change where this engine is mounted, simply change the :at option to something different.
#
# We ask that you don't use the :as option here, as Solidus relies on it being the default of "spree"
#{CORE_MOUNT_ROUTE}, at: '/'
RUBY
end
end

def run_migrations
if @run_migrations
say_status :running, "migrations"
Expand Down Expand Up @@ -209,6 +225,27 @@ def bundle_command(command, env = {})
Bundler.reset_paths!
end

def ask_with_default(desc:, default:)
if options[:auto_accept]
say_status :using, "#{default} (default)"
return default
end

default_label = " (#{set_color :default, :bold}: \"#{default}\")" if default

say_status :question, "#{desc}#{default_label}.", :yellow
answer = ask(set_color("answer:".rjust(13), :blue, :bold)).to_s.downcase.presence

case answer
when nil
say_status :using, "#{default} (default)"
default
else
say_status :using, answer
answer
end
end

def ask_with_description(desc:, limited_to:, default:)
loop do
say_status :question, desc, :yellow
Expand Down

0 comments on commit f4f3950

Please sign in to comment.