Skip to content

Commit

Permalink
Introduce suspenders:development:environment generator (#1149)
Browse files Browse the repository at this point in the history
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

Co-authored-by: Steve Polito <[email protected]>
  • Loading branch information
crackofdusk and stevepolitodesign authored Apr 5, 2024
1 parent 5a733e0 commit 7d666ca
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 6 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Unreleased
* Introduce `suspenders:cleanup:organize_gemfile` task
* Introduce `suspenders:production:environment` generator
* Introduce `suspenders:test:environment` generator
* Introduce `suspenders:development:environment` generator

20230113.0 (January, 13, 2023)

Expand Down
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ bin/rails g suspenders:ci

### Environments

<<<<<<< HEAD
#### Production

Configures the production environment.
Expand All @@ -208,11 +207,7 @@ Configures the production environment.

[require_master_key]: https://guides.rubyonrails.org/configuring.html#config-require-master-key

```
bin/rails g suspenders:production:environment
```

=======
#### Test

Configures test environment.
Expand All @@ -227,7 +222,24 @@ bin/rails g suspenders:test:environment
[raise_on_missing_translations]: https://guides.rubyonrails.org/configuring.html#config-i18n-raise-on-missing-translations
[action_dispatch.show_exceptions]: https://edgeguides.rubyonrails.org/configuring.html#config-action-dispatch-show-exceptions

>>>>>>> 9102851 (Introduce `suspenders:test:environment` generator)
#### Development

Configures the development environment.

```
bin/rails g suspenders:development:evironment
```

- Enables [raise_on_missing_translations][]
- Enables [annotate_rendered_view_with_filenames][]
- Enables [i18n_customize_full_message][]
- Enables [query_log_tags_enabled][]

[raise_on_missing_translations]: https://guides.rubyonrails.org/configuring.html#config-i18n-raise-on-missing-translations
[annotate_rendered_view_with_filenames]: https://guides.rubyonrails.org/configuring.html#config-action-view-annotate-rendered-view-with-filenames
[i18n_customize_full_message]: https://guides.rubyonrails.org/configuring.html#config-active-model-i18n-customize-full-message
[query_log_tags_enabled]: https://guides.rubyonrails.org/configuring.html#config-active-record-query-log-tags-enabled

## Contributing

See the [CONTRIBUTING] document.
Expand Down
52 changes: 52 additions & 0 deletions lib/generators/suspenders/development/environment_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module Suspenders
module Generators
module Development
class EnvironmentGenerator < Rails::Generators::Base
desc <<~MARKDOWN
Configures the development environment.
- Enables [raise_on_missing_translations][]
- Enables [annotate_rendered_view_with_filenames][]
- Enables [i18n_customize_full_message][]
- Enables [query_log_tags_enabled][]
[raise_on_missing_translations]: https://guides.rubyonrails.org/configuring.html#config-i18n-raise-on-missing-translations
[annotate_rendered_view_with_filenames]: https://guides.rubyonrails.org/configuring.html#config-action-view-annotate-rendered-view-with-filenames
[i18n_customize_full_message]: https://guides.rubyonrails.org/configuring.html#config-active-model-i18n-customize-full-message
[query_log_tags_enabled]: https://guides.rubyonrails.org/configuring.html#config-active-record-query-log-tags-enabled
MARKDOWN

def raise_on_missing_translations
if development_config.match?(/^\s#\s*config\.i18n\.raise_on_missing_translations/)
uncomment_lines "config/environments/development.rb", "config.i18n.raise_on_missing_translations = true"
else
environment %(config.i18n.raise_on_missing_translations = true), env: "development"
end
end

def annotate_render_view_with_filename
if development_config.match?(/^\s#\s*config\.action_view\.annotate_render_view_with_filename/)
uncomment_lines "config/environments/development.rb",
"config.action_view.annotate_rendered_view_with_filenames = true"
else
environment %(config.action_view.annotate_rendered_view_with_filenames = true), env: "development"
end
end

def enable_i18n_customize_full_message
environment %(config.active_model.i18n_customize_full_message = true), env: "development"
end

def enable_query_log_tags_enabled
environment %(config.active_record.query_log_tags_enabled = true), env: "development"
end

private

def development_config
File.read(Rails.root.join("config/environments/development.rb"))
end
end
end
end
end
2 changes: 2 additions & 0 deletions test/fixtures/files/environments/development.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Rails.application.configure do
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
require "test_helper"
require "generators/suspenders/development/environment_generator"

module Suspenders
module Generators
module Development
class EnvironmentGenerator::DefaultTest < Rails::Generators::TestCase
include Suspenders::TestHelpers

tests Suspenders::Generators::Development::EnvironmentGenerator
destination Rails.root
setup :prepare_destination
teardown :restore_destination

test "raise on missing translations" do
run_generator

assert_file app_root("config/environments/development.rb") do |file|
assert_match(
/^ +config.i18n.raise_on_missing_translations = true$/,
file
)
end
end

test "raise on missing translations (when config is not commented out)" do
content = file_fixture("environments/development.rb").read
remove_file_if_exists "config/environments/development.rb"
touch "config/environments/development.rb", content: content

run_generator

assert_file app_root("config/environments/development.rb") do |file|
assert_match(
/^ +config.i18n.raise_on_missing_translations = true$/,
file
)
end
end

test "annotate rendered view with file names" do
run_generator

assert_file app_root("config/environments/development.rb") do |file|
assert_match(
/^ +config.action_view.annotate_rendered_view_with_filenames = true$/,
file
)
end
end

test "annotate rendered view with file names (when config is not commented out)" do
content = file_fixture("environments/development.rb").read
remove_file_if_exists "config/environments/development.rb"
touch "config/environments/development.rb", content: content

run_generator

assert_file app_root("config/environments/development.rb") do |file|
assert_match(
/^ +config.action_view.annotate_rendered_view_with_filenames = true$/,
file
)
end
end

test "enable active_model.i18n_customize_full_message" do
run_generator

assert_file app_root("config/environments/development.rb") do |file|
assert_match(/^\s*config\.active_model\.i18n_customize_full_message\s*=\s*true/, file)
end
end

test "enable active_record.query_log_tags_enabled" do
run_generator

assert_file app_root("config/environments/development.rb") do |file|
assert_match(/^\s*config\.active_record\.query_log_tags_enabled\s*=\s*true/, file)
end
end

private

def prepare_destination
backup_file "config/environments/development.rb"
end

def restore_destination
restore_file "config/environments/development.rb"
end
end
end
end
end

0 comments on commit 7d666ca

Please sign in to comment.