Skip to content

Re-organize namespaces; Do not autoload; Fix typo #18

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 3 commits into from
Jun 3, 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
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rails-app_env (0.2.0)
rails-app_env (0.3.0)
activesupport (>= 8.0.0)
railties (>= 8.0.0)

Expand Down
17 changes: 11 additions & 6 deletions lib/rails/app_env.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
require_relative "app_env/version"

require_relative "app_env/error"

require_relative "app_env/console"
require_relative "app_env/credentials"
require_relative "app_env/environment_inquirer"

require_relative "app_env/extensions/rails/app_env_aware"
require_relative "app_env/extensions/credentials_command/original_aware"
require_relative "app_env/extensions/application/app_configurable"

require_relative "app_env/railtie"

module Rails
module AppEnv
autoload :ApplicationHelpers, "rails/app_env/application_helpers"
autoload :Console, "rails/app_env/console"
autoload :Credentials, "rails/app_env/credentials"
autoload :EnvironmentInquirer, "rails/app_env/environment_inquirer"
autoload :Error, "rails/app_env/error"
autoload :RailsHelpers, "rails/app_env/rails_helpers"
end
end
9 changes: 0 additions & 9 deletions lib/rails/app_env/application_helpers.rb

This file was deleted.

3 changes: 2 additions & 1 deletion lib/rails/app_env/credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def key_path
end

def monkey_patch_rails_credentials_command!
require_relative "../rails_ext/credentials_command"
return unless defined?(Rails::Command::CredentialsCommand)
Rails::Command::CredentialsCommand.extend(Extensions::OriginalAware)
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions lib/rails/app_env/extensions/application/app_configurable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Rails::AppEnv::Extensions
module AppConfigurable
extend self

def app_config_for(name)
Rails.application.config_for(name, env: Rails.app_env)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Rails::AppEnv::Extensions
module OriginalAware
private

def config
Rails::AppEnv::Credentials.original
end
end
end
13 changes: 13 additions & 0 deletions lib/rails/app_env/extensions/rails/app_env_aware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Rails
module AppEnv
module Extensions
module AppEnvAware
extend self

def app_env
@_app_env ||= EnvironmentInquirer.new(ENV["APP_ENV"] || Rails.env)
end
end
end
end
end
9 changes: 0 additions & 9 deletions lib/rails/app_env/rails_helpers.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/rails/app_env/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Rails
module AppEnv
class Railtie < Rails::Railtie
config.before_configuration do
Rails.extend(RailsHelpers)
Rails.application.extend(ApplicationHelpers)
Rails.extend(Extensions::AppEnvAware)
Rails.application.extend(Extensions::AppConfigurable)
end

config.before_configuration do
Expand Down
2 changes: 1 addition & 1 deletion lib/rails/app_env/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Rails
module AppEnv
VERSION = "0.2.0"
VERSION = "0.3.0"
end
end
11 changes: 0 additions & 11 deletions lib/rails/rails_ext/credentials_command.rb

This file was deleted.

64 changes: 33 additions & 31 deletions test/units/app_env/console_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,52 @@
require_relative "../../test_helper"
require "rails/commands/console/irb_console"

class Rails::AppEnv::ConsoleTest < ActiveSupport::TestCase
test "Rails::AppEnv::Console is a kind of Rails::Console::IRBConsole" do
assert_kind_of Rails::Console::IRBConsole, Rails::AppEnv::Console.new(nil)
end
module Rails::AppEnv
class ConsoleTest < ActiveSupport::TestCase
test "Rails::AppEnv::Console is a kind of Rails::Console::IRBConsole" do
assert_kind_of Rails::Console::IRBConsole, Rails::AppEnv::Console.new(nil)
end

test "Console#colorized_env prints Rails.env only when Rails.app_env is same as Rails.env" do
Rails.stub :app_env, "foo" do
Rails.stub :env, "foo" do
console = Rails::AppEnv::Console.new(nil)
assert_equal_without_color "foo", console.colorized_env
test "#colorized_env prints Rails.env only when Rails.app_env is same as Rails.env" do
Rails.stub :app_env, "foo" do
Rails.stub :env, "foo" do
console = Console.new(nil)
assert_equal_without_color "foo", console.colorized_env
end
end
end
end

