Class: Suspenders::AppBuilder

Inherits:
Rails::AppBuilder
  • Object
show all
Extended by:
Forwardable
Includes:
Actions
Defined in:
lib/suspenders/app_builder.rb

Instance Method Summary collapse

Methods included from Actions

#action_mailer_host, #configure_application_file, #configure_environment, #replace_in_file

Instance Method Details

#add_bower_dir_to_assets_pathsObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/suspenders/app_builder.rb', line 93

def add_bower_dir_to_assets_paths
  config = <<-RUBY
bower_components = Rails.root.join("vendor", "assets", "components")
Rails.application.config.assets.paths << bower_components
  RUBY

  after = "# Rails.application.config.assets.paths << Emoji.images_path\n"

  inject_into_file "config/initializers/assets.rb", config, after: after
end

#add_bullet_gem_configurationObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/suspenders/app_builder.rb', line 60

def add_bullet_gem_configuration
  config = <<-RUBY
  config.after_initialize do
Bullet.enable = true
Bullet.bullet_logger = true
Bullet.rails_logger = true
  end

  RUBY

  inject_into_file(
    "config/environments/development.rb",
    config,
    after: "config.action_mailer.raise_delivery_errors = true\n",
  )
end

#configure_action_mailerObject



323
324
325
326
327
# File 'lib/suspenders/app_builder.rb', line 323

def configure_action_mailer
  action_mailer_host "development", %{"localhost:3000"}
  action_mailer_host "test", %{"www.example.com"}
  action_mailer_host "production", %{ENV.fetch("APPLICATION_HOST")}
end

#configure_action_mailer_in_specsObject



298
299
300
# File 'lib/suspenders/app_builder.rb', line 298

def configure_action_mailer_in_specs
  copy_file 'action_mailer.rb', 'spec/support/action_mailer.rb'
end

#configure_active_jobObject



329
330
331
332
333
334
# File 'lib/suspenders/app_builder.rb', line 329

def configure_active_job
  configure_application_file(
    "config.active_job.queue_adapter = :delayed_job"
  )
  configure_environment "test", "config.active_job.queue_adapter = :inline"
end

#configure_automatic_deploymentObject



408
409
410
411
412
413
414
415
416
417
418
# File 'lib/suspenders/app_builder.rb', line 408

def configure_automatic_deployment
  deploy_command = <<-YML.strip_heredoc
  deployment:
    staging:
      branch: master
      commands:
        - bin/deploy staging
  YML

  append_file "circle.yml", deploy_command
end

#configure_background_jobs_for_rspecObject



294
295
296
# File 'lib/suspenders/app_builder.rb', line 294

def configure_background_jobs_for_rspec
  run 'rails g delayed_job:active_record'
end

#configure_capybara_webkitObject



302
303
304
# File 'lib/suspenders/app_builder.rb', line 302

def configure_capybara_webkit
  copy_file "capybara_webkit.rb", "spec/support/capybara_webkit.rb"
end

#configure_ciObject



281
282
283
# File 'lib/suspenders/app_builder.rb', line 281

def configure_ci
  template "circle.yml.erb", "circle.yml"
end

#configure_generatorsObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/suspenders/app_builder.rb', line 113

def configure_generators
  config = <<-RUBY

config.generators do |generate|
  generate.helper false
  generate.javascript_engine false
  generate.request_specs false
  generate.routing_specs false
  generate.stylesheets false
  generate.test_framework :rspec
  generate.view_specs false
end

  RUBY

  inject_into_class 'config/application.rb', 'Application', config
end

#configure_i18n_for_missing_translationsObject



289
290
291
292
# File 'lib/suspenders/app_builder.rb', line 289

def configure_i18n_for_missing_translations
  raise_on_missing_translations_in("development")
  raise_on_missing_translations_in("test")
end

#configure_i18n_for_test_environmentObject



285
286
287
# File 'lib/suspenders/app_builder.rb', line 285

def configure_i18n_for_test_environment
  copy_file "i18n.rb", "spec/support/i18n.rb"
