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
@@ -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'"
@@ -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'
@@ -5,5 +5,6 @@
5
5
  ## What you get
6
6
 
7
7
  * [Flash](./flash)
8
- * [Modals](./modals)
9
8
  * [Navbar](./navbar)
9
+
10
+ Modals moved to the [Turbo modals](../turbo/modals) generator.
@@ -0,0 +1,191 @@
1
+ require 'prism'
2
+
3
+ module Rolemodel
4
+ module FileContentHelper
5
+ # Finds a block for inject_into_block. `pattern` matches a Prism node by its structure, not its text.
6
+ # `example` is code the pattern matches. Its first line describes the matcher in error messages,
7
+ # and a spec checks that the pattern matches it. Build one with for_class, for_module or for_call.
8
+ BlockMatcher = Data.define(:example, :pattern) do
9
+ # A matcher for a class, by its name ('User', 'Admin::User'), its superclass
10
+ # ('Rails::Application'), or both. A leading :: on either name does not matter.
11
+ def self.for_class(name: nil, superclass: nil)
12
+ parent = constant_argument(superclass)
13
+ definition(Prism::ClassNode, 'class', name, suffix: (" < #{parent}" if parent)) do |node|
14
+ parent.nil? || constant_name(node.superclass) == parent
15
+ end
16
+ end
17
+
18
+ # A matcher for a module, by its name ('Admin', 'Admin::Reports'). A leading :: does not matter.
19
+ def self.for_module(name)
20
+ definition(Prism::ModuleNode, 'module', name)
21
+ end
22
+
23
+ # A matcher for a method call with a do ... end block, given as the names in its chain, such as
24
+ # for_call(:Rails, :application, :configure). The first name may be a constant, such as :Rails or
25
+ # 'Admin::Settings', with or without a leading ::. Without one, the chain must start with a call
26
+ # that has no receiver, as in for_call(:configure). It does not compare arguments.
27
+ def self.for_call(first, *rest)
28
+ names = [constant_argument(first), *rest.map(&:to_s)]
29
+ new(
30
+ example: "#{names.join('.')} do\nend\n",
31
+ pattern: lambda do |node|
32
+ node.is_a?(Prism::CallNode) &&
33
+ node.block.is_a?(Prism::BlockNode) &&
34
+ same_chain?(node, names)
35
+ end
36
+ )
37
+ end
38
+
39
+ # A matcher for a class or module definition: a node of `type` whose name is `name`, when given,
40
+ # and that passes the block, when given.
41
+ def self.definition(type, keyword, name, suffix: nil, &extra)
42
+ name = constant_argument(name)
43
+ new(
44
+ example: "#{keyword} #{name || 'Example'}#{suffix}\nend\n",
45
+ pattern: lambda do |node|
46
+ node.is_a?(type) &&
47
+ (name.nil? || constant_name(node.constant_path) == name) &&
48
+ (extra.nil? || extra.call(node))
49
+ end
50
+ )
51
+ end
52
+
53
+ # A constant name given to a factory, as a String without a leading ::, or nil when not given.
54
+ def self.constant_argument(name)
55
+ name&.to_s&.delete_prefix('::')
56
+ end
57
+
58
+ # A constant's full name without a leading ::, or nil when the node is not a plain constant,
59
+ # such as `Struct.new(:x)` or `foo::Bar`.
60
+ def self.constant_name(node)
61
+ node.full_name.delete_prefix('::') if node.respond_to?(:full_name)
62
+ rescue Prism::ConstantPathNode::DynamicPartsInConstantPathError
63
+ nil
64
+ end
65
+
66
+ # Whether `node` and its receivers are the chain `names`, read from the end: each call's name
67
+ # in turn, then either a constant with the first name or no receiver at all.
68
+ def self.same_chain?(node, names)
69
+ return node.nil? if names.empty?
70
+
71
+ *rest, last = names
72
+ if node.is_a?(Prism::CallNode)
73
+ node.name.to_s == last && same_chain?(node.receiver, rest)
74
+ else
75
+ rest.empty? && constant_name(node) == last
76
+ end
77
+ end
78
+ private_class_method :definition, :constant_argument, :constant_name, :same_chain?
79
+
80
+ def call(node) = pattern.call(node)
81
+ def to_s = example.lines.first.strip
82
+
83
+ # The first node in `source` that the pattern matches, or nil.
84
+ def find_in(source) = Prism.parse(source).value.breadth_first_search { call(it) }
85
+ end
86
+
87
+ private
88
+
89
+ def replace_content(relative_path)
90
+ source = File.expand_path(relative_path.to_s, destination_root)
91
+ content = File.exist?(source) ? File.open(source) { it.binmode.read } : ''
92
+
93
+ remove_file relative_path
94
+ create_file relative_path, yield(content)
95
+ end
96
+
97
+ def modify_json_file(relative_path)
98
+ replace_content(relative_path) do |content|
99
+ # Editor config files such as .vscode/*.json allow comments and trailing commas.
100
+ # Writing the file back drops the comments.
101
+ hash = JSON.parse(
102
+ content.presence || '{}',
103
+ allow_comments: true,
104
+ allow_trailing_comma: true
105
+ )
106
+ yield(hash)
107
+ JSON.pretty_generate(hash) + "\n"
108
+ end
109
+ end
110
+
111
+ def add_package_json_script(command_name, command)
112
+ modify_json_file('package.json') do |hash|
113
+ hash['scripts'] ||= {}
114
+ hash['scripts'][command_name] = command
115
+ hash
116
+ end
117
+ end
118
+
119
+ # Adds code as the last lines of the `Rails.application.configure` block in an environment file,
120
+ # indented 2 spaces, so it overrides the Rails defaults above it.
121
+ # Rails' own `environment(code, env:)` adds it at the top instead.
122
+ def append_to_environment(env, code)
123
+ inject_into_block(
124
+ "config/environments/#{env}.rb",
125
+ BlockMatcher.for_call(:Rails, :application, :configure),
126
+ optimize_indentation(code, 2),
127
+ at: :end
128
+ )
129
+ end
130
+
131
+ # Overrides Thor's inject_into_class, with the same arguments and options. See inject_into_block.
132
+ def inject_into_class(path, klass, *args, &block)
133
+ inject_into_block(path, BlockMatcher.for_class(name: klass), *args, &block)
134
+ end
135
+
136
+ # Overrides Thor's inject_into_module, with the same arguments and options. See inject_into_block.
137
+ def inject_into_module(path, module_name, *args, &block)
138
+ inject_into_block(path, BlockMatcher.for_module(module_name), *args, &block)
139
+ end
140
+
141
+ # Adds data inside the first block in a file that `matcher`, a BlockMatcher, accepts. A block is any
142
+ # code that closes with `end`: a class, module, method, or a do ... end block.
143
+ #
144
+ # Works like Thor's inject_into_class: data as a string or a block, the :verbose and :force options,
145
+ # and no change to the data's indentation. It adds one option: at: :start (the default) adds the data
146
+ # right after the block's opening line, and at: :end adds it just before the block's `end`.
147
+ #
148
+ # Prism finds the block, so spacing, comments and code around it do not change where the data goes,
149
+ # and it raises when the block is missing, where Thor prints "unchanged" and goes on. Thor's
150
+ # insert_into_file still writes the data, so it skips data the file already has, honors --pretend,
151
+ # and `rails destroy` can remove it.
152
+ def inject_into_block(path, matcher, *args, &block)
153
+ config = args.extract_options!
154
+ at = config.delete(:at) || :start
155
+ unless %i[start end].include?(at)
156
+ raise ArgumentError, "at: must be :start or :end, not #{at.inspect}"
157
+ end
158
+
159
+ content = File.read(File.join(destination_root, path))
160
+ opening_line, closing = block_lines(matcher.find_in(content))
161
+ unless closing
162
+ raise Thor::Error, "Could not find `#{matcher}` with `end` on its own line in #{path}"
163
+ end
164
+
165
+ lines_before = content.lines.first(at == :start ? opening_line : closing.start_line - 1).join
166
+ config[:after] = /\A#{Regexp.escape(lines_before)}/
167
+ insert_into_file(path, *(args << config), &block)
168
+ end
169
+
170
+ # For a block, the last line of its opening (such as `class User < ApplicationRecord`, or
171
+ # `configure do |config|` when the opening wraps), and the location of the `end` that closes it.
172
+ # Returns nil unless the `end` keyword starts its own line. A `{ }` block has no `end`.
173
+ def block_lines(node)
174
+ opening, closing = case node
175
+ in Prism::ClassNode
176
+ [node.superclass || node.constant_path, node.end_keyword_loc]
177
+ in Prism::ModuleNode
178
+ [node.constant_path, node.end_keyword_loc]
179
+ in Prism::DefNode
180
+ [node.rparen_loc || node.name_loc, node.end_keyword_loc]
181
+ in Prism::CallNode[block: Prism::BlockNode => block]
182
+ [block.parameters || block.opening_loc, block.closing_loc]
183
+ else return
184
+ end
185
+ return unless closing.slice == 'end' && closing.start_line_slice.strip.empty?
186
+
187
+ opening = opening.location if opening.respond_to?(:location)
188
+ [opening.end_line, closing]
189
+ end
190
+ end
191
+ end
@@ -2,13 +2,13 @@
2
2
 
