Skip to content

Implement app_config_for helper #15

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

Merged
merged 1 commit into from
Jun 2, 2025
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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ TODO.

### `Rails.app_env`

`Rails.app_env` is like `Rails.env` but it is set by the `APP_ENV` environment variable (`ENV["APP_ENV"]`).
`Rails.app_env` is like [`Rails.env`](https://api.rubyonrails.org/classes/Rails.html#method-c-env) but it is set by the
`APP_ENV` environment variable (`ENV["APP_ENV"]`).

It is optimization for `staging` and `review` ([the two extra Heroku pipeline stages](https://devcenter.heroku.com/articles/pipelines)),
so it doesn't need to rely on the slower delegation through `method_missing` that `ActiveSupport::EnvironmentInquirer`
Expand All @@ -59,6 +60,17 @@ Rails.env.production? # => true

In case `ENV["APP_ENV"]` is blank, `Rails.app_env` falls back to `Rails.env`.

### `Rails.application.app_config_for`

`Rails.application.app_config_for` wraps[`Rails.application.config_for`](https://api.rubyonrails.org/classes/Rails/Application.html#method-i-config_for)
with `{env: Rails.app_env}` as the second argument.

```ruby
# These two lines are equivalent.
Rails.application.app_config_for(:foo)
Rails.application.config_for(:foo, env: Rails.app_env)
```

### Credentials

`Rails APP_ENV` overrides the default Rails credentials `content_path` and `key_path` according to `Rails.app_env`.
Expand Down
3 changes: 2 additions & 1 deletion lib/rails/app_env.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require_relative "app_env/version"
require_relative "app_env/environment_inquirer"
require_relative "app_env/helpers"
require_relative "app_env/rails_helpers"
require_relative "app_env/application_helpers"
require_relative "app_env/credentials"
require_relative "app_env/railtie"

Expand Down
9 changes: 9 additions & 0 deletions lib/rails/app_env/application_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Rails
module AppEnv
module ApplicationHelpers
def app_config_for(name)
config_for(name, env: Rails.app_env)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Rails
module AppEnv
module Helpers
module RailsHelpers
def app_env
@_app_env ||= EnvironmentInquirer.new(ENV["APP_ENV"] || Rails.env)
end
Expand Down
15 changes: 10 additions & 5 deletions lib/rails/app_env/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
module Rails
module AppEnv
class Railtie < Rails::Railtie
initializer :load_helpers, before: :initialize_logger do
Rails.extend(Helpers)
config.before_configuration do
Rails.extend(RailsHelpers)
Rails.application.extend(ApplicationHelpers)
end

initializer :set_credentials, before: :initialize_logger do
config.before_configuration do
# TODO: Allow opt-out.
Rails::AppEnv::Credentials.initialize!
end

config.after_initialize do
Rails::Info.property "Application environment", Rails.app_env
Rails::Info.property "Application environment" do
Rails.app_env
end
end

console do |app|
# TODO: Allow opt-out.
require_relative "console"

app.config.console = Rails::AppEnv::Console.new(app)

puts "Loading #{Rails.app_env} application environment (rails-app_env #{Rails::AppEnv::VERSION})" # standard:disable Rails/Output
end
end
Expand Down
25 changes: 25 additions & 0 deletions test/features/app_config_for_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "minitest/mock"
require_relative "../test_helper"
require_relative "env_helpers"

module Rails::AppEnv::FeaturesTest
class AppConfigForTest < ActiveSupport::TestCase
include EnvHelpers

test "Rails.application.app_config_for delegates to Rails.application.config_for" do
app_env = "foo"
expected = Object.new

mock = Minitest::Mock.new
mock.expect(:call, expected, [:bar], env: app_env)

with_app_env(app_env) do
Rails.application.stub(:config_for, mock) do
assert_same expected, Rails.application.app_config_for(:bar)
end
end

mock.verify
end
end
end