end

#configure_newrelicObject



143
144
145
# File 'lib/suspenders/app_builder.rb', line 143

def configure_newrelic
  template 'newrelic.yml.erb', 'config/newrelic.yml'
end

#configure_pumaObject



340
341
342
# File 'lib/suspenders/app_builder.rb', line 340

def configure_puma
  copy_file "puma.rb", "config/puma.rb"
end

#configure_quiet_assetsObject



85
86
87
88
89
90
91
# File 'lib/suspenders/app_builder.rb', line 85

def configure_quiet_assets
  config = <<-RUBY
config.quiet_assets = true
  RUBY

  inject_into_class "config/application.rb", "Application", config
end

#configure_rack_timeoutObject



311
312
313
314
315
316
317
# File 'lib/suspenders/app_builder.rb', line 311

def configure_rack_timeout
  rack_timeout_config = <<-RUBY
Rack::Timeout.timeout = (ENV["RACK_TIMEOUT"] || 10).to_i
  RUBY

  append_file "config/environments/production.rb", rack_timeout_config
end

#configure_rspecObject



274
275
276
277
278
279
# File 'lib/suspenders/app_builder.rb', line 274

def configure_rspec
  remove_file "spec/rails_helper.rb"
  remove_file "spec/spec_helper.rb"
  copy_file "rails_helper.rb", "spec/rails_helper.rb"
  copy_file "spec_helper.rb", "spec/spec_helper.rb"
end

#configure_simple_formObject



319
320
321
# File 'lib/suspenders/app_builder.rb', line 319

def configure_simple_form
  bundle_command "exec rails generate simple_form:install"
end

#configure_smtpObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/suspenders/app_builder.rb', line 147

def configure_smtp
  copy_file 'smtp.rb', 'config/smtp.rb'

  prepend_file 'config/environments/production.rb',
    %{require Rails.root.join("config/smtp")\n}

  config = <<-RUBY

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = SMTP_SETTINGS
  RUBY

  inject_into_file 'config/environments/production.rb', config,
    after: "config.action_mailer.raise_delivery_errors = false"
end

#configure_spec_support_featuresObject



269
270
271
272
# File 'lib/suspenders/app_builder.rb', line 269

def configure_spec_support_features
  empty_directory_with_keep_file 'spec/features'
  empty_directory_with_keep_file 'spec/support/features'
end

#configure_time_formatsObject



306
307
308
309
# File 'lib/suspenders/app_builder.rb', line 306

def configure_time_formats
  remove_file "config/locales/en.yml"
  template "config_locales_en.yml.erb", "config/locales/en.yml"
end

#copy_dotfilesObject



377
378
379
# File 'lib/suspenders/app_builder.rb', line 377

def copy_dotfiles
  directory("dotfiles", ".")
end

#copy_miscellaneous_filesObject



438
439
440
441
442
443
# File 'lib/suspenders/app_builder.rb', line 438

def copy_miscellaneous_files
  copy_file ".bowerrc", ".bowerrc"
  copy_file "browserslist", "browserslist"
  copy_file "errors.rb", "config/initializers/errors.rb"
  copy_file "json_encoding.rb", "config/initializers/json_encoding.rb"
end

#create_application_layoutObject



239
240
241
242
243
# File 'lib/suspenders/app_builder.rb', line 239

def create_application_layout
  template 'suspenders_layout.html.erb.erb',
    'app/views/layouts/application.html.erb',
    force: true
end

#create_databaseObject



250
251
252
# File 'lib/suspenders/app_builder.rb', line 250

def create_database
  bundle_command 'exec rake db:create db:migrate'
end

#create_github_repo(repo_name) ⇒ Object



420
421
422
# File 'lib/suspenders/app_builder.rb', line 420

def create_github_repo(repo_name)
  run "hub create #{repo_name}"
end

#create_heroku_apps(flags) ⇒ Object



385
386
387
388
# File 'lib/suspenders/app_builder.rb', line 385

def create_heroku_apps(flags)
  create_staging_heroku_app(flags)
  create_production_heroku_app(flags)
