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



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

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"

#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



292
293
294
# File 'lib/suspenders/app_builder.rb', line 292

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

#configure_active_jobObject



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

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

TODO Use TravisCI, because so much of our work is open-source.



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

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



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

def configure_background_jobs_for_rspec
  run 'rails g delayed_job:active_record'
end

#configure_capybara_webkitObject



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

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

#configure_generatorsObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/suspenders/app_builder.rb', line 101

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



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

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

#configure_i18n_for_test_environmentObject



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

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

#configure_newrelicObject



127
128
129
# File 'lib/suspenders/app_builder.rb', line 127

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

#configure_pumaObject



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

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

#configure_quiet_assetsObject



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

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



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

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



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

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_smtpObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/suspenders/app_builder.rb', line 131

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



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

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

#configure_time_formatsObject



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

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

#copy_dotfilesObject



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

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

#copy_miscellaneous_filesObject



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

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



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

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

#create_databaseObject



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

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

#create_github_repo(repo_name) ⇒ Object



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

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

#create_heroku_apps(flags) ⇒ Object



366
367
368
369
# File 'lib/suspenders/app_builder.rb', line 366

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

#create_partials_directoryObject



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

def create_partials_directory
  empty_directory 'app/views/application'
end

#create_shared_css_overridesObject



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

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

#create_shared_flashesObject



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

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



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

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

#customize_error_pagesObject



426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/suspenders/app_builder.rb', line 426

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



467
468
469
# File 'lib/suspenders/app_builder.rb', line 467

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

#disallow_wrapping_parametersObject



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

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

#enable_database_cleanerObject



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

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

#enable_rack_canonical_hostObject



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

def enable_rack_canonical_host
  config = "\n  if ENV.fetch(\"HEROKU_APP_NAME\", \"\").include?(\"staging-pr-\")\nENV[\"APPLICATION_HOST\"] = ENV[\"HEROKU_APP_NAME\"] + \".herokuapp.com\"\n  end\n\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: \"Rails.application.configure do\",\n  )\nend\n"

#enable_rack_deflaterObject



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

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"

#gemfileObject



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

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

#generate_factories_fileObject



123
124
125
# File 'lib/suspenders/app_builder.rb', line 123

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

#generate_rspecObject



326
327
328
# File 'lib/suspenders/app_builder.rb', line 326

def generate_rspec
  generate 'rspec:install'
end

#gitignoreObject



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

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

#init_gitObject



362
363
364
# File 'lib/suspenders/app_builder.rb', line 362

def init_git
  run 'git init'
end

#provide_deploy_scriptObject



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/suspenders/app_builder.rb', line 371

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



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

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

#provide_setup_scriptObject



92
93
94
95
# File 'lib/suspenders/app_builder.rb', line 92

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

#provide_shoulda_matchers_configObject



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

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

#raise_on_delivery_errorsObject



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

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



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

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



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

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



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

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

#remove_config_comment_linesObject



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/suspenders/app_builder.rb', line 439

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



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

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



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

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

#set_test_delivery_methodObject



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

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



119
120
121
# File 'lib/suspenders/app_builder.rb', line 119

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

#set_up_foregoObject



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

def set_up_forego
  copy_file "Procfile", "Procfile"
end

#setup_asset_hostObject



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

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



411
412
413
414
# File 'lib/suspenders/app_builder.rb', line 411

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



344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/suspenders/app_builder.rb', line 344

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



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

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

#setup_rack_mini_profilerObject



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

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

#setup_secret_tokenObject



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

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

#setup_segmentObject



406
407
408
409
# File 'lib/suspenders/app_builder.rb', line 406

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

#setup_springObject



416
417
418
# File 'lib/suspenders/app_builder.rb', line 416

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

#setup_staging_environmentObject



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

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



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

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

#use_postgres_config_templateObject



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

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