Class: Suspenders::AppBuilder

Inherits:
Rails::AppBuilder
  • Object
show all
Extended by:
Forwardable
Includes:
Actions
Defined in:
lib/voyage/app_builder.rb,
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

#accept_defaultsObject



10
11
12
13
14
15
16
# File 'lib/voyage/app_builder.rb', line 10

def accept_defaults
  if agree?('Would you like to accept all defaults? [slim, devise w/ first & last name, refills nav & footer] (Y/n)')
    @@accept_defaults = true
  else
    @@accept_defaults = false
  end
end

#add_auto_annotate_models_rake_taskObject



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

def add_auto_annotate_models_rake_task
  template '../templates/auto_annotate_models.rake', 'lib/tasks/auto_annotate_models.rake', force: true
end

#add_bullet_gem_configurationObject



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

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_canard_roles_to_devise_resourceObject



236
237
238
239
240
241
242
243
244
245
246
# File 'lib/voyage/app_builder.rb', line 236

def add_canard_roles_to_devise_resource
  inject_into_file 'app/models/user.rb', before: /^end/ do "\n    # Permissions cascade/inherit through the roles listed below. The order of\n    # this list is important, it should progress from least to most privelage\n    ROLES = [:admin].freeze\n    acts_as_user roles: ROLES\n    roles ROLES\n    RUBY\n  end\nend\n".gsub(/^ {6}/, '')

#add_high_voltage_static_pagesObject



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/voyage/app_builder.rb', line 338

def add_high_voltage_static_pages
  template '../templates/about.html.erb', "app/views/pages/about.html.#{@@use_slim ? 'slim' : 'erb'}"
  template '../templates/welcome.html.erb', "app/views/pages/welcome.html.erb"

  inject_into_file 'config/routes.rb', before: /^end/ do "    root 'high_voltage/pages#show', id: 'welcome'\n    RUBY\n  end\n\n  create_file 'config/initializers/high_voltage.rb' do <<-RUBY.gsub(/^ {8}/, '')\n    HighVoltage.configure do |config|\n      config.route_drawer = HighVoltage::RouteDrawers::Root\n    end\n    RUBY\n  end\nend\n".gsub(/^ {6}/, '')

#add_refills_to_layoutObject



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/voyage/app_builder.rb', line 405

def add_refills_to_layout
  if @@use_slim
    inject_into_file 'app/views/layouts/application.html.slim', before: '    = yield' do "        = render 'refills/navigation'\n        RUBY\n    end\n    inject_into_file 'app/views/layouts/application.html.slim', before: '    = render \"javascript\"' do <<-RUBY.gsub(/^ {8}/, '')\n        = render 'refills/footer'\n        RUBY\n    end\n  else\n    inject_into_file 'app/views/layouts/application.html.erb', before: '  <%= yield %>' do <<-RUBY.gsub(/^ {8}/, '')\n      <%= render 'refills/navigation' %>\n      RUBY\n    end\n    inject_into_file 'app/views/layouts/application.html.erb', before: '  <%= render \"javascript\" %>' do <<-RUBY.gsub(/^ {8}/, '')\n      <%= render 'refills/footer' %>\n      RUBY\n    end\n  end\nend\n".gsub(/^ {8}/, '')

#add_refills_to_stylesheetsObject



427
428
429
430
431
432
433
# File 'lib/voyage/app_builder.rb', line 427

def add_refills_to_stylesheets
  inject_into_file 'app/assets/stylesheets/application.scss', after: '@import "refills/flashes";'  do "    \\n@import \"refills/navigation\";\n    @import \"refills/footer\";\n    RUBY\n  end\nend\n".gsub(/^ {8}/, '')

#add_rubocop_configObject



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

def add_rubocop_config
  template '../templates/rubocop.yml', '.rubocop.yml', force: true
end

#add_sign_in_and_sign_out_routes_for_deviseObject



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/voyage/app_builder.rb', line 257

def 
  inject_into_file 'config/routes.rb', before: /^end/ do "    authenticated :user do\n      # root to: 'dashboard#show', as: :authenticated_root\n    end\n\n    devise_scope :user do\n      get 'sign-in',  to: 'devise/sessions#new'\n      get 'sign-out', to: 'devise/sessions#destroy'\n    end\n    RUBY\n  end\nend\n".gsub(/^ {6}/, '')