end

#create_partials_directoryObject



219
220
221
# File 'lib/suspenders/app_builder.rb', line 219

def create_partials_directory
  empty_directory 'app/views/application'
end

#create_shared_css_overridesObject



232
233
234
235
236
237
# File 'lib/suspenders/app_builder.rb', line 232

def create_shared_css_overrides
  copy_file(
    "_css_overrides.html.erb",
    "app/views/application/_css_overrides.html.erb",
  )
end

#create_shared_flashesObject



223
224
225
226
# File 'lib/suspenders/app_builder.rb', line 223

def create_shared_flashes
  copy_file "_flashes.html.erb", "app/views/application/_flashes.html.erb"
  copy_file "flashes_helper.rb", "app/helpers/flashes_helper.rb"
end

#create_shared_javascriptsObject



228
229
230
# File 'lib/suspenders/app_builder.rb', line 228

def create_shared_javascripts
  copy_file '_javascript.html.erb', 'app/views/application/_javascript.html.erb'
end

#customize_error_pagesObject



445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/suspenders/app_builder.rb', line 445

def customize_error_pages
  meta_tags =<<-EOS
  <meta charset="utf-8" />
  <meta name="ROBOTS" content="NOODP" />
  <meta name="viewport" content="initial-scale=1" />
  EOS

  %w(500 404 422).each do |page|
    inject_into_file "public/#{page}.html", meta_tags, after: "<head>\n"
    replace_in_file "public/#{page}.html", /<!--.+-->\n/, ''
  end
end

#disable_xml_paramsObject



486
487
488
# File 'lib/suspenders/app_builder.rb', line 486

def disable_xml_params
  copy_file 'disable_xml_params.rb', 'config/initializers/disable_xml_params.rb'
end

#disallow_wrapping_parametersObject



215
216
217
# File 'lib/suspenders/app_builder.rb', line 215

def disallow_wrapping_parameters
  remove_file "config/initializers/wrap_parameters.rb"
end

#enable_database_cleanerObject



258
259
260
# File 'lib/suspenders/app_builder.rb', line 258

def enable_database_cleaner
  copy_file 'database_cleaner_rspec.rb', 'spec/support/database_cleaner.rb'
end

#enable_rack_canonical_hostObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/suspenders/app_builder.rb', line 163

def enable_rack_canonical_host
  config = <<-RUBY

  if ENV.fetch("HEROKU_APP_NAME", "").include?("staging-pr-")
ENV["APPLICATION_HOST"] = ENV["HEROKU_APP_NAME"] + ".herokuapp.com"
  end

  # Ensure requests are only served from one, canonical host name
  config.middleware.use Rack::CanonicalHost, ENV.fetch("APPLICATION_HOST")
  RUBY

  inject_into_file(
    "config/environments/production.rb",
    config,
    after: "Rails.application.configure do",
  )
end

#enable_rack_deflaterObject



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/suspenders/app_builder.rb', line 181

def enable_rack_deflater
  config = <<-RUBY

  # Enable deflate / gzip compression of controller-generated responses
  config.middleware.use Rack::Deflater
  RUBY

  inject_into_file(
    "config/environments/production.rb",
    config,
    after: serve_static_files_line
  )
end

#gemfileObject



28
29
30
# File 'lib/suspenders/app_builder.rb', line 28

def gemfile
  template "Gemfile.erb", "Gemfile"
end

#generate_factories_fileObject



135
136
137
# File 'lib/suspenders/app_builder.rb', line 135

def generate_factories_file
  copy_file "factories.rb", "spec/factories.rb"
end

#generate_rspecObject



336
337
338
# File 'lib/suspenders/app_builder.rb', line 336

def generate_rspec
  generate 'rspec:install'
end

#gitignoreObject



24
25
26
# File 'lib/suspenders/app_builder.rb', line 24

def gitignore
  copy_file "suspenders_gitignore", ".gitignore"
end

#init_gitObject



381
382
383
# File 'lib/suspenders/app_builder.rb', line 381