3
3
  require 'rails/generators'
4
4
  require 'rails/generators/bundle_helper'
5
- require_relative 'replace_content_helper'
5
+ require_relative 'file_content_helper'
6
6
  require_relative 'skills_helper'
7
7
  require 'rolemodel/yarn'
8
8
 
9
9
  module Rolemodel
10
10
  class GeneratorBase < ::Rails::Generators::Base
11
- include ::Rails::Generators::BundleHelper, ReplaceContentHelper, SkillsHelper
11
+ include ::Rails::Generators::BundleHelper, FileContentHelper, SkillsHelper
12
12
 
13
13
  private
14
14
 
@@ -38,6 +38,45 @@ module Rolemodel
38
38
  invoke 'rolemodel:yarn:setup', [], options.slice(:pretend)
39
39
  end
40
40
 
41
+ # Adds a setting that reads ENV through EnvValue as the last line of `class Application`
42
+ # in config/application.rb, for example
43
+ # add_setting("config.x.sentry.dsn = EnvValue.string('SENTRY_DSN')").
44
+ # Runs rolemodel:dotenv first when the app has no EnvValue yet.
45
+ def add_setting(line)
46
+ ensure_env_value
47
+ inject_into_class 'config/application.rb', 'Application', optimize_indentation(line, 4), at: :end
48
+ end
49
+
50
+ # Sets every URL built outside a request, such as links and assets in emails, from DEFAULT_URL.
51
+ # Heroku apps with no DEFAULT_URL, such as review apps, use their herokuapp.com address,
52
+ # and local apps use http://localhost:3000. Safe to call from several generators.
53
+ def add_default_url
54
+ # Rails splits a full URL given as host: into its protocol, host and port.
55
+ add_setting <<~'RUBY'
56
+ # Links in emails, and URLs built outside a request, use DEFAULT_URL.
57
+ config.x.default_url = EnvValue.url('DEFAULT_URL') do
58
+ heroku_app = EnvValue.string('HEROKU_APP_NAME')
59
+ heroku_app ? "https://#{heroku_app}.herokuapp.com" : 'http://localhost:3000'
60
+ end
61
+ config.action_mailer.default_url_options = { host: config.x.default_url }
62
+ config.action_mailer.asset_host = config.x.default_url
63
+ Rails.application.routes.default_url_options = config.action_mailer.default_url_options
64
+ RUBY
65
+
66
+ # New Rails apps set their own default_url_options in development.rb and production.rb, and
67
+ # those files run after application.rb, so they would win over DEFAULT_URL. Comment them out.
68
+ # test.rb keeps its own, so tests still build URLs for example.com.
69
+ %w[development production].each do |env|
70
+ comment_lines "config/environments/#{env}.rb", /config\.action_mailer\.default_url_options\s*=/
71
+ end
72
+ end
73
+
74
+ def ensure_env_value
75
+ return if File.exist?(File.join(destination_root, 'lib/env_value.rb'))
76
+
77
+ invoke 'rolemodel:dotenv', [], options.slice(:pretend)
78
+ end
79
+
41
80
  def application_stylesheet_path