#add_to_gitignoreObject



495
496
497
498
499
500
501
502
503
# File 'lib/voyage/app_builder.rb', line 495

def add_to_gitignore
  inject_into_file '.gitignore', after: '/tmp/*' do "\n    .env\n    .zenflow-log\n    errors.err\n    RUBY\n  end\nend\n".gsub(/^ {8}/, '')

#add_views_for_devise_resource(adding_first_and_last_name) ⇒ Object



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

def add_views_for_devise_resource(adding_first_and_last_name)
  config = { adding_first_and_last_name: adding_first_and_last_name }
  template '../templates/users_index.html.slim.erb', 'app/views/users/index.html.slim', config
end

#agree?(prompt) ⇒ Boolean



3
4
5
6
7
8
# File 'lib/voyage/app_builder.rb', line 3

def agree?(prompt)
  puts prompt
  response = STDIN.gets.chomp

  response.empty? || %w(y yes).include?(response.downcase.strip)
end

#authorize_devise_resource_for_index_actionObject



224
225
226
227
228
229
230
231
232
233
234
# File 'lib/voyage/app_builder.rb', line 224

def authorize_devise_resource_for_index_action
  generate 'canard:ability user can:manage:user cannot:destroy:user'
  generate 'canard:ability admin can:destroy:user'

  %w(admins users).each do |resource_name|
    replace_in_file "spec/abilities/#{resource_name}_spec.rb", "require 'cancan/matchers'", "require_relative '../support/matchers/custom_cancan'"
  end

  generate 'migration add_roles_mask_to_users roles_mask:integer'
  template '../templates/custom_cancan_matchers.rb', 'spec/support/matchers/custom_cancan.rb'
end

#configure_action_mailerObject



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

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



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

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

#configure_active_jobObject



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

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



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

def configure_automatic_deployment
  deploy_command = "  deployment:\n    staging:\n      branch: master\n      commands:\n        - bin/deploy staging\n  YML\n\n  append_file \"circle.yml\", deploy_command\nend\n".strip_heredoc

#configure_background_jobs_for_rspecObject



541
542
543
# File 'lib/voyage/app_builder.rb', line 541

def configure_background_jobs_for_rspec
  run 'rails g delayed_job:active_record'
end

#configure_capybara_webkitObject



545
546
547
# File 'lib/voyage/app_builder.rb', line 545

def configure_capybara_webkit
  # NOTE: (2016-02-03) jonk => don't want this
end

#configure_ciObject



537
538
539
# File 'lib/voyage/app_builder.rb', line 537

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

#configure_generatorsObject

OVERRIDE SUSPENDERS METHODS #



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/voyage/app_builder.rb', line 508

def configure_generators
  config = "    config.generators do |g|\n      g.helper false\n      g.javascript_engine false\n      g.request_specs false\n      g.routing_specs false\n      g.stylesheets false\n      g.test_framework :rspec\n      g.view_specs false\n      g.fixture_replacement :factory_girl, dir: 'spec/factories'\n      g.template_engine :slim\n    end\n  RUBY\n\n  inject_into_class 'config/application.rb', 'Application', config\nend\n".gsub(/^ {4}/, '')

#configure_i18n_for_missing_translationsObject



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

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

#configure_i18n_for_test_environmentObject



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

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

#configure_quiet_assetsObject



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

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

#configure_rack_timeoutObject



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

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



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

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_rvm_prepend_bin_to_pathObject



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

def configure_rvm_prepend_bin_to_path
  run "rm -f $rvm_path/hooks/after_cd_bundler"

  run "touch $rvm_path/hooks/after_cd_bundler"

  git_safe_dir = "    #!/usr/bin/env bash\n    export PATH=\".git/safe/../../bin:$PATH\"\n    RUBY\n\n  run \"echo '\#{git_safe_dir}' >> $rvm_path/hooks/after_cd_bundler\"\n\n  run 'chmod +x $rvm_path/hooks/after_cd_bundler'\n\n  run 'mkdir -p .git/safe'\nend\n".gsub(/^ {8}/, '')

#configure_simple_formObject



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

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

#configure_smtpObject



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

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



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

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

#configure_time_formatsObject



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

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

