Skip to content

Commit 1476279

Browse files
crackofduskstevepolitodesign
authored andcommitted
Introduce suspenders:production:environment generator
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
1 parent f6c6f45 commit 1476279

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-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:production:environment` generator
2122

2223
20230113.0 (January, 13, 2023)
2324

README.md

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

200+
### Environments
201+
202+
#### Production
203+
204+
Configures the production environment.
205+
206+
- Enables [require_master_key][]
207+
208+
[require_master_key]: https://guides.rubyonrails.org/configuring.html#config-require-master-key
209+
210+
```
211+
bin/rails g suspenders:production:environment
212+
```
213+
200214
## Contributing
201215

202216
See the [CONTRIBUTING] document.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Suspenders
2+
module Generators
3+
module Production
4+
class EnvironmentGenerator < Rails::Generators::Base
5+
desc <<~MARKDOWN
6+
Configures the production environment.
7+
8+
- Enables [require_master_key][]
9+
10+
[require_master_key]: https://guides.rubyonrails.org/configuring.html#config-require-master-key
11+
MARKDOWN
12+
13+
def require_master_key
14+
if production_config.match?(/^\s*#\s*config\.require_master_key\s*=\s*true/)
15+
uncomment_lines "config/environments/production.rb", /config\.require_master_key\s*=\s*true/
16+
else
17+
environment %(config.require_master_key = true), env: "production"
18+
end
19+
end
20+
21+
private
22+
23+
def production_config
24+
File.read(Rails.root.join("config/environments/production.rb"))
25+
end
26+
end
27+
end
28+
end
29+
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,47 @@
1+
require "test_helper"
2+
require "generators/suspenders/production/environment_generator"
3+
4+
module Suspenders
5+
module Generators
6+
module Production
7+
class EnvironmentGeneratorTest < Rails::Generators::TestCase
8+
include Suspenders::TestHelpers
9+
10+
tests Suspenders::Generators::Production::EnvironmentGenerator
11+
destination Rails.root
12+
setup :prepare_destination
13+
teardown :restore_destination
14+
15+
test "requires master key" do
16+
run_generator
17+
18+
assert_file app_root("config/environments/production.rb") do |file|
19+
assert_match(/^\s*config\.require_master_key\s*=\s*true/, file)
20+
end
21+
end
22+
23+
test "requires master key (when config is not commented out)" do
24+
content = file_fixture("environments/production.rb").read
25+
remove_file_if_exists "config/environments/production.rb"
26+
touch "config/environments/production.rb", content: content
27+
28+
run_generator
29+
30+
assert_file app_root("config/environments/production.rb") do |file|
31+
assert_match(/^\s*config\.require_master_key\s*=\s*true/, file)
32+
end
33+
end
34+
35+
private
36+
37+
def prepare_destination
38+
backup_file "config/environments/production.rb"
39+
end
40+
41+
def restore_destination
42+
restore_file "config/environments/production.rb"
43+
end
44+
end
45+
end
46+
end
47+
end

0 commit comments

Comments
 (0)