Skip to content

Use autoload #16

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 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
11 changes: 6 additions & 5 deletions lib/rails/app_env.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
require_relative "app_env/version"
require_relative "app_env/environment_inquirer"
require_relative "app_env/rails_helpers"
require_relative "app_env/application_helpers"
require_relative "app_env/credentials"
require_relative "app_env/railtie"

module Rails
module AppEnv
# Your code goes here...
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
4 changes: 1 addition & 3 deletions lib/rails/app_env/credentials.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
require_relative "error"

module Rails
module AppEnv
module Credentials
class AlreadyInitializedError < Rails::AppEnv::Error; end
class AlreadyInitializedError < Error; end

class << self
attr_reader :original
Expand Down
5 changes: 2 additions & 3 deletions lib/rails/app_env/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Railtie < Rails::Railtie

config.before_configuration do
# TODO: Allow opt-out.
Rails::AppEnv::Credentials.initialize!
Credentials.initialize!
end

config.after_initialize do
Expand All @@ -19,8 +19,7 @@ class Railtie < Rails::Railtie

console do |app|
# TODO: Allow opt-out.
require_relative "console"
app.config.console = Rails::AppEnv::Console.new(app)
app.config.console = Console.new(app)

puts "Loading #{Rails.app_env} application environment (rails-app_env #{Rails::AppEnv::VERSION})" # standard:disable Rails/Output
end
Expand Down
2 changes: 2 additions & 0 deletions lib/rails/rails_ext/credentials_command.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Rails
module Command
class CredentialsCommand
private

def config
Rails::AppEnv::Credentials.original
end
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Application < Rails::Application
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")

config.eager_load = false
config.eager_load = ENV["CI"].present?

# Don't generate system test files.
config.generators.system_tests = nil
Expand Down
4 changes: 2 additions & 2 deletions test/features/credentials_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ class CredentialsTest < ActiveSupport::TestCase
private

def assert_credentials_path(expected_path:, touch_file: nil, **options)
assert_runner_puts_with_file Rails.root.join(expected_path), "Rails.configuration.credentials.content_path", touch_file, **options
assert_runner_puts_with_file dummy_path(expected_path), "Rails.configuration.credentials.content_path", touch_file, **options
end

def assert_key_path(expected_path:, touch_file: nil, **options)
assert_runner_puts_with_file Rails.root.join(expected_path), "Rails.configuration.credentials.key_path", touch_file, **options
assert_runner_puts_with_file dummy_path(expected_path), "Rails.configuration.credentials.key_path", touch_file, **options
end

def assert_runner_puts_with_file(expected, subject, file_path, **options)
Expand Down
4 changes: 2 additions & 2 deletions test/units/app_env/console_test.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "minitest/mock"
require_relative "../../test_helper"
require "rails/app_env/console"
require "rails/commands/console/irb_console"

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

Expand Down
4 changes: 4 additions & 0 deletions test/units/app_env/credentials_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
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

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

Expand Down