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_bullet_gem_configurationObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/suspenders/app_builder.rb', line 38

def add_bullet_gem_configuration
  config = "  config.after_initialize do\nBullet.enable = true\nBullet.bullet_logger = true\nBullet.rails_logger = true\n  end\n\n  RUBY\n\n  inject_into_file(\n    \"config/environments/development.rb\",\n    config,\n    after: \"config.action_mailer.raise_delivery_errors = true\\n\",\n  )\nend\n"

#add_helpers_for_rspecObject



102
103
104
105
# File 'lib/suspenders/app_builder.rb', line 102

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



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

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



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

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

#configure_active_jobObject



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

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



319
320
321
322
323
324
325
# File 'lib/suspenders/app_builder.rb', line 319

def configure_available_locales
  config = "config.i18n.available_locales = [:en, :it]\n  RUBY\n\n  inject_into_class 'config/application.rb', 'Application', config\nend\n"

#configure_background_jobs_for_rspecObject



272
273
274
# File 'lib/suspenders/app_builder.rb', line 272

def configure_background_jobs_for_rspec
  run 'rails g delayed_job:active_record'
end

#configure_capybara_webkitObject



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

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

#configure_generatorsObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/suspenders/app_builder.rb', line 80

def configure_generators
  config = "\nconfig.generators do |generate|\n  generate.helper false\n  generate.javascript_engine false\n  generate.request_specs false\n  generate.routing_specs false\n  generate.stylesheets false\n  generate.test_framework :rspec\n  generate.view_specs false\nend\n\n  RUBY\n\n  inject_into_class 'config/application.rb', 'Application', config\nend\n"

#configure_i18n_for_missing_translationsObject



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

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

#configure_i18n_for_test_environmentObject



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

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

#configure_i18n_tasksObject



267
268
269
270
# File 'lib/suspenders/app_builder.rb', line 267

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



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

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



284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/suspenders/app_builder.rb', line 284

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.i18n.default_locale = :de",
    "config.i18n.default_locale = :it"

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

#configure_pumaObject



346
347
348
# File 'lib/suspenders/app_builder.rb', line 346

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

#configure_quiet_assetsObject



63
64
65
66
67
68
69
# File 'lib/suspenders/app_builder.rb', line 63

def configure_quiet_assets
  config = "config.quiet_assets = true\n  RUBY\n\n  inject_into_class \"config/application.rb\", \"Application\", config\nend\n"

#configure_rack_timeoutObject



297
298
299
300
301
302
303
# File 'lib/suspenders/app_builder.rb', line 297

def configure_rack_timeout
  rack_timeout_config = "Rack::Timeout.timeout = (ENV[\"RACK_TIMEOUT\"] || 10).to_i\n  RUBY\n\n  append_file \"config/environments/production.rb\", rack_timeout_config\nend\n"

#configure_rspecObject



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

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



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

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

#configure_slimObject



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

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

#configure_smtpObject



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

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

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

  config = "\n  config.action_mailer.delivery_method = :smtp\n  config.action_mailer.smtp_settings = SMTP_SETTINGS\n  RUBY\n\n  inject_into_file 'config/environments/production.rb', config,\n    after: \"config.action_mailer.raise_delivery_errors = false\"\nend\n"

#configure_spec_support_featuresObject



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

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

#copy_dotfilesObject



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

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

#copy_miscellaneous_filesObject



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

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



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

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



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

def create_binstubs
  bundle_command "binstubs brakeman"
  bundle_command "binstubs rubocop"
  bundle_command "binstubs i18n-tasks"
end

#create_databaseObject



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

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

#create_github_repo(repo_name) ⇒ Object



437
438
439
# File 'lib/suspenders/app_builder.rb', line 437

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

#create_heroku_apps(flags) ⇒ Object



408
409
410
411
# File 'lib/suspenders/app_builder.rb', line 408

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

#create_partials_directoryObject



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

def create_partials_directory
  empty_directory 'app/views/application'
end

#create_production_heroku_app(flags) ⇒ Object



402
403
404
405
406
# File 'lib/suspenders/app_builder.rb', line 402

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



231
232
233
# File 'lib/suspenders/app_builder.rb', line 231

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

#create_shared_flashesObject



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

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



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

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

#create_staging_heroku_app(flags) ⇒ Object



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

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



470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/suspenders/app_builder.rb', line 470

def customize_error_pages
  meta_tags ="  <meta charset=\"utf-8\" />\n  <meta name=\"ROBOTS\" content=\"NOODP\" />\n  <meta name=\"viewport\" content=\"initial-scale=1\" />\n  EOS\n\n  %w(500 404 422).each do |page|\n    inject_into_file \"public/\#{page}.html\", meta_tags, after: \"<head>\\n\"\n    replace_in_file \"public/\#{page}.html\", /<!--.+-->\\n/, ''\n  end\nend\n"

#disable_xml_paramsObject



511
512
513
# File 'lib/suspenders/app_builder.rb', line 511

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

#disallow_wrapping_parametersObject



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

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

#enable_database_cleanerObject



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

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

#enable_rack_canonical_hostObject



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/suspenders/app_builder.rb', line 127

