rolemodel-rails 2.7.1 → 2.9.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 (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +13 -4
  3. data/exe/rmr +6 -0
  4. data/lib/generators/rolemodel/README.md +2 -0
  5. data/lib/generators/rolemodel/agent_docs/README.md +14 -0
  6. data/lib/generators/rolemodel/agent_docs/USAGE +10 -0
  7. data/lib/generators/rolemodel/agent_docs/agent_docs_generator.rb +22 -0
  8. data/lib/generators/rolemodel/agent_docs/templates/.claude/settings.json +15 -0
  9. data/lib/generators/rolemodel/agent_docs/templates/.github/instructions/conventions.instructions.md +9 -0
  10. data/lib/generators/rolemodel/agent_docs/templates/AGENTS.md.tt +26 -0
  11. data/lib/generators/rolemodel/agent_docs/templates/CLAUDE.md +1 -0
  12. data/lib/generators/rolemodel/agent_docs/templates/docs/CONVENTIONS.md +18 -0
  13. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/callbacks.md +5 -0
  14. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/controllers.md +3 -0
  15. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/eager-loading.md +5 -0
  16. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/logic-in-models.md +7 -0
  17. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/migrations.md +7 -0
  18. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/no-sql-strings.md +11 -0
  19. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/partial-locals.md +3 -0
  20. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/reproduce-the-bug.md +5 -0
  21. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/reuse-scopes.md +3 -0
  22. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/stimulus.md +3 -0
  23. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/string-enums.md +3 -0
  24. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/system-spec-assertions.md +17 -0
  25. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/test-layers.md +3 -0
  26. data/lib/generators/rolemodel/agent_docs/templates/docs/conventions/views-and-forms.md +9 -0
  27. data/lib/generators/rolemodel/all_generator.rb +2 -0
  28. data/lib/generators/rolemodel/core_setup/README.md +4 -0
  29. data/lib/generators/rolemodel/core_setup/USAGE +4 -4
  30. data/lib/generators/rolemodel/core_setup/core_setup_generator.rb +4 -1
  31. data/lib/generators/rolemodel/dotenv/README.md +110 -0
  32. data/lib/generators/rolemodel/dotenv/USAGE +15 -0
  33. data/lib/generators/rolemodel/dotenv/dotenv_generator.rb +43 -0
  34. data/lib/generators/rolemodel/dotenv/templates/lib/env_value.rb +66 -0
  35. data/lib/generators/rolemodel/dotenv/templates/spec/lib/env_value_spec.rb +178 -0
  36. data/lib/generators/rolemodel/editors/editors_generator.rb +10 -13
  37. data/lib/generators/rolemodel/good_job/good_job_generator.rb +5 -1
  38. data/lib/generators/rolemodel/good_job/templates/config/initializers/good_job.rb +1 -1
  39. data/lib/generators/rolemodel/heroku/heroku_generator.rb +4 -9
  40. data/lib/generators/rolemodel/heroku/templates/deploy-app/SKILL.md +1 -1
  41. data/lib/generators/rolemodel/linters/eslint/eslint_generator.rb +0 -1
  42. data/lib/generators/rolemodel/linters/rubocop/templates/.rubocop.yml +17 -0
  43. data/lib/generators/rolemodel/mailers/mailers_generator.rb +27 -47
  44. data/lib/generators/rolemodel/mailers/templates/app/mailers/staging_mailer_interceptor.rb +2 -3
  45. data/lib/generators/rolemodel/readme/templates/README.md.erb +3 -0
  46. data/lib/generators/rolemodel/saas/devise/devise_generator.rb +3 -23
  47. data/lib/generators/rolemodel/sentry/sentry_generator.rb +1 -0
  48. data/lib/generators/rolemodel/sentry/templates/config/initializers/sentry.rb +1 -1
  49. data/lib/generators/rolemodel/source_map/README.md +1 -1
  50. data/lib/generators/rolemodel/source_map/source_map_generator.rb +8 -4
  51. data/lib/generators/rolemodel/source_map/templates/lib/middleware/rolemodel/source_map.rb +1 -1
  52. data/lib/generators/rolemodel/testing/jasmine_playwright/jasmine_playwright_generator.rb +0 -1
  53. data/lib/generators/rolemodel/testing/vitest/vitest_generator.rb +0 -1
  54. data/lib/rolemodel/engine.rb +2 -0
  55. data/lib/rolemodel/file_content_helper.rb +191 -0
  56. data/lib/rolemodel/generator_base.rb +41 -2
  57. data/lib/rolemodel/new_app/template.rb +7 -0
  58. data/lib/rolemodel/new_app.rb +22 -0
  59. data/lib/rolemodel/version.rb +1 -1
  60. metadata +34 -4
  61. data/lib/generators/rolemodel/editors/vscode_helpers.rb +0 -22
  62. data/lib/rolemodel/replace_content_helper.rb +0 -29
@@ -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: {
@@ -27,21 +27,16 @@ module Rolemodel
27
27
  inject_into_file 'Gemfile', "\nruby file: '.ruby-version'\n", after: /^source .*$/
28
28
  end
29
29
 
30
- def force_ssl
31
- say 'Require SSL for production environment.', :green
32
-
33
- uncomment_lines('config/environments/production.rb', 'config.force_ssl = true')
34
- end
35
-
36
30
  def enable_log_level_configurability
37
31
  say 'Enable log-level adjustment via "LOG_LEVEL" environment variable', :green
38
32
 
33
+ add_setting "config.x.log_level = EnvValue.string('LOG_LEVEL', default: 'INFO')"
34
+
39
35
  gsub_file(
40
36
  'config/environments/production.rb',
41
- 'config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info")',
42
- "config.log_level = ENV.fetch('LOG_LEVEL', 'INFO')"
37
+ /config\.log_level = (ENV\.fetch\("RAILS_LOG_LEVEL", "info"\)|:info)/,
38
+ 'config.log_level = config.x.log_level'
43
39
  )
44
- gsub_file('config/environments/production.rb', 'config.log_level = :info', "config.log_level = ENV.fetch('LOG_LEVEL', 'INFO')")
45
40
  end
46
41
 
47
42
  def create_assets_rake_tasks # rubocop:disable Metrics/MethodLength
@@ -41,7 +41,7 @@ exception). Stop and tell the user how to fix any that fail:
41
41
  fall back to another mechanism.
42
42
  5. Read `Procfile` — note whether a `worker` process exists.
43
43
  6. Read `config/initializers/sentry.rb` — determine how the DSN is consumed:
44
- - reads `ENV['SENTRY_DSN']` → **env-var mode** (RoleModel standard)
44
+ - reads `Rails.configuration.x.sentry.dsn` or `ENV['SENTRY_DSN']` → **env-var mode** (RoleModel standard)
45
45
  - reads `Rails.application.credentials...` → **credentials mode**
46
46
  7. Deploy-readiness (recent `rolemodel-rails` bakes these in; older apps need the manual
47
47
  fixes noted in each phase). Confirm the generated app already has:
@@ -3,7 +3,6 @@
3
3
  module Rolemodel
4
4
  module Linters
5
5
  class EslintGenerator < GeneratorBase
6
- include ReplaceContentHelper
7
6
  source_root File.expand_path('templates', __dir__)
8
7
 
9
8
  ESLINT_COMMAND = "eslint 'app/**/*.js' 'spec/**/*.js'"
@@ -70,5 +70,22 @@ Style/EachWithObject:
70
70
  Style/Documentation:
71
71
  Enabled: false
72
72
 
73
+ Style/ItBlockParameter:
74
+ EnforcedStyle: always
75
+
76
+ Style/EndlessMethod:
77
+ EnforcedStyle: require_single_line
78
+
79
+ Style/TrailingCommaInArrayLiteral:
80
+ EnforcedStyleForMultiline: consistent_comma
81
+
82
+ Style/TrailingCommaInHashLiteral:
83
+ EnforcedStyleForMultiline: consistent_comma
84
+
85
+ # Parent and child setters keep inverted index/new/create conditions,
86
+ # even when the controller doesn't define all three.
87
+ Rails/LexicallyScopedActionFilter:
88
+ Enabled: false
89
+
73
90
  Cops/NoChromeTag:
74
91
  AutoCorrect: contextual
@@ -17,60 +17,40 @@ module Rolemodel
17
17
  after: /^\s*require\('postcss-preset-env'\)\({(.|\n)*?}\)/
18
18
  end
19
19
 
20
- def add_action_mailer_asset_host
21
- unless File.exist?(Rails.root.join('config/initializers/devise.rb'))
22
- inject_into_file 'config/environments/development.rb',
23
- after: "config.action_mailer.perform_caching = false\n" do
24
- optimize_indentation <<~'RUBY', 2
25
-
26
- # Default mailing host suggested by Devise installation instructions
27
- config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
28
- RUBY
29
- end
30
- end
31
-
32
- inject_into_file 'config/environments/development.rb',
33
- after: "config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }\n" do
34
- optimize_indentation <<~'RUBY', 2
35
-
36
- # Ensure premailer_rails can point to webpack compiled resources
37
- # https://github.com/fphilipe/premailer-rails/issues/232#issuecomment-839819705
38
- config.action_mailer.asset_host = 'http://localhost:3000'
39
- RUBY
40
- end
20
+ # Email links and assets use DEFAULT_URL in every environment. The asset host lets
21
+ # premailer_rails find webpack compiled resources:
22
+ # https://github.com/fphilipe/premailer-rails/issues/232#issuecomment-839819705
23
+ def use_default_url
24
+ add_default_url
41
25
  end
42
26
 
43
27
  def add_production_mailer_defaults
28
+ add_setting "config.x.whitelisted_emails = EnvValue.array('WHITELISTED_EMAILS')"
29
+ add_setting "config.x.heroku_app_name = EnvValue.string('HEROKU_APP_NAME')"
30
+ add_setting "config.x.sendgrid.api_key = EnvValue.string('SENDGRID_API_KEY')"
31
+
32
+ copy_file 'app/mailers/staging_mailer_interceptor.rb'
44
33
  prepend_to_file 'config/environments/production.rb',
45
34
  "require Rails.root.join('app/mailers/staging_mailer_interceptor')\n"
46
35
 
47
- inject_into_file 'config/environments/production.rb', after: "config.action_mailer.perform_caching = false\n" do
48
- optimize_indentation <<~'RUBY', 2
49
- # Prevent live user emails from being sent out in staging
50
- if ENV['WHITELISTED_EMAILS'].present?
51
- ActionMailer::Base.register_interceptor(StagingMailerInterceptor)
52
- end
53
-
54
- host = ENV['PRODUCTION_HOST']
55
-
56
- # Ensure premailer_rails can point to webpack compiled resources
57
- # https://github.com/fphilipe/premailer-rails/issues/232#issuecomment-839819705
58
- config.action_mailer.asset_host = host
36
+ append_to_environment 'production', <<~'RUBY'
37
+ # Prevent live user emails from being sent out in staging
38
+ if config.x.whitelisted_emails.any?
39
+ ActionMailer::Base.register_interceptor(StagingMailerInterceptor)
40
+ end
59
41
 
60
- config.action_mailer.default_url_options = { host: host }
61
- config.action_mailer.delivery_method = :smtp
62
- config.action_mailer.perform_deliveries = true
63
- config.action_mailer.smtp_settings = {
64
- user_name: 'apikey',
65
- password: ENV['SENDGRID_API_KEY'],
66
- domain: host,
67
- address: 'smtp.sendgrid.net',
68
- port: 587,
69
- authentication: :plain,
70
- enable_starttls_auto: true
71
- }
72
- RUBY
73
- end
42
+ config.action_mailer.delivery_method = :smtp
43
+ config.action_mailer.perform_deliveries = true
44
+ config.action_mailer.smtp_settings = {
45
+ user_name: 'apikey',
46
+ password: config.x.sendgrid.api_key,
47
+ domain: URI(config.x.default_url).host,
48
+ address: 'smtp.sendgrid.net',
49
+ port: 587,
50
+ authentication: :plain,
51
+ enable_starttls_auto: true
52
+ }
53
+ RUBY
74
54
  end
75
55
 
76
56
  def add_mailer_css
@@ -1,8 +1,7 @@
1
1
  class StagingMailerInterceptor
2
2
  def self.delivering_email(message)
3
- whitelisted_emails = ENV['WHITELISTED_EMAILS'].split(',').map(&:strip)
4
- message.to = message.to & whitelisted_emails
5
- subject_name = ENV['HEROKU_APP_NAME'].presence || 'STAGING'
3
+ message.to = message.to & Rails.configuration.x.whitelisted_emails
4
+ subject_name = Rails.configuration.x.heroku_app_name || 'STAGING'
6
5
  message.subject = "#{subject_name} - #{message.subject}"
7
6
  message.perform_deliveries = false if message.to.blank?
8
7
  end
@@ -36,6 +36,9 @@ gem install bundler
36
36
  bundle install
37
37
  \```
38
38
 
39
+ ## Local settings
40
+ Put local settings in `.env.local` in the project root, as `KEY=value` lines. Git ignores this file. Restart the server after you change it.
41
+
39
42
  ## How to run locally
40
43
  `rails s`
41
44
 
@@ -79,28 +79,8 @@ module Rolemodel
79
79
  end
80
80
 
81
81
  def modify_existing_files
82
- unless File.exist?(Rails.root.join('config/initializers/premailer_rails.rb'))
83
- inject_into_file 'config/environments/development.rb',
84
- after: "config.action_mailer.perform_caching = false\n" do
85
- optimize_indentation <<~'RUBY', 2
86
-
87
- # Default mailing host suggested by Devise installation instructions
88
- config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
89
- RUBY
90
- end
91
- end
92
-
93
- inject_into_file 'config/environments/production.rb', after: "config.action_mailer.perform_caching = false\n" do
94
- optimize_indentation <<~'RUBY', 2
95
-
96
- # Tell Devise about your host, so it can send password reset emails
97
- if ENV['REVIEW_APP'] == 'true' && ENV['HEROKU_APP_NAME'].present?
98
- config.action_mailer.default_url_options = { host: "https://#{ENV['HEROKU_APP_NAME']}.herokuapp.com" }
99
- else
100
- config.action_mailer.default_url_options = { host: ENV['PRODUCTION_HOST'] }
101
- end
102
- RUBY
103
- end
82
+ # Devise links in emails, such as password resets, use DEFAULT_URL.
83
+ add_default_url
104
84
 
105
85
  # Update Devise email_regexp to something more useful
106
86
  gsub_file 'config/initializers/devise.rb', '/\A[^@\s]+@[^@\s]+\z/',
@@ -111,7 +91,7 @@ module Rolemodel
111
91
  'config.validate_on_invite = true'
112
92
  end
113
93
 
114
- inject_into_file 'app/models/user.rb', after: /devise\s+:.*\n.*\n/ do
94
+ inject_into_class 'app/models/user.rb', 'User', at: :end do
115
95
  optimize_indentation <<~'RUBY', 2
116
96
 
117
97
  enum :role, { user: 'user', admin: 'admin' }
@@ -28,6 +28,7 @@ module Rolemodel
28
28
  def add_ruby_initializer
29
29
  say 'Setting up Sentry for Ruby error reporting', :green
30
30
 
31
+ add_setting "config.x.sentry.dsn = EnvValue.string('SENTRY_DSN')"
31
32
  copy_file 'config/initializers/sentry.rb'
32
33
  end
33
34
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Sentry.init do |config|
4
- config.dsn = ENV['SENTRY_DSN']
4
+ config.dsn = Rails.configuration.x.sentry.dsn
5
5
  config.debug = Rails.env.local?
6
6
 
7
7
  # Traces sampling — 5% for HTTP requests, 10% for other transactions
@@ -10,4 +10,4 @@ Adds Rack middleware for serving js source maps only to allowed users in a produ
10
10
  ## Usage
11
11
 
12
12
  * Ensure that app uses Warden::Manager middleware
13
- * Set ENV['SOURCE_MAPS_ALLOWED_USERS_EMAILS'] to a comma-separated list of emails or update the middleware to allow all super admins
13
+ * Set `SOURCE_MAPS_ALLOWED_USERS_EMAILS` to a comma-separated list of emails. The middleware reads it from `Rails.configuration.x.source_maps.allowed_user_emails`. To allow all super admins instead, change `permit_user?` in the middleware.
@@ -5,13 +5,17 @@ module Rolemodel
5
5
  source_root File.expand_path('templates', __dir__)
6
6
 
7
7
  def inject_config_for_production
8
- inject_into_file 'config/environments/production.rb',
9
- "\nrequire_relative Rails.root.join('lib/middleware/rolemodel/source_map.rb')\n", after: "require \"active_support/core_ext/integer/time\"\n"
10
- inject_into_file 'config/environments/production.rb',
11
- "\n config.middleware.insert_after Warden::Manager, Middleware::Rolemodel::SourceMap\n\n", after: "Rails.application.configure do\n"
8
+ prepend_to_file(
9
+ 'config/environments/production.rb',
10
+ "require_relative Rails.root.join('lib/middleware/rolemodel/source_map.rb')\n"
11
+ )
12
+ append_to_environment 'production', <<~'RUBY'
13
+ config.middleware.insert_after Warden::Manager, Middleware::Rolemodel::SourceMap
14
+ RUBY
12
15
  end
13
16
 
14
17
  def copy_middleware_files
18
+ add_setting "config.x.source_maps.allowed_user_emails = EnvValue.array('SOURCE_MAPS_ALLOWED_USERS_EMAILS')"
15
19
  copy_file 'lib/middleware/rolemodel/source_map.rb'
16
20
  end
17
21
 
@@ -8,7 +8,7 @@ module Middleware
8
8
  default_headers = { 'Set-Cookie' => 'Same-Site=None', 'Cache-Control' => 'max-age=0;no-cache' }
9
9
  custom_headers = default_headers.merge(options.delete(:headers) || {})
10
10
  @app = app
11
- @allowed_users_emails = ENV.fetch('SOURCE_MAPS_ALLOWED_USERS_EMAILS', '').split(',')
11
+ @allowed_users_emails = Rails.configuration.x.source_maps.allowed_user_emails
12
12
  @file_server = Rack::Files.new(root, custom_headers)
13
13
  end
14
14
 
@@ -3,7 +3,6 @@
3
3
  module Rolemodel
4
4
  module Testing
5
5
  class JasminePlaywrightGenerator < GeneratorBase
6
- include ReplaceContentHelper
7
6
  source_root File.expand_path('templates', __dir__)
8
7
  class_option :github_package_token, type: :string, default: ENV['GITHUB_PACKAGES_TOKEN'], desc: 'GitHub Packages token with access to @rolemodel packages'
9
8
 
@@ -3,7 +3,6 @@
3
3
  module Rolemodel
4
4
  module Testing
5
5
  class VitestGenerator < GeneratorBase
6
- include ReplaceContentHelper
7
6
  source_root File.expand_path('templates', __dir__)
8
7
 
9
8
  TEST_COMMAND = 'NODE_ENV=test vitest'
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'rails'
4
+
3
5
  module Rolemodel
4
6
  class Engine < ::Rails::Engine
5
7
  require_relative 'generator_base'