Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce suspenders:production:environment generator #1151

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions lib/generators/suspenders/production/environment_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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
if production_config.match?(/^\s*#\s*config\.require_master_key\s*=\s*true/)
uncomment_lines "config/environments/production.rb", /config\.require_master_key\s*=\s*true/
else
environment %(config.require_master_key = true), env: "production"
end
end

private

def production_config
File.read(Rails.root.join("config/environments/production.rb"))
end
end
end
end
end
2 changes: 2 additions & 0 deletions test/fixtures/files/environments/production.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,47 @@
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

test "requires master key (when config is not commented out)" do
content = file_fixture("environments/production.rb").read
remove_file_if_exists "config/environments/production.rb"
touch "config/environments/production.rb", content: content

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