42
81
  Dir.glob('app/assets/stylesheets/application.*').first
43
82
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ gem 'rolemodel-rails'
4
+
5
+ after_bundle do
6
+ rails_command 'generate rolemodel:core_setup'
7
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/command'
4
+ require 'rails/commands/application/application_command'
5
+
6
+ module Rolemodel
7
+ module NewApp
8
+ DEFAULTS = %W[
9
+ --database=postgresql
10
+ --javascript=webpack
11
+ --skip-kamal
12
+ --skip-solid
13
+ --skip-test
14
+ --template=#{File.expand_path('new_app/template.rb', __dir__)}
15
+ ].freeze
16
+
17
+ def self.argv(argv)
18
+ scrubbed = Rails::Generators::ARGVScrubber.new(argv).prepare!
19
+ scrubbed.take(1) + DEFAULTS + scrubbed.drop(1)
20
+ end
21
+ end
22
+ end
@@ -62,7 +62,7 @@ module Rolemodel
62
62
  end
63
63
 
64
64
  def to_h
65
- @args.transform_values { it.to_s.strip.match?('_') ? nil : it.to_s.strip }.compact
65
+ @args.transform_values { it.to_s.strip.presence unless it.to_s.strip == '_' }.compact
66
66
  end
67
67
  end
68
68
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rolemodel
4
- VERSION = '2.7.0'
4
+ VERSION = '2.8.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rolemodel-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.0
4
+ version: 2.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - RoleModel Software Inc
@@ -181,23 +181,29 @@ description: A collection of executable best practices for common aspects of Rai
181
181
  application development.
