Class: Panacea::Rails::Generator
- Inherits:
-
Object
- Object
- Panacea::Rails::Generator
- 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
-
#app_generator ⇒ Object
readonly
The Rails::Generators::AppGenerator context.
-
#config ⇒ Object
readonly
The Panacea’s Configuration Hash.
-
#root_dir ⇒ Object
readonly
A String with Panacea’s installation directory.
Instance Method Summary collapse
-
#after_bundle_hook ⇒ Object
All the methods passed via block are executed on the Rails::Generators::AppGenerator after_bundle hook.
-
#bye_message ⇒ Object
Display good bye message.
-
#commit! ⇒ Object
Commit only if end users want to.
-
#copy_gemfile ⇒ Object
Update the Gemfile.
-
#copy_readme ⇒ Object
Update the README.md.
-
#create_database ⇒ Object
Create database.
-
#fix_offenses! ⇒ Object
This needs to be run before commiting.
-
#generate_panacea_document ⇒ Object
Creates the PANACEA.md file.
-
#initialize(app_generator, panacea_config, root_dir) ⇒ Generator
constructor
This methods receive the context of the Rails::Generators::AppGenerator So it executes all methods against it.
-
#method_missing(method_name, *args, &block) ⇒ Object
Send any unknown method to the Rails::Generators::AppGenerator context.
-
#override_application_system_test ⇒ Object
Setup Headless Chrome Driver based on chosen test suite.
-
#override_test_helper ⇒ Object
Override test helper based on the chosen test suite.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Whitelist of Rails::Generator::AppGenerator and Thor methods.
-
#setup_background_job ⇒ Object
Setup chosen Background Job gem.
-
#setup_bullet ⇒ Object
Setup Bullet gem.
-
#setup_default_locale ⇒ Object
Setup the application’s locale.
-
#setup_devise ⇒ Object
Setup Devise gem.
-
#setup_dotenv ⇒ Object
Setup Dotenv gem.
-
#setup_foreman ⇒ Object
Setup foreman gem.
-
#setup_githook ⇒ Object
Creates chosen Git hook.
-
#setup_kaminari ⇒ Object
Setup Kaminari gem.
-
#setup_letter_opener ⇒ Object
Setup letter_opener gem.
-
#setup_money_rails ⇒ Object
Setup money_rails gem.
-
#setup_oj ⇒ Object
Setup OJ gem.
-
#setup_pundit ⇒ Object
Setup Pundit gem.
-
#setup_rubocop ⇒ Object
Create .rubocop.yml in generated Rails app.
-
#setup_simplecov ⇒ Object
Setup Simplecov based on the chosen test suite.
-
#setup_test_suite ⇒ Object
Setup the test suite (rspec or minitest).
-
#setup_timezone ⇒ Object
Setup the application’s timezone.
-
#setup_webpack ⇒ Object
Setup webpacker gem.
-
#update_source_paths ⇒ Object
Update Rails::Generators::AppGenerator source paths, making Panacea’s Templates under templates/ directory available.
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_generator ⇒ Object (readonly)
The Rails::Generators::AppGenerator context
14 15 16 |
# File 'lib/panacea/rails/generator.rb', line 14 def app_generator @app_generator end |
#config ⇒ Object (readonly)
The Panacea’s Configuration Hash
22 23 24 |
# File 'lib/panacea/rails/generator.rb', line 22 def config @config end |
#root_dir ⇒ Object (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_hook ⇒ Object
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_message ⇒ Object
Display good bye message.
317 318 319 320 |
# File 'lib/panacea/rails/generator.rb', line 317 def = "Panacea's work is done, enjoy!" say "\n\n\e[34m#{}\e[0m" end |
#commit! ⇒ Object
Commit only if end users want to.
310 311 312 313 |
# File 'lib/panacea/rails/generator.rb', line 310 def commit! git add: "." git commit: "-m '#{config.dig('commit_msg')}'" end |
#copy_gemfile ⇒ Object
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_readme ⇒ Object
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_database ⇒ Object
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.
304 305 306 |
# File 'lib/panacea/rails/generator.rb', line 304 def fix_offenses! run "rubocop -a --format=simple" end |
#generate_panacea_document ⇒ Object
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_test ⇒ Object
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_helper ⇒ Object
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
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[ 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_job ⇒ Object
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_bullet ⇒ Object
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_locale ⇒ Object
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_devise ⇒ Object
Setup Devise gem.
246 247 248 249 250 251 252 253 254 255 256 |
# 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:views" if config.dig("devise_override_views") rails_command "db:migrate" end |
#setup_dotenv ⇒ Object
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_foreman ⇒ Object
Setup foreman gem
279 280 281 282 283 |
# File 'lib/panacea/rails/generator.rb', line 279 def setup_foreman run "gem install foreman" unless system("gem list -i foreman") template "templates/Procfile.tt", "Procfile" end |
#setup_githook ⇒ Object
Creates chosen Git hook.
294 295 296 297 298 |
# File 'lib/panacea/rails/generator.rb', line 294 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_kaminari ⇒ Object
Setup Kaminari gem.
266 267 268 |
# File 'lib/panacea/rails/generator.rb', line 266 def setup_kaminari generate "kaminari:config" end |
#setup_letter_opener ⇒ Object
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_rails ⇒ Object
Setup money_rails gem.
260 261 262 |
# File 'lib/panacea/rails/generator.rb', line 260 def setup_money_rails generate "money_rails:initializer" end |
#setup_oj ⇒ Object
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_pundit ⇒ Object
Setup Pundit gem
287 288 289 290 |
# File 'lib/panacea/rails/generator.rb', line 287 def setup_pundit generate "pundit:install" inject_into_class "app/controllers/application_controller.rb", "ApplicationController", " include Pundit\n" end |
#setup_rubocop ⇒ Object
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_simplecov ⇒ Object
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_suite ⇒ Object
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_timezone ⇒ Object
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_webpack ⇒ Object
Setup webpacker gem.
272 273 274 275 |
# File 'lib/panacea/rails/generator.rb', line 272 def setup_webpack rails_command "webpacker:install" rails_command "webpacker:install:#{config.dig('webpack_type')}" if config.dig("webpack_type") != "none" end |
#update_source_paths ⇒ Object
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 |