Class: Spree::InstallGenerator

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_pathsObject



23
24
25
26
27
28
29
# File 'lib/generators/spree/install/install_generator.rb', line 23

def self.source_paths
  paths = superclass.source_paths
  paths << File.expand_path('../templates', "../../#{__FILE__}")
  paths << File.expand_path('../templates', "../#{__FILE__}")
  paths << File.expand_path('templates', __dir__)
  paths.flatten
end

Instance Method Details

#add_filesObject



45
46
47
# File 'lib/generators/spree/install/install_generator.rb', line 45

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

#completeObject



175
176
177
178
179
180
181
182
# File 'lib/generators/spree/install/install_generator.rb', line 175

def complete
  unless options[:quiet]
    puts '*' * 50
    puts "Spree has been installed successfully. You're all ready to go!"
    puts ' '
    puts 'Enjoy!'
  end
end

#configure_applicationObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/generators/spree/install/install_generator.rb', line 75

def configure_application
  application "\n    config.to_prepare do\n      # Load application's model / class decorators\n      Dir.glob(File.join(File.dirname(__FILE__), \"../app/**/*_decorator*.rb\")) do |c|\n        Rails.configuration.cache_classes ? require(c) : load(c)\n      end\n    end\n  APP\n\n  unless options[:enforce_available_locales].nil?\n    application <<-APP.strip_heredoc.indent!(4)\n      # Prevent this deprecation message: https://github.com/svenfuchs/i18n/commit/3b6e56e\n      I18n.enforce_available_locales = \#{options[:enforce_available_locales]}\n    APP\n  end\nend\n".strip_heredoc.indent!(4)

#include_seed_dataObject



94
95
96
97
98
99
100
# File 'lib/generators/spree/install/install_generator.rb', line 94

def include_seed_data
  append_file 'db/seeds.rb', "\n    Spree::Core::Engine.load_seed if defined?(Spree::Core)\n    Spree::Auth::Engine.load_seed if defined?(Spree::Auth)\n  SEEDS\nend\n".strip_heredoc

#install_adminObject



69
70
71
72
73
# File 'lib/generators/spree/install/install_generator.rb', line 69

def install_admin
  if @install_admin && Spree::Core::Engine.admin_available?
    generate 'spree:admin:install'
  end
end

#install_authenticationObject

Currently we only support devise, in the future we will also add support for default Rails authentication



50
51
52
53
54
55
56
# File 'lib/generators/spree/install/install_generator.rb', line 50

def install_authentication
  if @authentication == 'devise'
    generate 'spree:authentication:devise'
  elsif @authentication == 'dummy'
    # this is for dummy / test app
  end
end

#install_migrationsObject



102
103
104
105
106
107
108
109
110
# File 'lib/generators/spree/install/install_generator.rb', line 102

def install_migrations
  say_status :copying, 'migrations'
  silence_stream(STDOUT) do
    silence_warnings { rake 'active_storage:install:migrations' }
    silence_warnings { rake 'action_text:install:migrations' }
    silence_warnings { rake 'spree:install:migrations' }
    silence_warnings { rake 'spree_api:install:migrations' }
  end
end

#install_storefrontObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/generators/spree/install/install_generator.rb', line 58

def install_storefront
  if @install_storefront && Spree::Core::Engine.frontend_available?
    generate 'spree:storefront:install'

    # generate devise controllers if authentication is devise
    if @authentication == 'devise'
      generate 'spree:storefront:devise'
    end
  end
end

#load_sample_dataObject



140
141
142
143
144
145
146
147
148
149
# File 'lib/generators/spree/install/install_generator.rb', line 140

def load_sample_data
  return unless Spree::Core::Engine.sample_available?

  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

#notify_about_routesObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/generators/spree/install/install_generator.rb', line 151

def notify_about_routes
  insert_into_file(File.join('config', 'routes.rb'),
                   after: "Rails.application.routes.draw do\n") do
    "      # This line mounts Spree's routes at the root of your application.\n      # This means, any requests to URLs such as /products, will go to\n      # Spree::ProductsController.\n      # If you would like to change where this engine is mounted, simply change the\n      # :at option to something different.\n      #\n      # We ask that you don't use the :as option here, as Spree relies on it being\n      # the default of \"spree\".\n      mount Spree::Core::Engine, at: '/'\n    ROUTES\n  end\n\n  unless options[:quiet]\n    puts '*' * 50\n    puts \"We added the following line to your application's config/routes.rb file:\"\n    puts ' '\n    puts \"    mount Spree::Core::Engine, at: '/'\"\n  end\nend\n".strip_heredoc.indent!(2)

#populate_seed_dataObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/generators/spree/install/install_generator.rb', line 121

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]

    cmd = -> { rake("db:seed #{rake_options.join(' ')}") }
    if options[:auto_accept] || (options[:admin_email] && options[:admin_password])
      silence_warnings &cmd
    else
      cmd.call
    end
  else
    say_status :skipping, 'seed data (you can always run rake db:seed)'
  end
end

#prepare_optionsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/generators/spree/install/install_generator.rb', line 31

def prepare_options
  @run_migrations = options[:migrate]
  @load_seed_data = options[:seed]
  @load_sample_data = options[:sample]
  @install_storefront = options[:install_storefront]
  @install_admin = options[:install_admin]
  @authentication = options[:authentication]

  unless @run_migrations
    @load_seed_data = false
    @load_sample_data = false
  end
end

#run_migrationsObject



112
113
114
115
116
117
118
119
# File 'lib/generators/spree/install/install_generator.rb', line 112

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