-
-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
suspenders:production:environment
generator (#1151)
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
1 parent
f6c6f45
commit 667b7ae
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
lib/generators/suspenders/production/environment_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Rails.application.configure do | ||
end |
47 changes: 47 additions & 0 deletions
47
test/generators/suspenders/production/environment_generator_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |