Class: Panacea::Rails::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/panacea/rails/generator.rb

Overview

Panacea::Rails::Generator

This class is in charge of grouping all the actions to be executed via Panacea::Rails::Template

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_generator, panacea_config, root_dir) ⇒ Generator

This methods receive the context of the Rails::Generators::AppGenerator So it executes all methods against it.

It also receives the Panacea’s Config Hash and the Panacea’s Gem Root dir in order to update Rails::Generators::AppGenerator source paths.



30
31
32
33
34
35
# File 'lib/panacea/rails/generator.rb', line 30

def initialize(app_generator, panacea_config, root_dir)
  @app_generator = app_generator
  @config = panacea_config
  @app_generator.instance_variable_set :@panacea, @config
  @root_dir = root_dir
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Send any unknown method to the Rails::Generators::AppGenerator context. That context also knows Thor actions.



40
41
42
43
# File 'lib/panacea/rails/generator.rb', line 40

def method_missing(method_name, *args, &block)
  super unless app_generator.respond_to?(method_name)
  app_generator.send(method_name, *args, &block)
end

Instance Attribute Details

#app_generatorObject (readonly)

The Rails::Generators::AppGenerator context



14
15
16
# File 'lib/panacea/rails/generator.rb', line 14

def app_generator
  @app_generator
end

#configObject (readonly)

The Panacea’s Configuration Hash



22
23
24
# File 'lib/panacea/rails/generator.rb', line 22

def config
  @config
end

#root_dirObject (readonly)

A String with Panacea’s installation directory



18
19
20
# File 'lib/panacea/rails/generator.rb', line 18

def root_dir
  @root_dir
end

Instance Method Details

#after_bundle_hookObject

All the methods passed via block are executed on the Rails::Generators::AppGenerator after_bundle hook.



61
62
63
64
65
66
# File 'lib/panacea/rails/generator.rb', line 61

def after_bundle_hook
  after_bundle do
    run "spring stop"
    yield self
  end
end

#bye_messageObject

Display good bye message.



345
346
347
348
# File 'lib/panacea/rails/generator.rb', line 345

def bye_message
  message = "Panacea's work is done, enjoy!"
  say "\n\n\e[34m#{message}\e[0m"
end

#commit!Object

Commit only if end users want to.



338
339
340
341
# File 'lib/panacea/rails/generator.rb', line 338

def commit!
  git add: "."
  git commit: "-m '#{config.dig('commit_msg')}'"
end

#copy_gemfileObject

Update the Gemfile



77
78
79
# File 'lib/panacea/rails/generator.rb', line 77

def copy_gemfile
  template "templates/Gemfile.tt", "Gemfile", force: true
end

#copy_readmeObject

Update the README.md



83
84
85
# File 'lib/panacea/rails/generator.rb', line 83

def copy_readme
  template "templates/README.tt", "README.md", force: true
end

#create_databaseObject

Create database



207
208
209
# File 'lib/panacea/rails/generator.rb', line 207

def create_database
  rails_command "db:create"
end

#fix_offenses!Object

This needs to be run before commiting.

Fix existing application’s style offenses.



332
333
334
# File 'lib/panacea/rails/generator.rb', line 332

def fix_offenses!
  run "rubocop -a --format=simple"
end

#generate_panacea_documentObject

Creates the PANACEA.md file



89
90
91
# File 'lib/panacea/rails/generator.rb', line 89

def generate_panacea_document
  template "templates/PANACEA.tt", "PANACEA.md"
end

#override_application_system_testObject

Setup Headless Chrome Driver based on chosen test suite.



133
134
135
136
# File 'lib/panacea/rails/generator.rb', line 133

def override_application_system_test
  return unless config.dig("test_suite") == "minitest"
  template "templates/application_system_test.tt", "test/application_system_test_case.rb", force: true
end

#override_test_helperObject

Override test helper based on the chosen test suite.



123
124
125
126
127
128
129
# File 'lib/panacea/rails/generator.rb', line 123

def override_test_helper
  if config.dig("test_suite") == "minitest"
    template "templates/minitest_test_helper.tt", "test/test_helper.rb", force: true
  else
    template "templates/rspec_test_helper.tt", "spec/rails_helper.rb", force: true
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Whitelist of Rails::Generator::AppGenerator and Thor methods.

Add here any new method to be used from Thor / Rails Generator

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
# File 'lib/panacea/rails/generator.rb', line 49

def respond_to_missing?(method_name, include_private = false)
  %i[
    after_bundle generate rails_command template
    run git source_paths empty_directory append_to_file
    environment application say inject_into_class
    inject_into_file directory
  ].include?(method_name) || super
end

#setup_background_jobObject

Setup chosen Background Job gem.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/panacea/rails/generator.rb', line 153

def setup_background_job
  background_job = config.dig("background_job")

  application nil do
    <<~CONFS
      # Default adapter queue
      config.active_job.queue_adapter = :#{background_job}

    CONFS
  end

  if background_job == "sidekiq"
    route "mount Sidekiq::Web => '/sidekiq'"
    route "require 'sidekiq/web'"
  elsif background_job == "resque"
    route "mount Resque::Server, at: '/jobs'"
    route "require 'resque/server'"

    template "templates/Rakefile.tt", "Rakefile", force: true
  end
end

#setup_bootswatchObject

Setup booswatch-rails gem.



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/panacea/rails/generator.rb', line 261

def setup_bootswatch
  run "rm app/assets/stylesheets/application.css"
  template "templates/bootswatch/stylesheets/application.scss.tt", "app/assets/stylesheets/application.scss"

  inject_into_file "app/assets/javascripts/application.js", after: "//= require turbolinks" do
    <<~CONFS

      // Requires for Bootswatch
      //= require jquery
      //= require bootstrap-sprockets
    CONFS
  end

  run "rm app/views/layouts/application.html.erb"

  template "templates/bootswatch/views/shared/_navbar.html.haml", "app/views/shared/_navbar.html.haml"
  template "templates/bootswatch/views/shared/_flash_messages.html.haml", "app/views/shared/_flash_messages.html.haml"
  template "templates/bootswatch/views/layouts/application.html.haml", "app/views/layouts/application.html.haml", force: true

  generate "controller home index"
  inject_into_file "config/routes.rb", "\nroot to: 'home#index'", after: "Rails.application.routes.draw do"

  directory "templates/devise/views/", "app/views/devise/", force: true if config.dig("devise_override_views")
end

#setup_bulletObject

Setup Bullet gem



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/panacea/rails/generator.rb', line 213

def setup_bullet
  environment nil, env: "development" do
    <<~CONFS
      # Settings for Bullet gem
      config.after_initialize do
        Bullet.enable        = true
        Bullet.alert         = true
        Bullet.bullet_logger = true
        Bullet.console       = true
        Bullet.rails_logger  = true
        Bullet.add_footer    = true
      end

    CONFS
  end
end

#setup_default_localeObject

Setup the application’s locale.



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/panacea/rails/generator.rb', line 191

def setup_default_locale
  locale = config.dig("locale").split("- ").last

  application nil do
    <<~CONFS
      # Default i18n locale
      config.i18n.default_locale = :#{locale}

    CONFS
  end

  template "templates/default_locale.tt", "config/locales/#{locale}.yml" if locale != "en"
end

#setup_deviseObject

Setup Devise gem.



246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/panacea/rails/generator.rb', line 246

def setup_devise
  model_name = config.dig("devise_model_name").downcase
  plural_model_name = model_name.downcase.pluralize
  locale = config.dig("locale").split("- ").last

  generate "devise:install"
  generate "devise", model_name
  generate "devise:i18n:views", plural_model_name if config.dig("devise_override_views")
  generate "devise:i18n:locale", locale

  rails_command "db:migrate"
end

#setup_dotenvObject

Setup Dotenv gem.



146
147
148
149
# File 'lib/panacea/rails/generator.rb', line 146

