Skip to content

Commit 12ed7db

Browse files
crackofduskstevepolitodesign
authored andcommitted
Introduce suspenders:development:environment generator
Creates generator to configure the development environment. Keeps parity with Rails as much as possible in an effort to avoid drift with Rails standards. It's possible a future release of Rails will alleviate us from having to do the following: - enable `active_model.i18n_customize_full_message` which is being addressed in [#50406][] - enable `active_record.query_log_tags_enabled` which is being addressed in [#51342][] [#50406]: rails/rails#50406 [#51342]: rails/rails#51342
1 parent f6c6f45 commit 12ed7db

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Unreleased
1818
* Introduce `suspenders:prerequisites` generator
1919
* Introduce `suspenders:ci` generator
2020
* Introduce `suspenders:cleanup:organize_gemfile` task
21+
* Introduce `suspenders:development` generator
2122

2223
20230113.0 (January, 13, 2023)
2324

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,26 @@ Creates CI files for GitHub Actions.
197197
bin/rails g suspenders:ci
198198
```
199199

200+
### Environments
201+
202+
#### Development
203+
204+
Configures the development environment.
205+
206+
```
207+
bin/rails g suspenders:development:evironment
208+
```
209+
210+
- Enables [raise_on_missing_translations][]
211+
- Enables [annotate_rendered_view_with_filenames][]
212+
- Enables [i18n_customize_full_message][]
213+
- Enables [query_log_tags_enabled][]
214+
215+
[raise_on_missing_translations]: https://guides.rubyonrails.org/configuring.html#config-i18n-raise-on-missing-translations
216+
[annotate_rendered_view_with_filenames]: https://guides.rubyonrails.org/configuring.html#config-action-view-annotate-rendered-view-with-filenames
217+
[i18n_customize_full_message]: https://guides.rubyonrails.org/configuring.html#config-active-model-i18n-customize-full-message
218+
[query_log_tags_enabled]: https://guides.rubyonrails.org/configuring.html#config-active-record-query-log-tags-enabled
219+
200220
## Contributing
201221

202222
See the [CONTRIBUTING] document.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module Suspenders
2+
module Generators
3+
module Development
4+
class EnvironmentGenerator < Rails::Generators::Base
5+
desc <<~MARKDOWN
6+
Configures the development environment.
7+
8+
- Enables [raise_on_missing_translations][]
9+
- Enables [annotate_rendered_view_with_filenames][]
10+
- Enables [i18n_customize_full_message][]
11+
- Enables [query_log_tags_enabled][]
12+
13+
[raise_on_missing_translations]: https://guides.rubyonrails.org/configuring.html#config-i18n-raise-on-missing-translations
14+
[annotate_rendered_view_with_filenames]: https://guides.rubyonrails.org/configuring.html#config-action-view-annotate-rendered-view-with-filenames
15+
[i18n_customize_full_message]: https://guides.rubyonrails.org/configuring.html#config-active-model-i18n-customize-full-message
16+
[query_log_tags_enabled]: https://guides.rubyonrails.org/configuring.html#config-active-record-query-log-tags-enabled
17+
MARKDOWN
18+
19+
def raise_on_missing_translations
20+
if development_config.match?(/^\s#\s*config\.i18n\.raise_on_missing_translations/)
21+
uncomment_lines "config/environments/development.rb", "config.i18n.raise_on_missing_translations = true"
22+
else
23+
environment %(config.i18n.raise_on_missing_translations = true), env: "development"
24+
end
25+
end
26+
27+
def annotate_render_view_with_filename
28+
if development_config.match?(/^\s#\s*config\.action_view\.annotate_render_view_with_filename/)
29+
uncomment_lines "config/environments/development.rb",
30+
"config.action_view.annotate_rendered_view_with_filenames = true"
31+
else
32+
environment %(config.action_view.annotate_rendered_view_with_filenames = true), env: "development"
33+
end
34+
end
35+
36+
def enable_i18n_customize_full_message
37+
environment %(config.active_model.i18n_customize_full_message = true), env: "development"
38+
end
39+
40+
def enable_query_log_tags_enabled
41+
environment %(config.active_record.query_log_tags_enabled = true), env: "development"
42+
end
43+
44+
private
45+
46+
def development_config
47+
File.read(Rails.root.join("config/environments/development.rb"))
48+
end
49+
end
50+
end
51+
end
52+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Rails.application.configure do
2+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
require "test_helper"
2+
require "generators/suspenders/development/environment_generator"
3+
4+
module Suspenders
5+
module Generators
6+
module Development
7+
class EnvironmentGenerator::DefaultTest < Rails::Generators::TestCase
8+
include Suspenders::TestHelpers
9+
10+
tests Suspenders::Generators::Development::EnvironmentGenerator
11+
destination Rails.root
12+
setup :prepare_destination
13+
teardown :restore_destination
14+
15+
test "raise on missing translations" do
16+
run_generator
17+
18+
assert_file app_root("config/environments/development.rb") do |file|
19+
assert_match(
20+
/^ +config.i18n.raise_on_missing_translations = true$/,
21+
file
22+
)
23+
end
24+
end
25+
26+
test "raise on missing translations (when config is not commented out)" do
27+
content = file_fixture("environments/development.rb").read
28+
remove_file_if_exists "config/environments/development.rb"
29+
touch "config/environments/development.rb", content: content
30+
31+
run_generator
32+
33+
assert_file app_root("config/environments/development.rb") do |file|
34+
assert_match(
35+
/^ +config.i18n.raise_on_missing_translations = true$/,
36+
file
37+
)
38+
end
39+
end
40+
41+
test "annotate rendered view with file names" do
42+
run_generator
43+
44+
assert_file app_root("config/environments/development.rb") do |file|
45+
assert_match(
46+
/^ +config.action_view.annotate_rendered_view_with_filenames = true$/,
47+
file
48+
)
49+
end
50+
end
51+
52+
test "annotate rendered view with file names (when config is not commented out)" do
53+
content = file_fixture("environments/development.rb").read
54+
remove_file_if_exists "config/environments/development.rb"
55+
touch "config/environments/development.rb", content: content
56+
57+
run_generator
58+
59+
assert_file app_root("config/environments/development.rb") do |file|
60+
assert_match(
61+
/^ +config.action_view.annotate_rendered_view_with_filenames = true$/,
62+
file
63+
)
64+
end
65+
end
66+
67+
test "enable active_model.i18n_customize_full_message" do
68+
run_generator
69+
70+
assert_file app_root("config/environments/development.rb") do |file|
71+
assert_match(/^\s*config\.active_model\.i18n_customize_full_message\s*=\s*true/, file)
72+
end
73+
end
74+
75+
test "enable active_record.query_log_tags_enabled" do
76+
run_generator
77+
78+
assert_file app_root("config/environments/development.rb") do |file|
79+
assert_match(/^\s*config\.active_record\.query_log_tags_enabled\s*=\s*true/, file)
80+
end
81+
end
82+
83+
private
84+
85+
def prepare_destination
86+
backup_file "config/environments/development.rb"
87+
end
88+
89+
def restore_destination
90+
restore_file "config/environments/development.rb"
91+
end
92+
end
93+
end
94+
end
95+
end

0 commit comments

Comments
 (0)