182
182
  email:
183
183
  - it-support@rolemodelsoftware.com
184
- executables: []
184
+ executables:
185
+ - rmr
185
186
  extensions: []
186
187
  extra_rdoc_files: []
187
188
  files:
188
189
  - LICENSE.txt
189
190
  - README.md
190
191
  - Rakefile
192
+ - exe/rmr
191
193
  - lib/generators/rolemodel/README.md
192
194
  - lib/generators/rolemodel/USAGE
193
195
  - lib/generators/rolemodel/all_generator.rb
194
196
  - lib/generators/rolemodel/core_setup/README.md
195
197
  - lib/generators/rolemodel/core_setup/USAGE
196
198
  - lib/generators/rolemodel/core_setup/core_setup_generator.rb
199
+ - lib/generators/rolemodel/dotenv/README.md
200
+ - lib/generators/rolemodel/dotenv/USAGE
201
+ - lib/generators/rolemodel/dotenv/dotenv_generator.rb
202
+ - lib/generators/rolemodel/dotenv/templates/lib/env_value.rb
203
+ - lib/generators/rolemodel/dotenv/templates/spec/lib/env_value_spec.rb
197
204
  - lib/generators/rolemodel/editors/README.md
198
205
  - lib/generators/rolemodel/editors/USAGE
199
206
  - lib/generators/rolemodel/editors/editors_generator.rb
200
- - lib/generators/rolemodel/editors/vscode_helpers.rb
201
207
  - lib/generators/rolemodel/github/README.md
202
208
  - lib/generators/rolemodel/github/USAGE
203
209
  - lib/generators/rolemodel/github/github_generator.rb
@@ -449,7 +455,10 @@ files:
449
455
  - lib/generators/rolemodel/webpack/webpack_generator.rb
450
456
  - lib/rolemodel-rails.rb
451
457
  - lib/rolemodel/engine.rb
458
+ - lib/rolemodel/file_content_helper.rb
452
459
  - lib/rolemodel/generator_base.rb
460
+ - lib/rolemodel/new_app.rb
461
+ - lib/rolemodel/new_app/template.rb
453
462
  - lib/rolemodel/optics.rb
454
463
  - lib/rolemodel/optics/custom_icon_builder.rb
455
464
  - lib/rolemodel/optics/feather_icon_builder.rb
@@ -458,7 +467,6 @@ files:
458
467
  - lib/rolemodel/optics/material_icon_builder.rb
459
468
  - lib/rolemodel/optics/phosphor_icon_builder.rb
460
469
  - lib/rolemodel/optics/tabler_icon_builder.rb
461
- - lib/rolemodel/replace_content_helper.rb
462
470
  - lib/rolemodel/resource_for/controller_extension.rb
463
471
  - lib/rolemodel/skills_helper.rb
464
472
  - lib/rolemodel/utility.rb