Skip to content

Commit

Permalink
Make the install generator idempotent
Browse files Browse the repository at this point in the history
Currently if you run the install generator a second time the

    mount Spree::Core::Engine

route gets injected a second time.

Thor's insert_into_file should be an reversible operation but
I guess because we inject multiple lines into the routes file
it doesn't work as expected.
  • Loading branch information
tvdeyen committed Dec 22, 2017
1 parent ce908da commit 7ed23ee
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions core/lib/generators/spree/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

module Spree
class InstallGenerator < Rails::Generators::Base
CORE_MOUNT_ROUTE = "mount Spree::Core::Engine"

class_option :migrate, type: :boolean, default: true, banner: 'Run Solidus migrations'
class_option :seed, type: :boolean, default: true, banner: 'load seed data (migrations must be run)'
class_option :sample, type: :boolean, default: true, banner: 'load sample data (migrations must be run)'
Expand Down Expand Up @@ -149,23 +151,26 @@ def load_sample_data
end

def install_routes
insert_into_file File.join('config', 'routes.rb'), after: "Rails.application.routes.draw do\n" do
<<-ROUTES
routes_file_path = File.join('config', 'routes.rb')
unless File.read(routes_file_path).include? CORE_MOUNT_ROUTE
insert_into_file routes_file_path, after: "Rails.application.routes.draw do\n" do
<<-ROUTES
# 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"
mount Spree::Core::Engine, at: '/'
#{CORE_MOUNT_ROUTE}, at: '/'
ROUTES
ROUTES
end
end

unless options[:quiet]
puts "*" * 50
puts "We added the following line to your application's config/routes.rb file:"
puts " "
puts " mount Spree::Core::Engine, at: '/'"
puts " #{CORE_MOUNT_ROUTE}, at: '/'"
end
end

Expand Down

0 comments on commit 7ed23ee

Please sign in to comment.