Class: TsykvasRailsTemplate::Generators::CompanionsGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/tsykvas_rails_template/companions/companions_generator.rb

Overview

Adds the recommended companion gems used across the author’s reference projects (sport / planner / esl). Runs ‘bundle install` and the per-gem `:install` sub-generators (no User model is generated for Devise).

Designed to be idempotent: re-running the generator does not duplicate Gemfile entries, re-run sub-generators, or re-inject configuration.

Instance Method Summary collapse

Instance Method Details

#add_dev_group_gemsObject



71
72
73
74
75
# File 'lib/generators/tsykvas_rails_template/companions/companions_generator.rb', line 71

def add_dev_group_gems
  return if options[:skip_dev]

  add_grouped_gems(%i[development test], %w[dotenv-rails])
end

#add_dotenv_to_gitignoreObject



149
150
151
152
153
154
155
156
# File 'lib/generators/tsykvas_rails_template/companions/companions_generator.rb', line 149

def add_dotenv_to_gitignore
  return if skip_post_install_for?(:dev)
  return unless File.exist?(destination_path(".gitignore"))
  return if File.read(destination_path(".gitignore")).match?(/^\.env\b/)

  append_to_file ".gitignore",
                 "\n# dotenv-rails (added by tsykvas_rails_template:companions)\n.env\n.env.*\n!.env.example\n"
end

#add_test_group_gemsObject



64
65
66
67
68
69
# File 'lib/generators/tsykvas_rails_template/companions/companions_generator.rb', line 64

def add_test_group_gems
  return if options[:skip_test]

  add_grouped_gems(%i[development test], %w[rspec-rails factory_bot_rails faker])
  add_grouped_gems([:test], %w[shoulda-matchers webmock])
end

#add_top_level_gemsObject



54
55
56
57
58
59
60
61
62
# File 'lib/generators/tsykvas_rails_template/companions/companions_generator.rb', line 54

def add_top_level_gems
  wanted = []
  wanted += %w[devise omniauth-rails_csrf_protection] unless options[:skip_auth]
  wanted << "simple_form" unless options[:skip_forms]
  wanted << "mini_magick" unless options[:skip_images]
  wanted << "mission_control-jobs" if !options[:skip_jobs_ui] && solid_queue_present?

  wanted.each { |name| add_gem_if_missing(name) }
end

#announceObject



158
159
160
161
162
163
164
165
# File 'lib/generators/tsykvas_rails_template/companions/companions_generator.rb', line 158

def announce
  say ""
  say "  Companions installed.", :green
  say "    Next: rails g devise User  (run yourself when your user schema is ready)"
  say "    /jobs UI is mounted with admin-only constraint; needs User#admin?"
  say "    Image processing requires ImageMagick installed system-wide."
  say ""
end

#bundle_update_companionsObject

Bump the companion gems we just added to their latest matching versions. ‘bundle install` is a no-op on re-runs where Gemfile.lock already pins older versions; `bundle update` lifts those pins within whatever constraint the Gemfile expresses (none, by default → latest).



91
92
93
94
95
96
97
98
99
100
# File 'lib/generators/tsykvas_rails_template/companions/companions_generator.rb', line 91

def bundle_update_companions
  return if options[:skip_bundle]

  names = companion_gem_names
  return if names.empty?

  in_root do
    Bundler.with_unbundled_env { run "bundle update #{names.join(" ")}" }
  end
end

#configure_shoulda_matchersObject



124
125
126
127
128
129
130
# File 'lib/generators/tsykvas_rails_template/companions/companions_generator.rb', line 124

def configure_shoulda_matchers
  return if skip_post_install_for?(:test)
  return unless File.exist?(destination_path("spec/rails_helper.rb"))
  return if File.read(destination_path("spec/rails_helper.rb")).include?("Shoulda::Matchers")

  append_to_file "spec/rails_helper.rb", shoulda_config_block
end

#configure_webmockObject



132
133
134
135
136
137
138
# File 'lib/generators/tsykvas_rails_template/companions/companions_generator.rb', line 132

def configure_webmock
  return if skip_post_install_for?(:test)
  return unless File.exist?(destination_path("spec/rails_helper.rb"))
  return if File.read(destination_path("spec/rails_helper.rb")).include?("WebMock.disable_net_connect!")

  append_to_file "spec/rails_helper.rb", webmock_config_block
end

#mount_mission_control_jobsObject



140
141
142
143
144
145
146
147
# File 'lib/generators/tsykvas_rails_template/companions/companions_generator.rb', line 140

def mount_mission_control_jobs
  return if skip_post_install_for?(:jobs_ui)
  return unless solid_queue_present?
  return unless File.exist?(destination_path("config/routes.rb"))
  return if File.read(destination_path("config/routes.rb")).include?("MissionControl::Jobs::Engine")

  route mission_control_route_block
end

#run_bundle_installObject



77
78
79
80
81
82
83
84
85
# File 'lib/generators/tsykvas_rails_template/companions/companions_generator.rb', line 77

def run_bundle_install
  return if options[:skip_bundle]

  in_root do
    Bundler.with_unbundled_env do
      run "bundle install"
    end
  end
end

#run_devise_installObject



102
103
104
105
106
107
# File 'lib/generators/tsykvas_rails_template/companions/companions_generator.rb', line 102

def run_devise_install
  return if skip_post_install_for?(:auth)
  return if File.exist?(destination_path("config/initializers/devise.rb"))

  generate "devise:install"
end

#run_rspec_installObject



117
118
119
120
121
122
# File 'lib/generators/tsykvas_rails_template/companions/companions_generator.rb', line 117

def run_rspec_install
  return if skip_post_install_for?(:test)
  return if File.exist?(destination_path("spec/rails_helper.rb"))

  generate "rspec:install"
end

#run_simple_form_installObject



109
110
111
112
113
114
115
# File 'lib/generators/tsykvas_rails_template/companions/companions_generator.rb', line 109

def run_simple_form_install
  return if skip_post_install_for?(:forms)
  return if File.exist?(destination_path("config/initializers/simple_form.rb"))

  flag = probe[:has_bootstrap] ? " --bootstrap" : ""
  generate "simple_form:install#{flag}"
end