def init_git
  run 'git init'
end

#install_bittersObject



359
360
361
# File 'lib/suspenders/app_builder.rb', line 359

def install_bitters
  run "bitters install --path app/assets/stylesheets"
end

#install_refillsObject



354
355
356
357
# File 'lib/suspenders/app_builder.rb', line 354

def install_refills
  generate "refills:import", "flashes"
  remove_dir "app/views/refills"
end

#provide_deploy_scriptObject



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/suspenders/app_builder.rb', line 390

def provide_deploy_script
  copy_file "bin_deploy", "bin/deploy"

  instructions = <<-MARKDOWN

## Deploying

If you have previously run the `./bin/setup` script,
you can deploy to staging and production with:

$ ./bin/deploy staging
$ ./bin/deploy production
  MARKDOWN

  append_file "README.md", instructions
  run "chmod a+x bin/deploy"
end

#provide_dev_prime_taskObject



109
110
111
# File 'lib/suspenders/app_builder.rb', line 109

def provide_dev_prime_task
  copy_file 'dev.rake', 'lib/tasks/dev.rake'
end

#provide_setup_scriptObject



104
105
106
107
# File 'lib/suspenders/app_builder.rb', line 104

def provide_setup_script
  template "bin_setup", "bin/setup", force: true
  run "chmod a+x bin/setup"
end

#provide_shoulda_matchers_configObject



262
263
264
265
266
267
# File 'lib/suspenders/app_builder.rb', line 262

def provide_shoulda_matchers_config
  copy_file(
    "shoulda_matchers_config_rspec.rb",
    "spec/support/shoulda_matchers.rb"
  )
end

#raise_on_delivery_errorsObject



47
48
49
50
# File 'lib/suspenders/app_builder.rb', line 47

def raise_on_delivery_errors
  replace_in_file 'config/environments/development.rb',
    'raise_delivery_errors = false', 'raise_delivery_errors = true'
end

#raise_on_missing_assets_in_testObject



39
40
41
42
43
44
45
# File 'lib/suspenders/app_builder.rb', line 39

def raise_on_missing_assets_in_test
  inject_into_file(
    "config/environments/test.rb",
    "\n  config.assets.raise_runtime_errors = true",
    after: "Rails.application.configure do",
  )
end

#raise_on_unpermitted_parametersObject



77
78
79
80
81
82
83
# File 'lib/suspenders/app_builder.rb', line 77

def raise_on_unpermitted_parameters
  config = <<-RUBY
config.action_controller.action_on_unpermitted_parameters = :raise
  RUBY

  inject_into_class "config/application.rb", "Application", config
end

#readmeObject



20
21
22
# File 'lib/suspenders/app_builder.rb', line 20

def readme
  template 'README.md.erb', 'README.md'
end

#remove_config_comment_linesObject



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/suspenders/app_builder.rb', line 458

def remove_config_comment_lines
  config_files = [
    "application.rb",
    "environment.rb",
    "environments/development.rb",
    "environments/production.rb",
    "environments/test.rb",
  ]

  config_files.each do |config_file|
    path = File.join(destination_root, "config/#{config_file}")

    accepted_content = File.readlines(path).reject do |line|
      line =~ /^.*#.*$/ || line =~ /^$\n/
    end

    File.open(path, "w") do |file|
      accepted_content.each { |line| file.puts line }
    end
  end
end

#remove_routes_comment_linesObject



480
481
482
483
484
# File 'lib/suspenders/app_builder.rb', line 480

def remove_routes_comment_lines
  replace_in_file 'config/routes.rb',
    /Rails\.application\.routes\.draw do.*end/m,
    "Rails.application.routes.draw do\nend"
end

#set_ruby_to_version_being_usedObject



254
255
256
# File 'lib/suspenders/app_builder.rb', line 254

def set_ruby_to_version_being_used
  create_file '.ruby-version', "#{Suspenders::RUBY_VERSION}\n"
end

#set_test_delivery_methodObject



52
53
54
55
56
57
58
# File 'lib/suspenders/app_builder.rb', line 52

