Class: Solidus::InstallGenerator

Inherits:
Rails::Generators::AppBase
  • Object
show all
Defined in:
lib/generators/solidus/install/install_generator.rb

Constant Summary collapse

CORE_MOUNT_ROUTE =
"mount Spree::Core::Engine"
FRONTENDS =
[
  { name: 'starter', description: 'Generate all necessary controllers and views directly in your Rails app', default: true },
  { name: 'none', description: 'Skip installing a frontend' }
]
AUTHENTICATIONS =
[
  { name: 'devise', description: 'Install and configure the standard `devise` integration', default: true },
  { name: 'existing', description: 'Integrate and configure an existing `devise` setup' },
  { name: 'custom', description: 'A starter configuration for rolling your own authentication system' },
  { name: 'none', description: 'Don\'t add any configuration for authentication' }
]
PAYMENT_METHODS =
[
  { name: 'paypal', description: 'Install `solidus_paypal_commerce_platform`', default: true },
  { name: 'stripe', description: 'Install `solidus_stripe`', default: false },
  { name: 'braintree', description: 'Install `solidus_braintree`', default: false },
  { name: 'none', description: 'Skip installing a payment method', default: false }
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/generators/solidus/install/install_generator.rb', line 50

def self.exit_on_failure?
  true
end

Instance Method Details

#add_filesObject



121
122
123
# File 'lib/generators/solidus/install/install_generator.rb', line 121

def add_files
  template 'config/initializers/spree.rb.tt', 'config/initializers/spree.rb'
end

#completeObject



215
216
217
# File 'lib/generators/solidus/install/install_generator.rb', line 215

def complete
  say_status :complete, "Solidus has been installed successfully. Enjoy!"
end

#create_databaseObject



163
164
165
166
# File 'lib/generators/solidus/install/install_generator.rb', line 163

def create_database
  say_status :creating, "database"
  rake 'db:create'
end

#create_overrides_directoryObject



148
149
150
# File 'lib/generators/solidus/install/install_generator.rb', line 148

def create_overrides_directory
  empty_directory "app/overrides"
end

#include_seed_dataObject



152
153
154
155
156
# File 'lib/generators/solidus/install/install_generator.rb', line 152

def include_seed_data
  append_file "db/seeds.rb", <<~RUBY
    Spree::Core::Engine.load_seed
  RUBY
end

#install_file_attachmentObject



125
126
127
128
129
130
131
132
133
# File 'lib/generators/solidus/install/install_generator.rb', line 125

def install_file_attachment
  if options[:active_storage]
    say_status :assets, "Active Storage", :green
    rake 'active_storage:install'
  else
    say_status :assets, "Paperclip", :green
    gsub_file 'config/initializers/spree.rb', "::ActiveStorageAttachment", "::PaperclipAttachment"
  end
end

#install_migrationsObject



158
159
160
161
# File 'lib/generators/solidus/install/install_generator.rb', line 158

def install_migrations
  say_status :copying, "migrations"
  rake 'railties:install:migrations'
end

#install_routesObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/generators/solidus/install/install_generator.rb', line 91

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

#install_solidus_adminObject



178
179
180
181
182
183
184
# File 'lib/generators/solidus/install/install_generator.rb', line 178

def install_solidus_admin
  return unless options[:admin_preview]

  say_status :installing, "SolidusAdmin", :blue
  bundle_command 'add solidus_admin -v ">= 0.2"'
  generate 'solidus_admin:install'
end

#install_subcomponentsObject



186
187
188
189
190
# File 'lib/generators/solidus/install/install_generator.rb', line 186

def install_subcomponents
  apply_template_for :authentication, @selected_authentication
  apply_template_for :frontend, @selected_frontend
  apply_template_for :payment_method, @selected_payment_method
end

#load_sample_dataObject



206
207
208
209
210
211
212
213
# File 'lib/generators/solidus/install/install_generator.rb', line 206

def load_sample_data
  if @load_sample_data
    say_status :loading, "sample data"
    rake 'spree_sample:load'
  else
    say_status :skipping, "sample data (you can always run rake spree_sample:load)"
  end
end

#populate_seed_dataObject



192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/generators/solidus/install/install_generator.rb', line 192

def populate_seed_data
  if @load_seed_data
    say_status :loading, "seed data"
    rake_options = []
    rake_options << "AUTO_ACCEPT=1" if options[:auto_accept]
    rake_options << "ADMIN_EMAIL=#{options[:admin_email]}" if options[:admin_email]
    rake_options << "ADMIN_PASSWORD=#{options[:admin_password]}" if options[:admin_password]

    rake("db:seed #{rake_options.join(' ')}")
  else
    say_status :skipping, "seed data (you can always run rake db:seed)"
  end
end

#prepare_optionsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/generators/solidus/install/install_generator.rb', line 54

def prepare_options
  @run_migrations = options[:migrate]
  @load_seed_data = options[:seed] && @run_migrations
  @load_sample_data = options[:sample] && @run_migrations && @load_seed_data

  @selected_frontend = selected_option_for(
    'frontend',
    selected: ENV['FRONTEND'] || options[:frontend],
    available_options: FRONTENDS,
  )

  @selected_authentication = selected_option_for(
    'authentication',
    selected:
      ('devise' if @selected_frontend == 'starter') ||
      ('devise' if has_gem?('solidus_auth_devise')) ||
      ENV['AUTHENTICATION'] || options[:authentication],
    available_options: AUTHENTICATIONS,
  )

  @selected_payment_method = selected_option_for(
    'payment method',
    selected:
      ('paypal' if has_gem?('solidus_paypal_commerce_platform')) ||
      ('stripe' if has_gem?('solidus_stripe')) ||
      ('bolt' if has_gem?('solidus_bolt')) ||
      ENV['PAYMENT_METHOD'] || options[:payment_method],
    available_options: PAYMENT_METHODS,
  )

  # Silence verbose output (e.g. Rails migrations will rely on this environment variable)
  ENV['VERBOSE'] = 'false'

  # No reason to check for their presence if we're about to install them
  ENV['SOLIDUS_SKIP_MIGRATIONS_CHECK'] = 'true'
end

#run_migrationsObject



168
169
170
171
172
173
174
175
176
# File 'lib/generators/solidus/install/install_generator.rb', line 168

def run_migrations
  if @run_migrations
    say_status :running, "migrations"

    rake 'db:migrate'
  else
    say_status :skipping, "migrations (don't forget to run rake db:migrate)"
  end
end

#setup_assetsObject



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/generators/solidus/install/install_generator.rb', line 135

def setup_assets
  empty_directory 'app/assets/images'

  %w{javascripts stylesheets images}.each do |path|
    empty_directory "vendor/assets/#{path}/spree/backend" if defined?(Spree::Backend) || Rails.env.test?
  end

  if defined?(Spree::Backend) || Rails.env.test?
    template "vendor/assets/javascripts/spree/backend/all.js"
    template "vendor/assets/stylesheets/spree/backend/all.css"
  end
end