#convert_refill_viewsObject



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/voyage/app_builder.rb', line 384

def convert_refill_views
  inside('lib') do # arbitrary, run in context of newly generated app
    run "erb2slim '../app/views/refills' '../app/views/refills'"
    run "erb2slim -d '../app/views/refills'"
  end

  find = "    |  <div class=\"flash-\n    = key\n    | \">\n    = value\n  RUBY\n\n  replace = <<-RUBY.gsub(/^ {2}/, '')\n    div class=\"flash-\\\#{key}\"\n      = value\n  RUBY\n\n  replace_in_file 'app/views/application/_flashes.html.slim', find, replace\nend\n".gsub(/^ {2}/, '')

#copy_dotfilesObject



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

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

#copy_env_to_exampleObject



491
492
493
# File 'lib/voyage/app_builder.rb', line 491

def copy_env_to_example
  run 'cp .env .env.example'
end

#copy_miscellaneous_filesObject



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

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



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

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

#create_databaseObject



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

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

#create_deploy_scriptObject



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

def create_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"

#create_github_repo(repo_name) ⇒ Object



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

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

#create_heroku_apps(flags) ⇒ Object



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

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

#create_partials_directoryObject



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

def create_partials_directory
  empty_directory 'app/views/application'
end

#create_shared_css_overridesObject



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

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

#create_shared_flashesObject



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

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



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

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

#customize_application_controller_for_devise(adding_first_and_last_name) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/voyage/app_builder.rb', line 155

def customize_application_controller_for_devise(adding_first_and_last_name)
  inject_into_file 'app/controllers/application_controller.rb', after: "  protect_from_forgery with: :exception" do "\n    before_action :configure_permitted_parameters, if: :devise_controller?\n\n    protected\n\n    # rubocop:disable Metrics/MethodLength\n    def configure_permitted_parameters\n      devise_parameter_sanitizer.permit(\n        :sign_up,\n        keys: [\n          \#{':first_name,' if adding_first_and_last_name}\n          \#{':last_name,' if adding_first_and_last_name}\n          :email,\n          :password,\n          :password_confirmation,\n          :remember_me,\n        ],\n      )\n\n      devise_parameter_sanitizer.permit(\n        :sign_in,\n        keys: [\n          :login, :email, :password, :remember_me\n        ],\n      )\n\n      devise_parameter_sanitizer.permit(\n        :account_update,\n        keys: [\n          \#{':first_name,' if adding_first_and_last_name}\n          \#{':last_name,' if adding_first_and_last_name}\n          :email,\n          :password,\n          :password_confirmation,\n          :current_password,\n        ],\n      )\n    end\n    # rubocop:enable Metrics/MethodLength\n    RUBY\n  end\nend\n".gsub(/^ {6}/, '')

#customize_application_jsObject



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

def customize_application_js
  template '../templates/application.js', 'app/assets/javascripts/application.js', force: true
end

#customize_devise_viewsObject



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

def customize_devise_views
  %w(edit new).each do |file|
    if @@use_slim
      file_path = "app/views/devise/registrations/#{file}.html.slim"
      inject_into_file file_path, before: "    = f.input :email, required: true, autofocus: true" do "        = f.input :first_name, required: true, autofocus: true\n        = f.input :last_name, required: true\n        RUBY\n      end\n    else\n      file_path = \"app/views/devise/registrations/\#{file}.html.erb\"\n      inject_into_file file_path, before: \"    <%= f.input :email, required: true, autofocus: true %>\" do <<-'RUBY'.gsub(/^ {8}/, '')\n        <%= f.input :first_name, required: true, autofocus: true %>\n        <%= f.input :last_name, required: true %>\n        RUBY\n      end\n    end\n  end\nend\n".gsub(/^ {8}/, '')

#customize_error_pagesObject



417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/suspenders/app_builder.rb', line 417

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"

#customize_resource_controller_for_devise(adding_first_and_last_name) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/voyage/app_builder.rb', line 200

