Skip to content

Commit

Permalink
Introduce suspenders:production:environment generator (#1151)
Browse files Browse the repository at this point in the history
Creates generator to configuration the production environment. For now,
this means [requiring a master key][master key].

Drops configuration for [asset_host][], since that should be an
infrastructure decision.

In an effort to distinguish between the development environment
configuration in #1149, we use the `production` namespace.

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

Co-authored-by: Steve Polito <stevepolito@hey.com>
  • Loading branch information
crackofdusk and stevepolitodesign authored Apr 5, 2024
1 parent f6c6f45 commit 667b7ae
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
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

0 comments on commit 667b7ae

Please sign in to comment.