rolemodel-rails 2.7.0 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +12 -3
  3. data/exe/rmr +6 -0
  4. data/lib/generators/rolemodel/README.md +1 -0
  5. data/lib/generators/rolemodel/all_generator.rb +1 -0
  6. data/lib/generators/rolemodel/core_setup/README.md +21 -11
  7. data/lib/generators/rolemodel/core_setup/USAGE +10 -6
  8. data/lib/generators/rolemodel/core_setup/core_setup_generator.rb +8 -0
  9. data/lib/generators/rolemodel/dotenv/README.md +110 -0
  10. data/lib/generators/rolemodel/dotenv/USAGE +15 -0
  11. data/lib/generators/rolemodel/dotenv/dotenv_generator.rb +43 -0
  12. data/lib/generators/rolemodel/dotenv/templates/lib/env_value.rb +66 -0
  13. data/lib/generators/rolemodel/dotenv/templates/spec/lib/env_value_spec.rb +178 -0
  14. data/lib/generators/rolemodel/editors/editors_generator.rb +10 -13
  15. data/lib/generators/rolemodel/good_job/good_job_generator.rb +5 -1
  16. data/lib/generators/rolemodel/good_job/templates/config/initializers/good_job.rb +1 -1
  17. data/lib/generators/rolemodel/heroku/heroku_generator.rb +4 -9
  18. data/lib/generators/rolemodel/heroku/templates/deploy-app/SKILL.md +1 -1
  19. data/lib/generators/rolemodel/linters/eslint/eslint_generator.rb +0 -1
  20. data/lib/generators/rolemodel/mailers/mailers_generator.rb +27 -47
  21. data/lib/generators/rolemodel/mailers/templates/app/mailers/staging_mailer_interceptor.rb +2 -3
  22. data/lib/generators/rolemodel/readme/templates/README.md.erb +3 -0
  23. data/lib/generators/rolemodel/saas/devise/devise_generator.rb +3 -23
  24. data/lib/generators/rolemodel/sentry/sentry_generator.rb +1 -0
  25. data/lib/generators/rolemodel/sentry/templates/config/initializers/sentry.rb +1 -1
  26. data/lib/generators/rolemodel/source_map/README.md +1 -1
  27. data/lib/generators/rolemodel/source_map/source_map_generator.rb +8 -4
  28. data/lib/generators/rolemodel/source_map/templates/lib/middleware/rolemodel/source_map.rb +1 -1
  29. data/lib/generators/rolemodel/testing/jasmine_playwright/jasmine_playwright_generator.rb +0 -1
  30. data/lib/generators/rolemodel/testing/vitest/vitest_generator.rb +0 -1
  31. data/lib/generators/rolemodel/ui_components/README.md +2 -1
  32. data/lib/rolemodel/file_content_helper.rb +191 -0
  33. data/lib/rolemodel/generator_base.rb +41 -2
  34. data/lib/rolemodel/new_app/template.rb +7 -0
  35. data/lib/rolemodel/new_app.rb +22 -0
  36. data/lib/rolemodel/utility/task_tools.rb +1 -1
  37. data/lib/rolemodel/version.rb +1 -1
  38. metadata +12 -4
  39. data/lib/generators/rolemodel/editors/vscode_helpers.rb +0 -22
  40. data/lib/rolemodel/replace_content_helper.rb +0 -29
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 30fe14227c2e8b6871de0ae74dd94bb58a204fd1d1db523cbd34c006f81b3d08
4
- data.tar.gz: 03c31aa3406bb87521c205bfe4e60a21834710e81a6bc40672291bf5ce7760dd
3
+ metadata.gz: 2eae463b96705a0478a97b06daf586e4022499a8d53cca46ba6c91ab2cf1d41c
4
+ data.tar.gz: 174fcab3a6f7b8a4e29434a0ee123a9b853fb3ef7006e9cefc1199721083bbe6
5
5
  SHA512:
