Class: Suspenders::AppBuilder

Inherits:
Rails::AppBuilder
  • Object
show all
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_helpers_for_rspecObject



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

def add_helpers_for_rspec
  copy_file 'queries_helper_rspec.rb', 'spec/support/queries_helper.rb'
  copy_file 'fixtures_helper_rspec.rb', 'spec/support/fixtures_helper.rb'
end

#configure_action_mailerObject



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

def configure_action_mailer
  action_mailer_host "development", %{"localhost:#{port}"}
  action_mailer_host "test", %{"www.example.com"}
  action_mailer_host "staging", %{ENV.fetch("HOST")}
  action_mailer_host "production", %{ENV.fetch("HOST")}
end

#configure_action_mailer_in_specsObject



261
262
263
# File 'lib/suspenders/app_builder.rb', line 261

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

#configure_active_jobObject



309
310
311
312
313
314
# File 'lib/suspenders/app_builder.rb', line 309

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_available_localesObject



301
302
303
304
305
306
307
# File 'lib/suspenders/app_builder.rb', line 301

def configure_available_locales
  config = <<-RUBY
config.i18n.available_locales = [:en, :it]
  RUBY

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

#configure_background_jobs_for_rspecObject



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

def configure_background_jobs_for_rspec
  run 'rails g delayed_job:active_record'
end

#configure_bulletObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/suspenders/app_builder.rb', line 22

def configure_bullet
  config = <<-RUBY.strip_heredoc
  config.after_initialize do
      Bullet.enable = true
      Bullet.bullet_logger = true
      Bullet.console = true
      Bullet.rails_logger = true
      # Bullet.stacktrace_includes = [ 'your_gem', 'your_middleware' ]
    end
  RUBY
  configure_environment "development", config
end

#configure_generatorsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/suspenders/app_builder.rb', line 52

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



247
248
249
250
# File 'lib/suspenders/app_builder.rb', line 247

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

#configure_i18n_for_test_environmentObject



243
244
245
# File 'lib/suspenders/app_builder.rb', line 243

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

#configure_i18n_tasksObject



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

def configure_i18n_tasks
  run "cp $(i18n-tasks gem-path)/templates/rspec/i18n_spec.rb spec/"
  copy_file "config_i18n_tasks.yml", "config/i18n-tasks.yml"
end

#configure_letter_openerObject



14
15
16
17
18
19
20
# File 'lib/suspenders/app_builder.rb', line 14

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

#configure_localesObject



265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/suspenders/app_builder.rb', line 265

def configure_locales
  remove_file "config/locales/en.yml"
  template "config_locales_it.yml.erb", "config/locales/it.yml"

  replace_in_file "config/application.rb",
    "# config.time_zone = 'Central Time (US & Canada)'",
    "config.time_zone = 'Rome'"

  replace_in_file "config/application.rb",
    "# config.i18n.default_locale = :de",
    "config.i18n.default_locale = :it"
end

#configure_newrelicObject



87
88
89
# File 'lib/suspenders/app_builder.rb', line 87

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

#configure_rack_timeoutObject



278
279
280
281
282
283
284
# File 'lib/suspenders/app_builder.rb', line 278

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



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

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



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

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

#configure_slimObject



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

def configure_slim
  copy_file 'slim.rb', 'config/initializers/slim.rb'
end

#configure_smtpObject



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

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



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

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

#configure_travisObject



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

def configure_travis
  template 'travis.yml.erb', '.travis.yml'
end

#configure_unicornObject



328
329
330
# File 'lib/suspenders/app_builder.rb', line 328

def configure_unicorn
  copy_file 'unicorn.rb', 'config/unicorn.rb'
end

#copy_miscellaneous_filesObject



473
474
475
476
477
# File 'lib/suspenders/app_builder.rb', line 473

def copy_miscellaneous_files
  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



186
187
188
189
190
191
# File 'lib/suspenders/app_builder.rb', line 186

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

#create_binstubsObject



469
470
471
# File 'lib/suspenders/app_builder.rb', line 469

def create_binstubs
  bundle_command "binstubs brakeman"
end

#create_databaseObject



198
199
200
# File 'lib/suspenders/app_builder.rb', line 198

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

#create_github_repo(repo_name) ⇒ Object