def set_test_delivery_method
  inject_into_file(
    "config/environments/development.rb",
    "\n  config.action_mailer.delivery_method = :file",
    after: "config.action_mailer.raise_delivery_errors = true",
  )
end

#set_up_factory_girl_for_rspecObject



131
132
133
# File 'lib/suspenders/app_builder.rb', line 131

def set_up_factory_girl_for_rspec
  copy_file 'factory_girl_rspec.rb', 'spec/support/factory_girl.rb'
end

#set_up_foregoObject



344
345
346
# File 'lib/suspenders/app_builder.rb', line 344

def set_up_forego
  copy_file "Procfile", "Procfile"
end

#set_up_houndObject



139
140
141
# File 'lib/suspenders/app_builder.rb', line 139

def set_up_hound
  copy_file "hound.yml", ".hound.yml"
end

#setup_asset_hostObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/suspenders/app_builder.rb', line 195

def setup_asset_host
  replace_in_file 'config/environments/production.rb',
    "# config.action_controller.asset_host = 'http://assets.example.com'",
    'config.action_controller.asset_host = ENV.fetch("ASSET_HOST", ENV.fetch("APPLICATION_HOST"))'

  replace_in_file 'config/initializers/assets.rb',
    "config.assets.version = '1.0'",
    'config.assets.version = (ENV["ASSETS_VERSION"] || "1.0")'

  inject_into_file(
    "config/environments/production.rb",
    '  config.static_cache_control = "public, max-age=#{1.year.to_i}"',
    after: serve_static_files_line
  )
end

#setup_bundler_auditObject



429
430
431
432
# File 'lib/suspenders/app_builder.rb', line 429

def setup_bundler_audit
  copy_file "bundler_audit.rake", "lib/tasks/bundler_audit.rake"
  append_file "Rakefile", %{\ntask default: "bundler:audit"\n}
end

#setup_default_directoriesObject



363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/suspenders/app_builder.rb', line 363

def setup_default_directories
  [
    'app/views/pages',
    'spec/lib',
    'spec/controllers',
    'spec/helpers',
    'spec/support/matchers',
    'spec/support/mixins',
    'spec/support/shared_examples'
  ].each do |dir|
    empty_directory_with_keep_file dir
  end
end

#setup_default_rake_taskObject



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/suspenders/app_builder.rb', line 490

def setup_default_rake_task
  append_file 'Rakefile' do
    <<-EOS
task(:default).clear
task default: [:spec]

if defined? RSpec
  task(:spec).clear
  RSpec::Core::RakeTask.new(:spec) do |t|
t.verbose = false
  end
end
    EOS
  end
end

#setup_rack_mini_profilerObject



32
33
34
35
36
37
# File 'lib/suspenders/app_builder.rb', line 32

def setup_rack_mini_profiler
  copy_file(
    "rack_mini_profiler.rb",
    "config/initializers/rack_mini_profiler.rb",
  )
end

#setup_secret_tokenObject



211
212
213
# File 'lib/suspenders/app_builder.rb', line 211

def setup_secret_token
  template 'secrets.yml', 'config/secrets.yml', force: true
end

#setup_segmentObject



424
425
426
427
# File 'lib/suspenders/app_builder.rb', line 424

def setup_segment
  copy_file '_analytics.html.erb',
    'app/views/application/_analytics.html.erb'
end

#setup_springObject



434
435
436
# File 'lib/suspenders/app_builder.rb', line 434

def setup_spring
  bundle_command "exec spring binstub --all"
end

#setup_stylesheetsObject



348
349
350
351
352
# File 'lib/suspenders/app_builder.rb', line 348

def setup_stylesheets
  remove_file "app/assets/stylesheets/application.css"
  copy_file "application.scss",
            "app/assets/stylesheets/application.scss"
end

#use_postgres_config_templateObject



245
246
247
248
# File 'lib/suspenders/app_builder.rb', line 245

def use_postgres_config_template
  template 'postgresql_database.yml.erb', 'config/database.yml',
    force: true
end