def customize_resource_controller_for_devise(adding_first_and_last_name)
  bundle_command 'exec rails generate controller users'
  run 'rm spec/controllers/users_controller_spec.rb'

  inject_into_class "app/controllers/users_controller.rb", "UsersController" do "    # https://github.com/CanCanCommunity/cancancan/wiki/authorizing-controller-actions\n    load_and_authorize_resource only: [:index, :show]\n    RUBY\n  end\n\n  unless adding_first_and_last_name\n    inject_into_file 'config/routes.rb', after: '  devise_for :users' do <<-RUBY.gsub(/^ {8}/, '')\n      \\n\n      resources :users\n      RUBY\n    end\n  end\nend\n".gsub(/^ {6}/, '')

#customize_user_factory(adding_first_and_last_name) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/voyage/app_builder.rb', line 271

def customize_user_factory(adding_first_and_last_name)
  inject_into_file 'spec/factories/users.rb', before: /^  end/ do "    password 'password'\n    sequence(:email) { |n| \"user_\#{n}@example.com\" }\n\n    trait :admin do\n      roles [:admin]\n      email '[email protected]'\n    end\n    RUBY\n  end\n\n  if adding_first_and_last_name\n    inject_into_file 'spec/factories/users.rb', after: /roles \\[:admin\\]\\n/ do <<-'RUBY'.gsub(/^ {4}/, '')\n      first_name 'Admin'\n      last_name 'User'\n      RUBY\n    end\n  end\nend\n".gsub(/^ {4}/, '')

#customize_user_specObject



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/voyage/app_builder.rb', line 301

def customize_user_spec
  find = "    pending \"add some examples to (or delete) \\\#{__FILE__}\"\n  RUBY\n\n  replace = <<-RUBY.gsub(/^ {6}/, '')\n    describe 'constants' do\n      context 'roles' do\n        it 'has the admin role' do\n          expect(User::ROLES).to eq([:admin])\n        end\n      end\n    end\n  RUBY\n\n  replace_in_file 'spec/models/user_spec.rb', find, replace\nend\n".gsub(/^ {6}/, '')

#disallow_wrapping_parametersObject



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

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

#downgrade_neat_1_8_so_refills_media_mixin_worksObject


TEMP FIX

https://github.com/thoughtbot/bourbon/issues/993
https://github.com/thoughtbot/refills/issues/400



360
361
362
363
364
# File 'lib/voyage/app_builder.rb', line 360

def downgrade_neat_1_8_so_refills_media_mixin_works
  replace_in_file 'Gemfile', "gem 'neat', '~> 2.0.0.beta.1'", "gem 'neat', '~> 1.8.0'"
  run 'gem uninstall -x neat -v2.0.0.beta.2'
  run 'bundle'
end

#enable_database_cleanerObject



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

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

#enable_rack_canonical_hostObject



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

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



167
168
169
# File 'lib/suspenders/app_builder.rb', line 167

def enable_rack_deflater
  configure_environment "production", "config.middleware.use Rack::Deflater"
end

#gemfileObject



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

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

#generate_data_migrationsObject



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

def generate_data_migrations
  generate 'data_migrations:install'

  empty_directory_with_keep_file 'db/data_migrate'
end

#generate_factories_fileObject


setup_test_environment overrides




533
534
535
# File 'lib/voyage/app_builder.rb', line 533

def generate_factories_file
  # NOTE: (2016-02-03) jonk => don't want this, we use individual factories
end

#generate_refillsObject


ADDING REFILLS COMPONENTS




373
374
375
376
377
378
379
380
381
382
# File 'lib/voyage/app_builder.rb', line 373

def generate_refills
  if @@accept_defaults || agree?('Would you like to install default Refill components? (Y/n)')
    bundle_command 'exec rails generate refills:import navigation'
    bundle_command 'exec rails generate refills:import footer'

    convert_refill_views if @@use_slim
    add_refills_to_layout
    add_refills_to_stylesheets
  end
end

#generate_rspecObject



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

def generate_rspec
  generate 'rspec:install'
end

#generate_ruby_version_and_gemsetObject



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

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

#generate_seeder_templates(using_devise:) ⇒ Object


END DEVISE SETUP




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

def generate_seeder_templates(using_devise:)
  config = { force: true, using_devise: using_devise }
  template '../templates/seeder.rb.erb', 'lib/seeder.rb', config
  template '../templates/seeds.rb.erb', 'db/seeds.rb', config
end

#generate_test_environmentObject


END ADDING REFILLS COMPONENTS