455
456
457
458
# File 'lib/suspenders/app_builder.rb', line 455

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

#create_heroku_apps(flags) ⇒ Object



397
398
399
400
# File 'lib/suspenders/app_builder.rb', line 397

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

#create_partials_directoryObject



173
174
175
# File 'lib/suspenders/app_builder.rb', line 173

def create_partials_directory
  empty_directory 'app/views/application'
end

#create_production_heroku_app(flags) ⇒ Object



391
392
393
394
395
# File 'lib/suspenders/app_builder.rb', line 391

def create_production_heroku_app(flags)
  app_name = heroku_app_name_for("production")

  run_heroku "create #{app_name} #{flags}", "production"
end

#create_ruby_gemset_fileObject



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

def create_ruby_gemset_file
  create_file '.ruby-gemset', "#{app_name}\n"
end

#create_shared_flashesObject



177
178
179
180
# File 'lib/suspenders/app_builder.rb', line 177

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

#create_shared_javascriptsObject



182
183
184
# File 'lib/suspenders/app_builder.rb', line 182

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

#create_staging_heroku_app(flags) ⇒ Object



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

def create_staging_heroku_app(flags)
  rack_env = "RACK_ENV=staging RAILS_ENV=staging"
  app_name = heroku_app_name_for("staging")

  run_heroku "create #{app_name} #{flags}", "staging"
  run_heroku "config:add #{rack_env}", "staging"
end

#customize_error_pagesObject



479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/suspenders/app_builder.rb', line 479

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



498
499
500
# File 'lib/suspenders/app_builder.rb', line 498

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

#disallow_wrapping_parametersObject



169
170
171
# File 'lib/suspenders/app_builder.rb', line 169

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

#enable_database_cleanerObject



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

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

#enable_rack_canonical_hostObject



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/suspenders/app_builder.rb', line 107

def enable_rack_canonical_host
  config = <<-RUBY

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

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

#enable_rack_deflaterObject



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/suspenders/app_builder.rb', line 121

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

#fix_i18n_deprecation_warningObject



316
317
318
319
320
321
322
# File 'lib/suspenders/app_builder.rb', line 316

def fix_i18n_deprecation_warning
  config = <<-RUBY
config.i18n.enforce_available_locales = true
  RUBY

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

#generate_rspecObject



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

def generate_rspec
  generate 'rspec:install'
end

#gitignore_filesObject



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/suspenders/app_builder.rb', line 353

def gitignore_files
  remove_file '.gitignore'
  copy_file 'suspenders_gitignore', '.gitignore'
  [
    'app/forms',
    'app/presenters',
    'app/queries',
    'app/services',
    'app/views/pages',
    'spec/controllers',
    'spec/forms',
    'spec/helpers',
    'spec/lib',
    'spec/presenters',
    'spec/queries',
    'spec/services',
    'spec/fixtures',
    'spec/support/matchers',
    'spec/support/mixins',
    'spec/support/shared_examples'
  ].each do |dir|
    run "mkdir #{dir}"
    run "touch #{dir}/.keep"
  end
end

#init_gitObject



379
380
381
# File 'lib/suspenders/app_builder.rb', line 379

def init_git
  run 'git init'
end

#install_bittersObject



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

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

#install_refillsObject



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

def install_refills
  run "rails generate refills:import flashes"
  run "rm app/views/refills/_flashes.html.erb"
  run "rmdir app/views/refills"
end

#join_heroku_app(environment) ⇒ Object



413
414
415
416
417
418
419
420
421
422
423
# File 'lib/suspenders/app_builder.rb', line 413

def join_heroku_app(environment)
  heroku_app_name = heroku_app_name_for(environment)
  <<-SHELL
if heroku join --app #{heroku_app_name} &> /dev/null; then
  git remote add #{environment} [email protected]:#{heroku_app_name}.git || true
  printf 'You are a collaborator on the "#{heroku_app_name}" Heroku app\n'
else
  printf 'Ask for access to the "#{heroku_app_name}" Heroku app\n'
fi
  SHELL
end

#provide_deploy_scriptObject



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/suspenders/app_builder.rb', line 437

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



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

def provide_dev_prime_task
  copy_file 'development_seeds.rb', 'lib/tasks/development_seeds.rake'