6
- metadata.gz: 02b574434c6cd1ecc72ed48a8b64205a5ac3430a708eabaf1529f2cd49261ec6bf15257af4e04c06cf7eb21331e674cdba104ee3f3ebe2c2692bcd2b614968c1
7
- data.tar.gz: 331e633f75a0b959c76a81d082f9a3f983a09838d5ff29258e544271e2ff98302bee2bace098c6f409136247bb6f7a248a1cacb433f34a046a2ec1fb798640dd
6
+ metadata.gz: ab6a232a25594bf86544265511deb2174f148ed29bfd945f9f561ac19cbeed3c26503206045b5f3dfc3065b83ad049d9ea821e9558e8a7238fa512cbc722ca88
7
+ data.tar.gz: a12296267725fd071c2db0fe5a57ebc4fa623f18a55b19c109d0399ad7be19df00c4f5c412da46f93ccac8bac50d7f8eb0a468766c9caf66a417ccbcb6a97226
data/README.md CHANGED
@@ -11,14 +11,21 @@ Attempts to solve the pain of:
11
11
  * Our BestPractice learns don't often get ported into other projects as it isn't straightforward to do so
12
12
  * There is an emerging pattern of libraries using generators (e.g. webpacker) to migrate a setup between library versions
13
13
 
14
- ## Precondition
14
+ ## New Apps
15
15
 
16
- The rolemodel-rails gem expects to be added to an existing Rails project. Typically those are started with:
16
+ Start a new app with `rmr`, which wraps `rails new`:
17
17
 
18
18
  ```shell
19
- rails new <app-name> --javascript=webpack --database=postgresql --skip-test --skip-solid
19
+ gem install rolemodel-rails
20
+ rmr new <app-name>
20
21
  ```
21
22
 
23
+ `rmr` passes `--database=postgresql --javascript=webpack --skip-kamal --skip-solid --skip-test` and a template that adds this gem and runs `rolemodel:core_setup`, which asks for an icon library (default Material). Flags in your `~/.railsrc` override those defaults, and flags on the command line override both.
24
+
25
+ ## Existing Apps
26
+
27
+ The rolemodel-rails gem can be added to an existing Rails project.
28
+
22
29
  The Devise generator requires your database to exist before running.
23
30
 