438
439
440
441
# File 'lib/voyage/app_builder.rb', line 438

def generate_test_environment
  template '../templates/controller_helpers.rb', 'spec/support/controller_helpers.rb'
  template '../templates/simplecov.rb', '.simplecov'
end

#gitignoreObject



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

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

#init_gitObject



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

def init_git
  run 'git init'
end

#install_deviseObject


DEVISE SETUP




94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/voyage/app_builder.rb', line 94

def install_devise
  if @@accept_defaults || agree?('Would you like to install Devise? (Y/n)')
    bundle_command 'exec rails generate devise:install'

    if @@accept_defaults || agree?("Would you like to add first_name and last_name to the devise model? (Y/n)")
      adding_first_and_last_name = true

      bundle_command "exec rails generate resource user first_name:string last_name:string"

      replace_in_file 'spec/factories/users.rb',
        'first_name "MyString"', 'first_name { Faker::Name.first_name }'
      replace_in_file 'spec/factories/users.rb',
        'last_name "MyString"', 'last_name { Faker::Name.last_name }'
    end

    bundle_command "exec rails generate devise user"
    bundle_command 'exec rails generate devise:views'

    if @@use_slim
      inside('lib') do # arbitrary, run in context of newly generated app
        run "erb2slim '../app/views/devise' '../app/views/devise'"
        run "erb2slim -d '../app/views/devise'"
      end
    end

    customize_devise_views if adding_first_and_last_name
    customize_application_controller_for_devise(adding_first_and_last_name)
    customize_resource_controller_for_devise(adding_first_and_last_name)
    add_views_for_devise_resource(adding_first_and_last_name)
    authorize_devise_resource_for_index_action
    add_canard_roles_to_devise_resource
    update_devise_initializer
    
    customize_user_factory(adding_first_and_last_name)
    generate_seeder_templates(using_devise: true)
    customize_user_spec
  else
    generate_seeder_templates(using_devise: false)
  end
end

#provide_dev_prime_taskObject



100
101
102
# File 'lib/suspenders/app_builder.rb', line 100

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

#provide_setup_scriptObject



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

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

#provide_shoulda_matchers_configObject



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

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

#raise_on_delivery_errorsObject



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

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



37
38
39
# File 'lib/suspenders/app_builder.rb', line 37

def raise_on_missing_assets_in_test
  configure_environment "test", "config.assets.raise_runtime_errors = true"
end

#raise_on_unpermitted_parametersObject



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

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"

#rake_db_setupObject

Do this last



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

def rake_db_setup
  rake 'db:migrate'
  rake 'db:seed' if File.exist?('config/initializers/devise.rb')
end

#readmeObject



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

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

#remove_config_comment_linesObject


End setup_test_environment overrides




552
553
554
# File 'lib/voyage/app_builder.rb', line 552

def remove_config_comment_lines
  # NOTE: (2016-02-09) jonk => don't want this
end

#remove_routes_comment_linesObject



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

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


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

def remove_turbolinks
  replace_in_file(
    "app/assets/javascripts/application.js",
    "//= require turbolinks",
    ""
  )
end

#replace_default_puma_configurationObject



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

def replace_default_puma_configuration
  copy_file "puma.rb", "config/puma.rb", force: true
end

#replace_gemfile(path) ⇒ Object



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