def setup_dotenv
  template "templates/dotenv.tt", ".env"
  append_to_file ".gitignore", "\n# Ignore .env file \n.env\n"
end

#setup_foremanObject

Setup foreman gem



307
308
309
310
311
# File 'lib/panacea/rails/generator.rb', line 307

def setup_foreman
  run "gem install foreman" unless system("gem list -i foreman")

  template "templates/Procfile.tt", "Procfile"
end

#setup_githookObject

Creates chosen Git hook.



322
323
324
325
326
# File 'lib/panacea/rails/generator.rb', line 322

def setup_githook
  hook_file = ".git/hooks/#{config.dig('githook_type')}"
  template "templates/githook.tt", hook_file
  run "chmod ug+x #{hook_file}"
end

#setup_kaminariObject

Setup Kaminari gem.



294
295
296
# File 'lib/panacea/rails/generator.rb', line 294

def setup_kaminari
  generate "kaminari:config"
end

#setup_letter_openerObject

Setup letter_opener gem.



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/panacea/rails/generator.rb', line 232

def setup_letter_opener
  environment nil, env: "development" do
    <<~CONFS
      # Settings for Letter Opener
      config.action_mailer.delivery_method = :letter_opener
      config.action_mailer.perform_deliveries = true
      config.action_mailer.default_url_options = { host: "localhost", port: 3000 }

    CONFS
  end
end

#setup_money_railsObject

Setup money_rails gem.



288
289
290
# File 'lib/panacea/rails/generator.rb', line 288

def setup_money_rails
  generate "money_rails:initializer"
end

#setup_ojObject

Setup OJ gem.



140
141
142
# File 'lib/panacea/rails/generator.rb', line 140

def setup_oj
  template "templates/oj_initializer.tt", "config/initializers/oj.rb"
end

#setup_punditObject

Setup Pundit gem



315
316
317
318
# File 'lib/panacea/rails/generator.rb', line 315

def setup_pundit
  generate "pundit:install"
  inject_into_class "app/controllers/application_controller.rb", "ApplicationController", "  include Pundit\n"
end

#setup_rubocopObject

Create .rubocop.yml in generated Rails app.



95
96
97
# File 'lib/panacea/rails/generator.rb', line 95

def setup_rubocop
  template "templates/rubocop.tt", ".rubocop.yml"
end

#setup_simplecovObject

Setup Simplecov based on the chosen test suite.



110
111
112
113
114
115
116
117
118
119
# File 'lib/panacea/rails/generator.rb', line 110

def setup_simplecov
  path = if config.dig("test_suite") == "minitest"
           "test/support"
         else
           "spec/support"
         end

  template "templates/simplecov.tt", "#{path}/simplecov.rb"
  append_to_file ".gitignore", "\n# Ignore Coverage files \n/coverage\n"
end

#setup_test_suiteObject

Setup the test suite (rspec or minitest).



101
102
103
104
105
106
# File 'lib/panacea/rails/generator.rb', line 101

def setup_test_suite
  return unless config.dig("test_suite") == "rspec"

  generate "rspec:install"
  run "rm -r test"
end

#setup_timezoneObject

Setup the application’s timezone.



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/panacea/rails/generator.rb', line 177

def setup_timezone
  timezone = config.dig("timezone").split("-").first.chomp(" ")

  application nil do
    <<~CONFS
      # Default timezone
      config.time_zone = "#{timezone}"

    CONFS
  end
end

#setup_webpackObject

Setup webpacker gem.



300
301
302
303
# File 'lib/panacea/rails/generator.rb', line 300

def setup_webpack
  rails_command "webpacker:install"
  rails_command "webpacker:install:#{config.dig('webpack_type')}" if config.dig("webpack_type") != "none"
end

#update_source_pathsObject

Update Rails::Generators::AppGenerator source paths, making Panacea’s Templates under templates/ directory available.



71
72
73
# File 'lib/panacea/rails/generator.rb', line 71

def update_source_paths
  source_paths.unshift(root_dir)
end