24
31
  ```shell
@@ -109,6 +116,7 @@ bin/rails g
109
116
  * [Tailored Select](./lib/generators/rolemodel/tailored_select)
110
117
  * [Tom Select](./lib/generators/rolemodel/tom_select)
111
118
  * [Lograge](./lib/generators/rolemodel/lograge)
119
+ * [Dotenv](./lib/generators/rolemodel/dotenv)
112
120
 
113
121
  ## Agent Skills
114
122
 
@@ -121,6 +129,7 @@ Generators that leave behind conventions a coding agent needs to follow also ins
121
129
  | `rolemodel:turbo:modals` | `turbo-modals` |
122
130
  | `rolemodel:turbo:form` | `dynamic-forms` |
123
131
  | `rolemodel:soft_destroyable` | `soft-destroyable` |
132
+ | `rolemodel:core_setup` | `polymorphic-parent-resources`, `rake-tasks` |
124
133
 
125
134
  ## Utilities
126
135
 
data/exe/rmr ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/rolemodel/new_app'
5
+
6
+ Rails::Generators::AppGenerator.start Rolemodel::NewApp.argv(ARGV)
@@ -11,6 +11,7 @@
11
11
  * [Kaminari](./kaminari)
12
12
  * [Linters](./linters)
13
13
  * [Lograge](./lograge)
14
+ * [Dotenv](./dotenv)
14
15
  * [Mailers](./mailers)
15
16
  * [Optics](./optics)
16
17
  * [React](./react)
@@ -3,6 +3,7 @@ module Rolemodel
3
3
  source_root File.expand_path('templates', __dir__)
4
4
 
5
5
  def run_all_the_generators
6
+ generate 'rolemodel:dotenv'
6
7
  generate 'rolemodel:github'
7
8
  generate 'rolemodel:heroku'
8
9
  generate 'rolemodel:readme'
@@ -1,5 +1,7 @@
1
1
  # Core Setup Generator
2
2
 
3
+ [USAGE](./USAGE)
4
+
3
5
  Runs the core generators every new Rails app should have. Unlike
4
6
  `rolemodel:all`, this skips the app-specific extras (React, SaaS/Devise,
5
7
  GoodJob, Kaminari, etc.) so you get just the standard baseline — a new Rails
@@ -9,21 +11,29 @@ app you can push straight to Heroku right after generation.
9
11
 
10
12
  - A freshly generated Rails app
11
13
 
14
+ ## Options
15
+
16
+ - `--material`, `--phosphor`, `--tabler`, `--feather`, `--lucide`, or `--custom`
17
+ picks the Optics icon library up front. Without one, the icons generator asks.
18
+ - `--skip-deployable` skips Heroku, Sentry, and Lograge.
19
+
12
20
  ## What you get
13
21
 
14
- - [GitHub](../github) — standard GitHub configuration
15
- - [Heroku](../heroku) — standard Heroku deployment configuration
16
- - [Readme](../readme) — standard project README
17
- - [Webpack](../webpack) — Webpack v5 for JS and CSS
18
- - [Sentry](../sentry) — error monitoring for Ruby and JavaScript
22
+ - [Dotenv](../dotenv) — local settings in `.env.local`, and `EnvValue` to read them
19
23
  - [Slim](../slim) — Slim templates
20
- - [Optics](../optics) — Optics design system (pass an icon library flag such as
21
- `--material` to choose it up front; otherwise the icons generator asks)
22
- - [Testing](../testing) — RSpec, FactoryBot, parallel_tests, TestProf
23
- (pass `--js-runner` to include jasmine-playwright-runner)
24
+ - [Webpack](../webpack) — Webpack v5 for JS and CSS
25
+ - [Optics](../optics) — Optics design system and icons
24
26
  - [SimpleForm](../simple_form) — SimpleForm with our configuration
25
27
  - [Tom Select](../tom_select) — Tom Select with a Stimulus controller and SimpleForm inputs
28
+ - [Testing](../testing) — RSpec, FactoryBot, parallel_tests, and TestProf
29
+ - [Turbo](../turbo) — Turbo 8 setup, confirm dialogs, modals, and dynamic forms
30
+ - [Flash](../ui_components/flash) — flash messages
31
+ - [GitHub](../github) — standard GitHub configuration
32
+ - [Readme](../readme) — standard project README
26
33
  - [Linters](../linters) — Rubocop and ESLint
27
- - [UI Components](../ui_components) — flash, the modal pattern, & Turbo 8 support
28
- - [Editors](../editors) — EditorConfig and recommended VSCode extensions
34
+ - [Heroku](../heroku) — standard Heroku deployment configuration
35
+ - [Sentry](../sentry) — error monitoring for Ruby and JavaScript
29
36
  - [Lograge](../lograge) — condensed request logging
37
+ - The [`polymorphic-parent-resources`](https://github.com/RoleModel/skills/tree/main/skills/polymorphic-parent-resources)
38
+ and [`rake-tasks`](https://github.com/RoleModel/skills/tree/main/skills/rake-tasks) agent skills for
39
+ `resource_for` and `Rolemodel::Utility::TaskTools`, installed into `.agents/skills`
@@ -1,12 +1,16 @@
1
1
  Description:
2
2
  Runs the core generators every new Rails app should have
3
3
 
4
- Sets up GitHub config, Heroku deployment, README, Webpack, Sentry, Slim,
5
- Optics, testing (RSpec & friends), SimpleForm, linters, Turbo 8+ support,
6
- and Lograge. The result is an app that is ready to push directly to Heroku.
4
+ Sets up Slim, Webpack, Optics, SimpleForm, Tom Select, testing (RSpec &
5
+ friends), Turbo, flash messages, GitHub config, README, linters, Heroku
6
+ deployment, Sentry, and Lograge. It also installs the agent skills for
7
+ resource_for and TaskTools. The result is an app that is ready to push
8
+ directly to Heroku.
7
9
 
8
- Pass --js-runner to also include jasmine-playwright-runner for browser
9
- JS testing.
10
+ Pass an icon library flag (--material, --phosphor, --tabler, --feather,
11
+ --lucide, or --custom) to choose the Optics icons up front.
12
+
13
+ Pass --skip-deployable to skip Heroku, Sentry, and Lograge.
10
14
 
11
15
  Example:
12
- rails generate rolemodel:core_setup
16
+ rails generate rolemodel:core_setup --material
@@ -6,7 +6,15 @@ module Rolemodel
6
6
 
7
7
  class_option :skip_deployable, type: :boolean, default: false, desc: 'skip generators for app deployment (Heroku, Sentry, Lograge)'
8
8
 
9
+ def install_agent_skills
10
+ say 'Installing agent skills for the rolemodel_rails utilities', :green
11
+
12
+ install_skill 'polymorphic-parent-resources'
13
+ install_skill 'rake-tasks'
14
+ end
15
+
9
16
  def run_the_core_generators
17
+ generate 'rolemodel:dotenv'
10
18
  generate 'rolemodel:slim'
11
19
  generate 'rolemodel:webpack'
12
20
  generate 'rolemodel:optics:all', *icon_library_flags
@@ -0,0 +1,110 @@
1
+ # Dotenv Generator
2
+
3
+ [USAGE](./USAGE)
4
+
5
+ Sets up local settings in a `.env.local` file, and adds `EnvValue` to read settings from ENV.
6
+
7
+ Run it with:
8
+
9
+ ```
10
+ bin/rails generate rolemodel:dotenv
11
+ ```
12
+
13
+ ## What you get
14
+
15
+ - The `dotenv` gem in the `development` and `test` groups.
16
+ - `/.env*` in `.gitignore`, so Git never commits your local values. New Rails apps already ignore it.
17
+ - `lib/env_value.rb`, required from `config/application.rb`.
18
+ - `spec/lib/env_value_spec.rb`, when the app has a `spec` folder.
19
+
20
+ ## Put a local value in `.env.local`
21
+
22
+ Create `.env.local` in the project root. Add one `KEY=value` line for each setting:
23
+
24
+ ```
25
+ SENDGRID_API_KEY=xxxxxxxxx
26
+ ```
27
+
28
+ Restart the server after you change the file.
29
+
30
+ To copy values from a Heroku app, run `heroku config -s -a <app>`. It prints `KEY=value` lines. Paste the lines you need into `.env.local`.
31
+
32
+ The dotenv gem loads `.env.local` in development. Tests don't read `.env.local`, so a test run never depends on your machine. Put a value that tests need in `.env.test`, or give it a `local:` or `default:` value (see [Defaults for each environment](#defaults-for-each-environment)). Production reads its values from the host, for example Heroku config vars.
33
+
34
+ ## Read a setting with `EnvValue`
35
+
36
+ `EnvValue` reads a value from ENV. A blank value counts as unset. When the value is unset, `EnvValue` uses a default.
37
+
38
+ Read each setting once in `config/application.rb`, and keep it as a `config.x` value:
39
+
40
+ ```ruby
41
+ config.x.sendgrid.api_key = EnvValue.string('SENDGRID_API_KEY')
42
+ config.x.report_emails = EnvValue.array('REPORT_EMAILS', default: %w[team@example.com])
43
+ config.x.serve_static_files = EnvValue.boolean('RAILS_SERVE_STATIC_FILES')
44
+ config.x.default_url = EnvValue.url('DEFAULT_URL', default: 'http://localhost:3000')
45
+ ```
46
+
47
+ Then read it in app code:
48
+
49
+ ```ruby
50
+ Rails.configuration.x.sendgrid.api_key
51
+ ```
52
+
53
+ | Method | Returns | When unset, with no default | When the value is not valid |
54
+ |---|---|---|---|
55
+ | `string` | the text | `nil` | — |
56
+ | `string!` | the text | raises | raises when blank |
57
+ | `array` | the comma-separated items, trimmed | `[]` | — |
58
+ | `boolean` | `true` for `true`, `t`, `1`, `yes`, `y`, `on`, `enabled`. `false` for `false`, `f`, `0`, `no`, `n`, `off`, `disabled`. Case does not matter. | `false` | raises on any other text, naming the key |
59
+ | `url` | the URL | `nil` | raises when it has no scheme or host |
60
+
61
+ Read settings in `config/application.rb`, so a raise stops the app when it starts. Then you see a bad value at once.
62
+
63
+ ### Defaults for each environment
64
+
65
+ A default can be different in each environment. `EnvValue` uses the first of these that applies:
66
+
67
+ 1. A key named for the current environment, such as `production:` or `staging:`. A key set to `nil` counts, and gives `nil`.
68
+ 2. `local:`, in development and test only.
69
+ 3. A block.
70
+ 4. `default:`.
71
+
72
+ ```ruby
73
+ # Every environment except production falls back to dev-key. Staging gets dev-key too.
74
+ # Production gets nil unless ENCRYPTION_KEY is set.
75
+ EnvValue.string('ENCRYPTION_KEY', default: 'dev-key', production: nil)
76
+
77
+ # Only development and test fall back to dev-client-id. Staging and production get nil.
78
+ EnvValue.string('API_CLIENT_ID', local: 'dev-client-id')
79
+
80
+ # The block runs only when the key is unset.
81
+ EnvValue.url('DEFAULT_URL') { "https://#{EnvValue.string('HEROKU_APP_NAME')}.herokuapp.com" }
82
+ ```
83
+
84
+ ## Move from `env.rb`
85
+
86
+ Some RoleModel apps keep local settings in `env.rb` and `.env_overrides.rb`. Those are Ruby files, so only Ruby can read them. A `.env` file holds plain `KEY=value` lines that Ruby, Node, Docker Compose, foreman and 1Password can all read.
87
+
88
+ If the app has `env.rb` or `.env_overrides.rb`:
89
+
90
+ 1. Run the generator.
91
+ 2. Move each `ENV['KEY'] ||= 'value'` line from `env.rb` and `.env_overrides.rb` to `.env.local` as `KEY=value`.
92
+ 3. If `env.rb` set a value that production uses, add it as a default in `config/application.rb` with `EnvValue`.
93
+ 4. Remove the line that requires `env.rb` from `config/application.rb`.
94
+ 5. Delete `env.rb` and `.env_overrides.rb`.
95
+
96
+ ## Generators that add settings
97
+
98
+ These generators add their settings to `config/application.rb` with `EnvValue`. If the app has no `lib/env_value.rb`, they run this generator first.
99
+
100
+ | Generator | ENV key | Setting |
101
+ |---|---|---|
102
+ | `rolemodel:heroku` | `LOG_LEVEL` | `config.x.log_level` (default `INFO`) |
103
+ | `rolemodel:sentry` | `SENTRY_DSN` | `config.x.sentry.dsn` |
104
+ | `rolemodel:mailers` | `WHITELISTED_EMAILS` | `config.x.whitelisted_emails` |
105
+ | | `DEFAULT_URL` | `config.x.default_url`: the app's full URL, such as `https://app.example.com`, used for links and assets in emails. Set it in production. Without it, a Heroku review app uses `https://<HEROKU_APP_NAME>.herokuapp.com`, and development uses `http://localhost:3000`. It replaces `PRODUCTION_HOST` and `REVIEW_APP`. The generator comments out the `default_url_options` line in development.rb and production.rb, so set the URL here, not there. |
106
+ | | `HEROKU_APP_NAME` | `config.x.heroku_app_name` |
107
+ | | `SENDGRID_API_KEY` | `config.x.sendgrid.api_key` |
108
+ | `rolemodel:saas:devise` | `DEFAULT_URL` | `config.x.default_url`, the same setting as `rolemodel:mailers` |
109
+ | `rolemodel:source_map` | `SOURCE_MAPS_ALLOWED_USERS_EMAILS` | `config.x.source_maps.allowed_user_emails` |
110
+ | `rolemodel:good_job` | `GOOD_JOB_QUEUES` | `config.x.good_job.queues` (default `pdf:1;-pdf`) |
@@ -0,0 +1,15 @@
1
+ Description:
2
+ Loads local settings from a .env.local file with the dotenv gem,
3
+ and adds EnvValue to read typed settings from ENV.
4
+
5
+ Example:
6
+ rails generate rolemodel:dotenv
7
+
8
+ This will create:
9
+ lib/env_value.rb
10
+ spec/lib/env_value_spec.rb (when the app has a spec folder)
11
+
12
+ This will change:
13
+ Gemfile (adds dotenv to the development and test groups)
14
+ config/application.rb (requires lib/env_value)
15
+ .gitignore (ignores /.env*)
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rolemodel
4
+ class DotenvGenerator < GeneratorBase
5
+ source_root File.expand_path('templates', __dir__)
6
+
7
+ def install_dotenv
8
+ say 'Installing dotenv', :green
9
+
10
+ bundle_command('add dotenv --group "development, test"')
11
+ end
12
+
13
+ # New Rails apps already ignore /.env*. Older apps may not.
14
+ def ignore_env_files
15
+ gitignore = File.join(destination_root, '.gitignore')
16
+ content = File.exist?(gitignore) ? File.read(gitignore) : ''
17
+ return if content.include?('/.env*')
18
+
19
+ create_file '.gitignore', content + <<~IGNORE, force: true
20
+
21
+ # Ignore all environment files.
22
+ /.env*
23
+ IGNORE
24
+ end
25
+
26
+ def add_env_value
27
+ say 'Adding EnvValue to read settings from ENV', :green
28
+
29
+ copy_file 'lib/env_value.rb'
30
+ inject_into_file(
31
+ 'config/application.rb',
32
+ "require_relative '../lib/env_value'\n",
33
+ before: /^# Require the gems listed in Gemfile/
34
+ )
35
+ end
36
+
37
+ def add_env_value_spec
38
+ return unless Dir.exist?(File.join(destination_root, 'spec'))
39
+
40
+ copy_file 'spec/lib/env_value_spec.rb'
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Reads typed settings from ENV. A blank value counts as unset. When it is unset, the
4
+ # default comes from the first of these that applies: a key named for the current
5
+ # environment, `local:` in development and test, the block, then `default:`.
6
+ #
7
+ # EnvValue.string('DEFAULT_URL') { 'http://localhost:3000' }
8
+ # EnvValue.string('API_CLIENT_ID', local: 'dev-client-id')
9
+ # EnvValue.string!('FROM') # raises KeyError when unset, ArgumentError when blank
10
+ # EnvValue.url('DEFAULT_URL') # raises ArgumentError without a scheme and host
11
+ # EnvValue.string('ENCRYPTION_KEY', default: 'dev-key', production: nil)
12
+ # EnvValue.array('REPORT_EMAILS', default: %w[someone@example.com])
13
+ # EnvValue.boolean('FEATURE_ON', default: true)
14
+ module EnvValue
15
+ ENVIRONMENTS = %i[development test staging production local default].freeze
16
+ TRUE_WORDS = %w[true t 1 yes y on enabled].freeze
17
+ FALSE_WORDS = %w[false f 0 no n off disabled].freeze
18
+
19
+ module_function
20
+
21
+ def string(key, **defaults, &)
22
+ read(key) || fallback(defaults, &)
23
+ end
24
+
25
+ def string!(key)
26
+ ENV.fetch(key).presence || raise(ArgumentError, "#{key} is blank")
27
+ end
28
+
29
+ def url(key, **defaults, &)
30
+ value = string(key, **defaults, &)
31
+ return value if value.nil? || URI.parse(value).then { it.scheme && it.host }
32
+
33
+ raise ArgumentError, "#{key} must be a full URL, like https://example.com, not #{value.inspect}"
34
+ end
35
+
36
+ def array(key, **defaults, &)
37
+ value = read(key)
38
+ value ? value.split(',').map(&:strip).compact_blank : fallback(defaults, &) || []
39
+ end
40
+
41
+ def boolean(key, **defaults, &)
42
+ value = read(key)&.downcase
43
+ return fallback(defaults, &) || false unless value
44
+ return true if TRUE_WORDS.include?(value)
45
+ return false if FALSE_WORDS.include?(value)
46
+
47
+ raise ArgumentError, "#{key} must be one of #{(TRUE_WORDS + FALSE_WORDS).join(', ')}, not #{value.inspect}"
48
+ end
49
+
50
+ def read(key)
51
+ ENV.fetch(key, nil).presence
52
+ end
53
+
54
+ def fallback(defaults)
55
+ unknown = defaults.keys - ENVIRONMENTS
56
+ raise ArgumentError, "unknown environment: #{unknown.join(', ')}" if unknown.any?
57
+
58
+ environment = Rails.env.to_sym
59
+ if defaults.key?(environment) then defaults[environment]
60
+ elsif Rails.env.local? && defaults.key?(:local) then defaults[:local]
61
+ elsif block_given? then yield
62
+ else defaults[:default]
63
+ end
64
+ end
65
+ private_class_method :read, :fallback
66
+ end
@@ -0,0 +1,178 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ RSpec.describe EnvValue do
6
+ let(:key) { 'ENV_VALUE_SPEC' }
7
+
8
+ around do |example|
9
+ original = ENV.fetch(key, nil)
10
+ example.run
11
+ ensure
12
+ ENV[key] = original
13
+ end
14
+
15
+ def in_environment(name)
16
+ allow(Rails).to receive(:env).and_return(ActiveSupport::EnvironmentInquirer.new(name))
17
+ end
18
+
19
+ describe '.string' do
20
+ it 'reads the value' do
21
+ ENV[key] = 'https://example.test'
22
+
23
+ expect(described_class.string(key, default: 'fallback')).to eq 'https://example.test'
24
+ end
25
+
26
+ it 'falls back to the default when the key is missing or blank' do
27
+ ENV[key] = nil
28
+ expect(described_class.string(key, default: 'fallback')).to eq 'fallback'
29
+
30
+ ENV[key] = ' '
31
+ expect(described_class.string(key, default: 'fallback')).to eq 'fallback'
32
+ end
33
+
34
+ it 'computes the default from a block only when it is needed' do
35
+ ENV[key] = 'set'
36
+ expect(described_class.string(key) { raise 'should not run' }).to eq 'set'
37
+
38
+ ENV[key] = nil
39
+ expect(described_class.string(key) { 'computed' }).to eq 'computed'
40
+ end
41
+
42
+ it 'is nil without a value or a default' do
43
+ ENV[key] = nil
44
+
45
+ expect(described_class.string(key)).to be_nil
46
+ end
47
+ end
48
+
49
+ describe '.string!' do
50
+ it 'reads the value' do
51
+ ENV[key] = '2026-09-01'
52
+
53
+ expect(described_class.string!(key)).to eq '2026-09-01'
54
+ end
55
+
56
+ it 'raises KeyError when the key is missing, as ENV.fetch does' do
57
+ ENV[key] = nil
58
+
59
+ expect { described_class.string!(key) }.to raise_error(KeyError, /ENV_VALUE_SPEC/)
60
+ end
61
+
62
+ it 'raises ArgumentError, naming the key, when the value is blank' do
63
+ ENV[key] = ' '
64
+
65
+ expect { described_class.string!(key) }.to raise_error(ArgumentError, 'ENV_VALUE_SPEC is blank')
66
+ end
67
+ end
68
+
69
+ describe '.url' do
70
+ it 'reads a full URL' do
71
+ ENV[key] = 'https://example.test'
72
+
73
+ expect(described_class.url(key)).to eq 'https://example.test'
74
+ end
75
+
76
+ it 'checks a default the same way' do
77
+ ENV[key] = nil
78
+
79
+ expect(described_class.url(key) { 'http://localhost:3000' }).to eq 'http://localhost:3000'
80
+ expect { described_class.url(key, default: 'localhost:3000') }.to raise_error(ArgumentError)
81
+ end
82
+
83
+ it 'rejects a value without a scheme, naming the key' do
84
+ ENV[key] = 'example.test'
85
+
86
+ expect { described_class.url(key) }.to raise_error(ArgumentError, /ENV_VALUE_SPEC must be a full URL/)
87
+ end
88
+
89
+ it 'is nil without a value or a default' do
90
+ ENV[key] = nil
91
+
92
+ expect(described_class.url(key)).to be_nil
93
+ end
94
+ end
95
+
96
+ describe 'defaults per environment' do
97
+ before { ENV[key] = nil }
98
+
99
+ it 'uses the default named for the current environment' do
100
+ in_environment('staging')
101
+
102
+ expect(described_class.string(key, default: 'any', staging: 'review')).to eq 'review'
103
+ end
104
+
105
+ it 'uses the local default in development and test' do
106
+ in_environment('development')
107
+ expect(described_class.string(key, local: 'asdf')).to eq 'asdf'
108
+
109
+ in_environment('production')
110
+ expect(described_class.string(key, local: 'asdf')).to be_nil
111
+ end
112
+
113
+ it 'prefers the named environment over local, and local over the plain default' do
114
+ in_environment('test')
115
+
116
+ expect(described_class.string(key, default: 'any', local: 'local', test: 'test')).to eq 'test'
117
+ expect(described_class.string(key, default: 'any', local: 'local')).to eq 'local'
118
+ end
119
+
120
+ it 'lets an environment opt out of the default with nil' do
121
+ in_environment('production')
122
+
123
+ expect(described_class.string(key, default: 'dev-key', production: nil)).to be_nil
124
+ end
125
+
126
+ it 'still prefers a set value over any default' do
127
+ in_environment('development')
128
+ ENV[key] = 'set'
129
+
130
+ expect(described_class.string(key, local: 'asdf')).to eq 'set'
131
+ end
132
+
133
+ it 'rejects an environment it does not know, to catch typos' do
134
+ expect { described_class.string(key, prodution: nil) }.to raise_error(ArgumentError, /prodution/)
135
+ end
136
+ end
137
+
138
+ describe '.array' do
139
+ it 'splits a comma-separated value and trims each item' do
140
+ ENV[key] = 'a@example.com, b@example.com,,c@example.com '
141
+
142
+ expect(described_class.array(key)).to eq %w[a@example.com b@example.com c@example.com]
143
+ end
144
+
145
+ it 'falls back to the default when the key is missing' do
146
+ ENV[key] = nil
147
+
148
+ expect(described_class.array(key, default: %w[fallback])).to eq %w[fallback]
149
+ expect(described_class.array(key)).to eq []
150
+ end
151
+ end
152
+
153
+ describe '.boolean' do
154
+ it 'reads the usual words for true and false, in any case' do
155
+ { 'true' => true, 'TRUE' => true, 't' => true, '1' => true, 'yes' => true, 'Y' => true, 'on' => true,
156
+ 'enabled' => true,
157
+ 'false' => false, 'f' => false, '0' => false, 'no' => false, 'n' => false, 'Off' => false,
158
+ 'disabled' => false, }.each do |value, expected|
159
+ ENV[key] = value
160
+
161
+ expect(described_class.boolean(key)).to be(expected), "expected #{value.inspect} to be #{expected}"
162
+ end
163
+ end
164
+
165
+ it 'rejects any other value rather than guessing, naming the key' do
166
+ ENV[key] = 'nope'
167
+
168
+ expect { described_class.boolean(key) }.to raise_error(ArgumentError, /ENV_VALUE_SPEC/)
169
+ end
170
+
171
+ it 'falls back to the default when the key is missing' do
172
+ ENV[key] = nil
173
+
174
+ expect(described_class.boolean(key, default: true)).to be true
175
+ expect(described_class.boolean(key)).to be false
176
+ end
177
+ end
178
+ end
@@ -1,12 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'vscode_helpers'
4
-
5
3
  module Rolemodel
