diff --git a/NEWS.md b/NEWS.md index a61f8fcdc..4c13c26ef 100644 --- a/NEWS.md +++ b/NEWS.md @@ -18,6 +18,7 @@ Unreleased * Introduce `suspenders:prerequisites` generator * Introduce `suspenders:ci` generator * Introduce `suspenders:cleanup:organize_gemfile` task +* Introduce `suspenders:production:environment` generator 20230113.0 (January, 13, 2023) diff --git a/README.md b/README.md index 717ae40c1..77fd12c8e 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,20 @@ Creates CI files for GitHub Actions. bin/rails g suspenders:ci ``` +### Environments + +#### Production + +Configures the production environment. + + - Enables [require_master_key][] + +[require_master_key]: https://guides.rubyonrails.org/configuring.html#config-require-master-key + +``` +bin/rails g suspenders:production:environment +``` + ## Contributing See the [CONTRIBUTING] document. diff --git a/lib/generators/suspenders/production/environment_generator.rb b/lib/generators/suspenders/production/environment_generator.rb new file mode 100644 index 000000000..71b9f0d82 --- /dev/null +++ b/lib/generators/suspenders/production/environment_generator.rb @@ -0,0 +1,19 @@ +module Suspenders + module Generators + module Production + class EnvironmentGenerator < Rails::Generators::Base + desc <<~MARKDOWN + Configures the production environment. + + - Enables [require_master_key][] + + [require_master_key]: https://guides.rubyonrails.org/configuring.html#config-require-master-key + MARKDOWN + + def require_master_key + uncomment_lines "config/environments/production.rb", /config\.require_master_key\s*=\s*true/ + end + end + end + end +end diff --git a/test/generators/suspenders/production/environment_test.rb b/test/generators/suspenders/production/environment_test.rb new file mode 100644 index 000000000..11bd41e53 --- /dev/null +++ b/test/generators/suspenders/production/environment_test.rb @@ -0,0 +1,35 @@ +require "test_helper" +require "generators/suspenders/production/environment_generator" + +module Suspenders + module Generators + module Production + class EnvironmentGeneratorTest < Rails::Generators::TestCase + include Suspenders::TestHelpers + + tests Suspenders::Generators::Production::EnvironmentGenerator + destination Rails.root + setup :prepare_destination + teardown :restore_destination + + test "requires master key" do + run_generator + + assert_file app_root("config/environments/production.rb") do |file| + assert_match(/^\s*config\.require_master_key\s*=\s*true/, file) + end + end + + private + + def prepare_destination + backup_file "config/environments/production.rb" + end + + def restore_destination + restore_file "config/environments/production.rb" + end + end + end + end +end