Class: Spree::InstallGenerator

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

Constant Summary

CORE_MOUNT_ROUTE =
"mount Spree::Core::Engine"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_pathsObject



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

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', __FILE__)
  paths.flatten
end

Instance Method Details

#add_filesObject



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

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

#additional_tweaksObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/generators/spree/install/install_generator.rb', line 44

def additional_tweaks
  return unless File.exist? 'public/robots.txt'
  append_file "public/robots.txt", <<-ROBOTS
User-agent: *
Disallow: /checkout
Disallow: /cart
Disallow: /orders
Disallow: /user
Disallow: /account
Disallow: /api
Disallow: /password
  ROBOTS
end

#completeObject



179
180
181
182
183
184
185
186
# File 'lib/generators/spree/install/install_generator.rb', line 179

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

#configure_applicationObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/generators/spree/install/install_generator.rb', line 80

def configure_application
  application <<-APP

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

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

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

#create_databaseObject



117
118
119
120
# File 'lib/generators/spree/install/install_generator.rb', line 117

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

#create_overrides_directoryObject



76
77
78
# File 'lib/generators/spree/install/install_generator.rb', line 76

def create_overrides_directory
  empty_directory "app/overrides"
end

#include_seed_dataObject



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

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

#install_migrationsObject



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

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

#install_routesObject



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

def install_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"
  #{CORE_MOUNT_ROUTE}, at: '/'

ROUTES
    end
  end

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

#load_sample_dataObject



146
147
148
149
150
151
152
153
# File 'lib/generators/spree/install/install_generator.rb', line 146

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



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/generators/spree/install/install_generator.rb', line 132

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



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

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

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

#run_migrationsObject



122
123
124
125
126
127
128
129
130
# File 'lib/generators/spree/install/install_generator.rb', line 122

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

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

#setup_assetsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/generators/spree/install/install_generator.rb', line 58

def setup_assets
  @lib_name = 'spree'
  %w{javascripts stylesheets images}.each do |path|
    empty_directory "vendor/assets/#{path}/spree/frontend" if defined? Spree::Frontend || Rails.env.test?
    empty_directory "vendor/assets/#{path}/spree/backend" if defined? Spree::Backend || Rails.env.test?
  end

  if defined? Spree::Frontend || Rails.env.test?
    template "vendor/assets/javascripts/spree/frontend/all.js"
    template "vendor/assets/stylesheets/spree/frontend/all.css"
  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