6
4
  # Add standard editorconfig and any extensions to enable it
7
5
  class EditorsGenerator < GeneratorBase
8
- include Rolemodel::VSCodeHelpers
9
-
10
6
  # This is bringing in the root from this gem, so we only modify
11
7
  # one copy of .editorconfig for both this repo and any consumers.
12
8
  source_root File.expand_path('../../../../', __dir__)
@@ -15,15 +11,16 @@ module Rolemodel
15
11
  # Bring over the .editorconfig file for whitespace, etc.
16
12
  copy_file '.editorconfig'
17
13
 
18
- # Configuration for VSCode to recommend the EditorConfig extension.
19
- ensure_extensions_config
20
-
21
- # Presumably in the future, we might add ESLint, Rubocop, etc.
22
- # extensions. We might eventually also split this out, if there are
23
- # extensions for RubyMine or other editors, but in theory, we might
24
- # want to bring over config files for any RM-supported editors.
25
- inject_into_file '.vscode/extensions.json', after: '"recommendations": [' do
26
- "\n \"EditorConfig.EditorConfig\","
14
+ # Recommend the EditorConfig extension in VSCode. This creates .vscode/extensions.json
15
+ # when it is missing. Presumably in the future, we might add ESLint, Rubocop, etc.
16
+ # extensions, or config files for other RM-supported editors.
17
+ # VS Code extension IDs ignore case, so an existing 'editorconfig.editorconfig' counts.
18
+ modify_json_file('.vscode/extensions.json') do |config|
19
+ recommendations = Array(config['recommendations'])
20
+ unless recommendations.any? { it.casecmp?('EditorConfig.EditorConfig') }
21
+ recommendations << 'EditorConfig.EditorConfig'
22
+ end
23
+ config['recommendations'] = recommendations
27
24
  end