end

#provide_setup_scriptObject



43
44
45
46
# File 'lib/suspenders/app_builder.rb', line 43

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

#raise_on_delivery_errorsObject



9
10
11
12
# File 'lib/suspenders/app_builder.rb', line 9

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

#raise_on_unpermitted_parametersObject



35
36
37
38
39
40
41
# File 'lib/suspenders/app_builder.rb', line 35

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



5
6
7
# File 'lib/suspenders/app_builder.rb', line 5

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

#remove_routes_comment_linesObject



492
493
494
495
496
# File 'lib/suspenders/app_builder.rb', line 492

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

#replace_gemfileObject



202
203
204
205
# File 'lib/suspenders/app_builder.rb', line 202

def replace_gemfile
  remove_file 'Gemfile'
  template 'Gemfile.erb', 'Gemfile'
end

#set_heroku_rails_secretsObject



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

def set_heroku_rails_secrets
  %w(staging production).each do |environment|
    run_heroku "config:add SECRET_KEY_BASE=#{generate_secret}", environment
  end
end

#set_heroku_remotesObject



402
403
404
405
406
407
408
409
410
411
# File 'lib/suspenders/app_builder.rb', line 402

def set_heroku_remotes
  remotes = <<-SHELL

# Set up the staging and production apps.
#{join_heroku_app('staging')}
#{join_heroku_app('production')}
  SHELL

  append_file 'bin/setup', remotes
end

#set_heroku_serve_static_filesObject



431
432
433
434
435
# File 'lib/suspenders/app_builder.rb', line 431

def set_heroku_serve_static_files
  %w(staging production).each do |environment|
    run_heroku "config:add RAILS_SERVE_STATIC_FILES=true", environment
  end
end

#set_ruby_to_version_being_usedObject



207
208
209
# File 'lib/suspenders/app_builder.rb', line 207

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

#set_up_capybara_for_rspecObject



74
75
76
# File 'lib/suspenders/app_builder.rb', line 74

def set_up_capybara_for_rspec
  template 'capybara_rspec.rb.erb', 'spec/support/capybara.rb', port_number: port
end

#set_up_factory_girl_for_rspecObject



70
71
72
# File 'lib/suspenders/app_builder.rb', line 70

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

#set_up_fakerObject



83
84
85
# File 'lib/suspenders/app_builder.rb', line 83

def set_up_faker
  copy_file 'faker_rspec.rb', 'spec/support/faker.rb'
end

#setup_asset_hostObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/suspenders/app_builder.rb', line 135

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("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



460
461
462
463
# File 'lib/suspenders/app_builder.rb', line 460

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_rake_taskObject



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/suspenders/app_builder.rb', line 502

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_foremanObject



332
333
334
335
# File 'lib/suspenders/app_builder.rb', line 332

def setup_foreman
  copy_file 'sample.env', '.sample.env'
  copy_file 'Procfile', 'Procfile'
end

#setup_heroku_specific_gemsObject



215
216
217
218
219
220
221
# File 'lib/suspenders/app_builder.rb', line 215

def setup_heroku_specific_gems
  inject_into_file(
    "Gemfile",
    %{\n\s\sgem "rails_stdout_logging"},
    after: /group :staging, :production do/
  )
end

#setup_secret_tokenObject



165
166
167
# File 'lib/suspenders/app_builder.rb', line 165

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

#setup_springObject



465
466
467
# File 'lib/suspenders/app_builder.rb', line 465

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

#setup_staging_environmentObject



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/suspenders/app_builder.rb', line 151

def setup_staging_environment
  staging_file = 'config/environments/staging.rb'
  copy_file 'staging.rb', staging_file

  config = <<-RUBY

Rails.application.configure do
  # ...
end
  RUBY

  append_file staging_file, config
end

#setup_stylesheetsObject



337
338
339
340
341
# File 'lib/suspenders/app_builder.rb', line 337

def setup_stylesheets
  remove_file 'app/assets/stylesheets/application.css'
  copy_file 'application.css.sass',
    'app/assets/stylesheets/application.css.sass'
end

#use_postgres_config_templateObject



193
194
195
196
# File 'lib/suspenders/app_builder.rb', line 193

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