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



21
22
23
24
25
# File 'lib/generators/spree/install/install_generator.rb', line 21

def self.source_paths
  paths = superclass.source_paths
  paths << File.expand_path('templates', __dir__)
  paths.flatten
end

Instance Method Details

#add_filesObject



39
40
41
# File 'lib/generators/spree/install/install_generator.rb', line 39

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

#completeObject



149
150
151
152
153
154
155
156
# File 'lib/generators/spree/install/install_generator.rb', line 149

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/generators/spree/install/install_generator.rb', line 51

def configure_application
  application <<-APP.strip_heredoc.indent!(4)

    config.to_prepare do
      # Load application's model / class decorators
      Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
        Rails.configuration.cache_classes ? require(c) : load(c)
      end
    end
  APP

  unless options[:enforce_available_locales].nil?
    application <<-APP.strip_heredoc.indent!(4)
      # Prevent this deprecation message: https://github.com/svenfuchs/i18n/commit/3b6e56e
      I18n.enforce_available_locales = #{options[:enforce_available_locales]}
    APP
  end
end

#include_seed_dataObject



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

def include_seed_data
  append_file 'db/seeds.rb', <<-SEEDS.strip_heredoc

    Spree::Core::Engine.load_seed if defined?(Spree::Core)
  SEEDS
end

#install_authenticationObject



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

def install_authentication
  if @authentication == 'devise'
    generate 'spree:authentication:devise'
  elsif @authentication == 'dummy'
    generate 'spree:authentication:dummy'
  end
end

#install_migrationsObject



77
78
79
80
81
82
83
84
# File 'lib/generators/spree/install/install_generator.rb', line 77

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' }
  end
end

#load_sample_dataObject



114
115
116
117
118
119
120
121
122
123
# File 'lib/generators/spree/install/install_generator.rb', line 114

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/generators/spree/install/install_generator.rb', line 125

def notify_about_routes
  insert_into_file(File.join('config', 'routes.rb'),
                   after: "Rails.application.routes.draw do\n") do
    <<-ROUTES.strip_heredoc.indent!(2)
      # This line mounts Spree'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 Spree relies on it being
      # the default of "spree".
      mount Spree::Core::Engine, at: '/'
    ROUTES
  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: '/'"
  end
end

#populate_seed_dataObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/generators/spree/install/install_generator.rb', line 95

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 bin/rails db:seed)'
  end
end

#prepare_optionsObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/generators/spree/install/install_generator.rb', line 27

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

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

#run_migrationsObject



86
87
88
89
90
91
92
93
# File 'lib/generators/spree/install/install_generator.rb', line 86

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