test "Console#colorized_env prints {Rails.env:Rails.app_env} when Rails.app_env is diff from Rails.env" do
Rails.stub :app_env, "foo" do
Rails.stub :env, "bar" do
console = Rails::AppEnv::Console.new(nil)
assert_equal_without_color "bar:foo", console.colorized_env
test "#colorized_env prints {Rails.env:Rails.app_env} when Rails.app_env is diff from Rails.env" do
Rails.stub :app_env, "foo" do
Rails.stub :env, "bar" do
console = Console.new(nil)
assert_equal_without_color "bar:foo", console.colorized_env
end
end
end
end

test "Console#colorized_env shorten Rails.app_env to dev when Rails.app_env is production" do
Rails.stub :app_env, "production" do
Rails.stub :env, "bar" do
console = Rails::AppEnv::Console.new(nil)
assert_equal_without_color "bar:prod", console.colorized_env
test "#colorized_env shorten Rails.app_env to prod when Rails.app_env is production" do
Rails.stub :app_env, "production" do
Rails.stub :env, "bar" do
console = Console.new(nil)
assert_equal_without_color "bar:prod", console.colorized_env
end
end
end
end

test "Console#colorized_env shorten Rails.app_env to dev when Rails.app_env is development" do
Rails.stub :app_env, "development" do
Rails.stub :env, "bar" do
console = Rails::AppEnv::Console.new(nil)
assert_equal_without_color "bar:dev", console.colorized_env
test "#colorized_env shorten Rails.app_env to dev when Rails.app_env is development" do
Rails.stub :app_env, "development" do
Rails.stub :env, "bar" do
console = Console.new(nil)
assert_equal_without_color "bar:dev", console.colorized_env
end
end
end
end

private
private