def replace_gemfile(path)
  template 'Gemfile.erb', 'Gemfile', force: true do |content|
    if path
      content.gsub(%r{gem .suspenders.}) { |s| %{#{s}, path: "#{path}"} }
    else
      content
    end
  end
end

#require_files_in_libObject



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

def require_files_in_lib
  create_file 'config/initializers/require_files_in_lib.rb',
    "Dir[File.join(Rails.root, 'lib', '**', '*.rb')].each { |l| require l }\n"
end

#run_rubocop_auto_correctObject



487
488
489
# File 'lib/voyage/app_builder.rb', line 487

def run_rubocop_auto_correct
  run 'rubocop --auto-correct'
end

#set_ruby_to_version_being_usedObject



526
527
528
# File 'lib/voyage/app_builder.rb', line 526

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

#set_test_delivery_methodObject



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

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



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

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

#set_up_foregoObject



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

def set_up_forego
  copy_file "Procfile", "Procfile"
end

#set_up_houndObject



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

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

#setup_asset_hostObject



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

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")'

  config = "config.public_file_server.headers = {\n\"Cache-Control\" => \"public, max-age=31557600\",\n  }\n  EOD\n\n  configure_environment(\"production\", config)\nend\n"

#setup_bundler_auditObject



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

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



336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/suspenders/app_builder.rb', line 336

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



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

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



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

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

#setup_secret_tokenObject



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

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

#setup_segmentObject



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

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

#setup_springObject



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

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

#update_application_layout_for_slimObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/voyage/app_builder.rb', line 32

def update_application_layout_for_slim
  find = "    <%#\n      Configure default and controller-, and view-specific titles in\n      config/locales/en.yml. For more see:\n      https://github.com/calebthompson/title#usage\n    %>\n  RUBY\n\n  replace = <<-RUBY.gsub(/^ {6}/, '')\n    <% # Configure default and controller-, and view-specific titles in\n  # config/locales/en.yml. For more see:\n  # https://github.com/calebthompson/title#usage %>\n  RUBY\n\n  replace_in_file 'app/views/layouts/application.html.erb', find, replace\n\n  inside('lib') do # arbitrary, run in context of newly generated app\n    run \"erb2slim '../app/views/layouts' '../app/views/layouts'\"\n    run \"erb2slim -d '../app/views/layouts'\"\n\n    run \"erb2slim '../app/views/application' '../app/views/application'\"\n    run \"erb2slim -d '../app/views/application'\"\n  end\n\n  # strip trailing space after closing \"> in application layout before\n  # trying to find and replace it\n  replace_in_file 'app/views/layouts/application.html.slim', '| \"> ', '| \">'\n\n  find = <<-RUBY.gsub(/^ {6}/, '')\n    |  <body class=\"\n    = body_class\n    | \">\n  RUBY\n\n  replace = <<-RUBY.gsub(/^ {6}/, '')\n    body class=\"\\\#{body_class}\"\n  RUBY\n\n  replace_in_file 'app/views/layouts/application.html.slim', find, replace\n\n  find = <<-RUBY.gsub(/^ {6}/, '')\n    = render \"flashes\"\n    = yield\n    = render \"javascript\"\n    = render \"css_overrides\"\n  RUBY\n\n  # Bump renders in to be nested within body\n  replace = <<-RUBY.gsub(/^ {6}/, '')\n      = render \"flashes\"\n      = yield\n      = render \"javascript\"\n      = render \"css_overrides\"\n  RUBY\n\n  replace_in_file 'app/views/layouts/application.html.slim', find, replace\nend\n".gsub(/^ {6}/, '')

#update_devise_initializerObject



248
249
250
251
252
253
254
255
# File 'lib/voyage/app_builder.rb', line 248

def update_devise_initializer
  replace_in_file 'config/initializers/devise.rb',
    'config.sign_out_via = :delete', 'config.sign_out_via = :get'

  replace_in_file 'config/initializers/devise.rb',
    "config.mailer_sender = '[email protected]'",
    "config.mailer_sender = '[email protected]'"
end

#update_gemset_in_gemfileObject



18
19
20
# File 'lib/voyage/app_builder.rb', line 18

def update_gemset_in_gemfile
  replace_in_file 'Gemfile', '#ruby-gemset', "#ruby-gemset=#{app_name}"
end

#update_test_environmentObject



443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/voyage/app_builder.rb', line 443

def update_test_environment
  inject_into_file 'spec/support/factory_girl.rb', before: /^end/ do "\n    # Spring doesn't reload factory_girl\n    config.before(:all) do\n      FactoryGirl.reload\n    end\n    RUBY\n  end\n\n  template \"../templates/rails_helper.rb.erb\", \"spec/rails_helper.rb\", force: true\nend\n".gsub(/^ {6}/, '')

#use_postgres_config_templateObject



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

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

#use_slimObject



22
23
24
25
26
27
28
29
30
# File 'lib/voyage/app_builder.rb', line 22

def use_slim
  if @@accept_defaults || agree?('Would you like to use slim? (Y/n)')
    @@use_slim = true
    run 'gem install html2slim'
    update_application_layout_for_slim
  else
    @@use_slim = false
  end
end