def enable_rack_canonical_host
  config = "\n  # Ensure requests are only served from one, canonical host name\n  config.middleware.use Rack::CanonicalHost, ENV.fetch(\"APPLICATION_HOST\")\n  RUBY\n\n  inject_into_file(\n    \"config/environments/production.rb\",\n    config,\n    after: serve_static_files_line\n  )\nend\n"

#enable_rack_deflaterObject



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/suspenders/app_builder.rb', line 141

def enable_rack_deflater
  config = "\n  # Enable deflate / gzip compression of controller-generated responses\n  config.middleware.use Rack::Deflater\n  RUBY\n\n  inject_into_file(\n    \"config/environments/production.rb\",\n    config,\n    after: serve_static_files_line\n  )\nend\n"

#fix_i18n_deprecation_warningObject



334
335
336
337
338
339
340
# File 'lib/suspenders/app_builder.rb', line 334

def fix_i18n_deprecation_warning
  config = "config.i18n.enforce_available_locales = true\n  RUBY\n\n  inject_into_class 'config/application.rb', 'Application', config\nend\n"

#generate_rspecObject



342
343
344
# File 'lib/suspenders/app_builder.rb', line 342

def generate_rspec
  generate 'rspec:install'
end

#gitignore_filesObject



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/suspenders/app_builder.rb', line 360

def gitignore_files
  remove_file '.gitignore'
  copy_file 'suspenders_gitignore', '.gitignore'
  [
    'app/forms',
    'app/decorators',
    'app/queries',
    'app/services',
    'app/views/pages',
    'spec/controllers',
    'spec/forms',
    'spec/helpers',
    'spec/lib',
    'spec/decorators',
    '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



390
391
392
# File 'lib/suspenders/app_builder.rb', line 390

def init_git
  run 'git init'
end

#provide_deploy_scriptObject



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/suspenders/app_builder.rb', line 419

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

  instructions = "\n## Deploying\n\nIf you have previously run the `./bin/setup` script,\nyou can deploy to staging and production with:\n\n$ ./bin/deploy staging\n$ ./bin/deploy production\n  MARKDOWN\n\n  append_file \"README.md\", instructions\n  run \"chmod a+x bin/deploy\"\nend\n"

#provide_dev_prime_taskObject



76
77
78
# File 'lib/suspenders/app_builder.rb', line 76

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

#provide_setup_scriptObject



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

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

#provide_shoulda_matchers_configObject



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

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

#raise_on_delivery_errorsObject



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

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



17
18
19
20
21
22
23
# File 'lib/suspenders/app_builder.rb', line 17

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



55
56
57
58
59
60
61
# File 'lib/suspenders/app_builder.rb', line 55

def raise_on_unpermitted_parameters
  config = "config.action_controller.action_on_unpermitted_parameters = :raise\n  RUBY\n\n  inject_into_class \"config/application.rb\", \"Application\", config\nend\n"

#readmeObject



13
14
15
# File 'lib/suspenders/app_builder.rb', line 13

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

#remove_config_comment_linesObject



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/suspenders/app_builder.rb', line 483

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



505
506
507
508
509
# File 'lib/suspenders/app_builder.rb', line 505

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



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

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

#set_heroku_serve_static_filesObject



413
414
415
416
417
# File 'lib/suspenders/app_builder.rb', line 413

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



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

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

#set_up_factory_girl_for_rspecObject



98
99
100
# File 'lib/suspenders/app_builder.rb', line 98

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

#set_up_fakerObject



107
108
109
# File 'lib/suspenders/app_builder.rb', line 107

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

#set_up_foregoObject



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

def set_up_forego
  copy_file "Procfile", "Procfile"
end

#setup_asset_hostObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/suspenders/app_builder.rb', line 155

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_brakemanObject



445
446
447
# File 'lib/suspenders/app_builder.rb', line 445

def setup_brakeman
  copy_file "brakeman.rake", "lib/tasks/brakeman.rake"
end

#setup_bundler_auditObject



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

def setup_bundler_audit
  copy_file "bundler_audit.rake", "lib/tasks/bundler_audit.rake"
end

#setup_default_rake_taskObject



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/suspenders/app_builder.rb', line 515

def setup_default_rake_task
  append_file 'Rakefile' do
    "task(:default).clear\n\nif defined? RSpec\n  task(:spec).clear\n  RSpec::Core::RakeTask.new(:spec) do |t|\nt.verbose = false\n  end\nend\n\ntask default: :rubocop\ntask default: :spec\ntask default: \"brakeman:check\"\ntask default: \"bundler:audit\"\n    EOS\n  end\nend\n"

#setup_rubocopObject



449
450
451
452
# File 'lib/suspenders/app_builder.rb', line 449

def setup_rubocop
  copy_file "rubocop.rake", "lib/tasks/rubocop.rake"
  copy_file "rubocop.yml", ".rubocop.yml"
end

#setup_secret_tokenObject



185
186
187
# File 'lib/suspenders/app_builder.rb', line 185

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

#setup_springObject



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

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

#setup_staging_environmentObject



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/suspenders/app_builder.rb', line 171

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

  config = "\nRails.application.configure do\n  # ...\nend\n  RUBY\n\n  append_file staging_file, config\nend\n"

#setup_stylesheetsObject



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

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

#use_postgres_config_templateObject



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

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