def assert_equal_without_color(expected, actual)
assert_equal expected, actual.gsub(/\e\[(\d+)(;\d+)*m/, "")
def assert_equal_without_color(expected, actual)
assert_equal expected, actual.gsub(/\e\[(\d+)(;\d+)*m/, "")
end
end
end
116 changes: 59 additions & 57 deletions test/units/app_env/credentials_test.rb
Original file line number Diff line number Diff line change
@@ -1,95 +1,97 @@
require "minitest/mock"
require_relative "../../test_helper"

class Rails::AppEnv::CredentialsTest < ActiveSupport::TestCase
test "Rails::AppEnv::Credentials::AlreadyInitializedError is a kind of Rails::AppEnv::Error" do
assert_kind_of Rails::AppEnv::Error, Rails::AppEnv::Credentials::AlreadyInitializedError.new
end
module Rails::AppEnv
class CredentialsTest < ActiveSupport::TestCase
test "Rails::AppEnv::Credentials::AlreadyInitializedError is a kind of Rails::AppEnv::Error" do
assert_kind_of Rails::AppEnv::Error, Rails::AppEnv::Credentials::AlreadyInitializedError.new
end

test "#initialize! can only be invoked once" do
reset_credentials
test ".initialize! can only be invoked once" do
reset_credentials

Rails::AppEnv::Credentials.initialize!
Credentials.initialize!

assert_raises Rails::AppEnv::Credentials::AlreadyInitializedError do
Rails::AppEnv::Credentials.initialize!
assert_raises Rails::AppEnv::Credentials::AlreadyInitializedError do
Credentials.initialize!
end
end
end

test "#initialize! backup original Rails.application.config.credentials" do
reset_credentials
test ".initialize! backup original Rails.application.config.credentials" do
reset_credentials

expected = Object.new
expected = Object.new

Rails.application.config.stub :credentials, expected do
Rails::AppEnv::Credentials.initialize!
end
Rails.application.config.stub :credentials, expected do
Credentials.initialize!
end

assert_same expected, Rails::AppEnv::Credentials.original
end
assert_same expected, Credentials.original
end

test "#configuration is a kind of ActiveSupport::Credentials" do
assert_kind_of ActiveSupport::InheritableOptions, Rails::AppEnv::Credentials.configuration
end
test ".configuration is a kind of ActiveSupport::Credentials" do
assert_kind_of ActiveSupport::InheritableOptions, Credentials.configuration
end

test "#configuration.content_path is config/credentials/{Rails.app_env}.yml.enc" do
Dir.mktmpdir do |tmp_dir|
Rails.stub :root, Pathname(tmp_dir) do
Rails.stub :app_env, Rails::AppEnv::EnvironmentInquirer.new("foo") do
path = Rails.root.join("config/credentials/foo.yml.enc")
test ".configuration.content_path is config/credentials/{Rails.app_env}.yml.enc" do
Dir.mktmpdir do |tmp_dir|
Rails.stub :root, Pathname(tmp_dir) do
Rails.stub :app_env, EnvironmentInquirer.new("foo") do
path = Rails.root.join("config/credentials/foo.yml.enc")

FileUtils.mkdir_p File.dirname(path)
FileUtils.touch path
FileUtils.mkdir_p File.dirname(path)
FileUtils.touch path

assert_equal path, Rails::AppEnv::Credentials.configuration.content_path
assert_equal path, Credentials.configuration.content_path
end
end
end
end
end

test "#configuration.content_path falls back to config/credentials.yml.enc when config/credentials/{Rails.app_env}.yml.enc not exist" do
Dir.mktmpdir do |tmp_dir|
Rails.stub :root, Pathname(tmp_dir) do
Rails.stub :app_env, Rails::AppEnv::EnvironmentInquirer.new("foo") do
path = Rails.root.join("config/credentials.yml.enc")
test ".configuration.content_path falls back to config/credentials.yml.enc when config/credentials/{Rails.app_env}.yml.enc not exist" do
Dir.mktmpdir do |tmp_dir|
Rails.stub :root, Pathname(tmp_dir) do
Rails.stub :app_env, EnvironmentInquirer.new("foo") do
path = Rails.root.join("config/credentials.yml.enc")

assert_equal path, Rails::AppEnv::Credentials.configuration.content_path
assert_equal path, Credentials.configuration.content_path
end
end
end
end
end

test "#configuration.key_path is config/credentials/{APP_ENV}.key" do
Dir.mktmpdir do |tmp_dir|
Rails.stub :root, Pathname(tmp_dir) do
Rails.stub :app_env, Rails::AppEnv::EnvironmentInquirer.new("foo") do
path = Rails.root.join("config/credentials/foo.key")
test ".configuration.key_path is config/credentials/{APP_ENV}.key" do
Dir.mktmpdir do |tmp_dir|
Rails.stub :root, Pathname(tmp_dir) do
Rails.stub :app_env, EnvironmentInquirer.new("foo") do
path = Rails.root.join("config/credentials/foo.key")

FileUtils.mkdir_p File.dirname(path)
FileUtils.touch path
FileUtils.mkdir_p File.dirname(path)
FileUtils.touch path

assert_equal path, Rails::AppEnv::Credentials.configuration.key_path
assert_equal path, Credentials.configuration.key_path
end
end
end
end
end

test "#configuration.key_path falls back to config/master.key when config/credentials/{APP_ENV}.key not exist" do
Dir.mktmpdir do |tmp_dir|
Rails.stub :root, Pathname(tmp_dir) do
Rails.stub :app_env, Rails::AppEnv::EnvironmentInquirer.new("foo") do
path = Rails.root.join("config/master.key")
test ".configuration.key_path falls back to config/master.key when config/credentials/{APP_ENV}.key not exist" do
Dir.mktmpdir do |tmp_dir|
Rails.stub :root, Pathname(tmp_dir) do
Rails.stub :app_env, EnvironmentInquirer.new("foo") do
path = Rails.root.join("config/master.key")

assert_equal path, Rails::AppEnv::Credentials.configuration.key_path
assert_equal path, Credentials.configuration.key_path
end
end
end
end
end

private
private

def reset_credentials
Rails::AppEnv::Credentials.instance_variable_set :@initialized, nil
Rails::AppEnv::Credentials.instance_variable_set :@original, nil
def reset_credentials
Credentials.instance_variable_set :@initialized, nil
Credentials.instance_variable_set :@original, nil
end
end
end
Loading
Loading