28
25
  end
29
26
  end
@@ -22,7 +22,10 @@ module Rolemodel
22
22
 
23
23
  RUBY
24
24
 
25
- gsub_file 'config/environments/production.rb', 'config.active_job.queue_adapter', '# config.active_job.queue_adapter'
25
+ # production.rb runs after application.rb, so an adapter it sets, such as Solid Queue's,
26
+ # would win. Comment out only a line that sets one; new apps made with --skip-solid
27
+ # already have it as a comment.
28
+ comment_lines 'config/environments/production.rb', /config\.active_job\.queue_adapter\s*=/
26
29
  end
27
30
 
28
31
  def configure_good_job_routes
@@ -55,6 +58,7 @@ module Rolemodel
55
58
  def copy_initializers
56
59
  say 'Add GoodJob initializers'
57
60
 
61
+ add_setting "config.x.good_job.queues = EnvValue.string('GOOD_JOB_QUEUES', default: 'pdf:1;-pdf')"
58
62
  directory 'config/initializers'
59
63
  end
60
64
 
@@ -11,7 +11,7 @@
11
11
  Rails.application.configure do
12
12
  config.good_job = {
13
13
  smaller_number_is_higher_priority: true, # new default in V4 and for all of ActiveJob
14
- queues: ENV.fetch('GOOD_JOB_QUEUES', 'pdf:1;-pdf'), # see note above
14
+ queues: config.x.good_job.queues, # see note above
15
15
  execution_mode: Rails.env.test? ? :inline : :external # see note above
16
16
  # enable_cron: true,
17
17
  # cron: {