~singpolyma/dhall-rails

7b837d7c4fe8b56aa4c02f5c4a248aacd59230d1 — Stephen Paul Weber 8 months ago f95eae9 master
Allow function to select by environment

Probably preferrable to a record with one item per environemnt and lots
of duplication, is a function that will return the config for the
desired environment.
M lib/dhall/rails.rb => lib/dhall/rails.rb +2 -1
@@ 2,12 2,13 @@

require "dhall"

require "dhall/rails/application"
require "dhall/rails/cache"
require "dhall/rails/coder"
require "dhall/rails/configuration"
require "dhall/rails/encrypted_configuration"
require "dhall/rails/environment_configuration"
require "dhall/rails/i18n"
require "dhall/rails/application"

module Dhall
	module Rails

M lib/dhall/rails/application.rb => lib/dhall/rails/application.rb +3 -2
@@ 47,8 47,9 @@ module Dhall
					raise "Could not load configuration. No such file - #{path}"
				end

				decoded = Dhall::Rails.load_decoded(path, transform_keys: :to_sym)
				ActiveSupport::OrderedOptions.new.update(decoded.fetch(env.to_sym, {}))
				EnvironmentConfiguration.new(
					Dhall::Rails.load_decoded(path, transform_keys: :to_sym)
				)[env.to_sym]
			end
		end
	end

M lib/dhall/rails/configuration.rb => lib/dhall/rails/configuration.rb +6 -5
@@ 23,8 23,10 @@ module Dhall
			end

			def load_database_yaml
				if (path = paths["config/database"].existent.first)
					Dhall::Rails.load_decoded("#{path} ? {}")
				if (path = pathname("config/database"))&.exist?
					EnvironmentConfiguration.new(
						Dhall::Rails.load_decoded(path, transform_keys: :to_sym)
					)
				else
					{}
				end


@@ 36,9 38,8 @@ module Dhall
			end

			def database_configuration
				path = pathname("config/database")

				return Dhall::Rails.load_decoded("#{path} ? {}") if path&.exist?
				config = load_database_yaml
				return config if config.is_a?(EnvironmentConfiguration)

				# Value from ENV['DATABASE_URL'] is set to default database connection
				# by Active Record.

A lib/dhall/rails/environment_configuration.rb => lib/dhall/rails/environment_configuration.rb +20 -0
@@ 0,0 1,20 @@
# frozen_string_literal: true

class EnvironmentConfiguration < Hash
	# In some cases, Rails expects a Hash, but we want to allow a function
	# Rails only needs a Hash to do things like merge env-level config
	# with root-level config, which we don't need, so just make the
	# root-level an empty hash and override #[] to get the env level

	def initialize(from_dhall)
		@from_dhall = from_dhall
	end

	def [](env)
		ActiveSupport::OrderedOptions.new.update(@from_dhall[env.to_sym])
	end

	def inspect
		"#<#{self.class} #{@from_dhall.inspect}>"
	end
end

A test/app/config/cable2.dhall => test/app/config/cable2.dhall +11 -0
@@ 0,0 1,11 @@
let async = { adapter = "async", url = None Text, channel_prefix = None Text }
in
\(env: <development | test | production>) -> merge {
	development = async,
	test = async,
	production = {
		adapter = "redis",
		url = Some (env:REDIS_URL as Text ? "redis://localhost:6379/1"),
		channel_prefix = Some "small-rails_production"
	}
} env

M test/app/config/database.dhall => test/app/config/database.dhall +3 -4
@@ 1,7 1,6 @@
let default = {
\(env: <test>) -> {
	adapter = "sqlite3",
	pool = env:RAILS_MAX_THREADS ? 5,
	timeout = 5000
} in {
	test = default // { database = "db/test.sqlite3" }
	timeout = 5000,
	database = "db/test.sqlite3"
}

M test/test_application.rb => test/test_application.rb +8 -1
@@ 19,13 19,20 @@ class TestApplication < Minitest::Test
		}.new
	end

	def test_config_for
	def test_config_for_record
		assert_equal(
			{ adapter: "async" },
			::Rails.application.config_for("cable")
		)
	end

	def test_config_for_function
		assert_equal(
			"async",
			::Rails.application.config_for("cable2")[:adapter]
		)
	end

	def test_config_for_yml
		assert_equal(
			{ "adapter" => "async" },

M test/test_configuration.rb => test/test_configuration.rb +11 -18
@@ 25,29 25,22 @@ class TestConfiguration < Minitest::Test

	def test_load_database_yaml
		assert_equal(
			{
				"test" => {
					"adapter"  => "sqlite3",
					"database" => "db/test.sqlite3",
					"pool"     => 5,
					"timeout"  => 5000
				}
			},
			CONFIG.load_database_yaml
			"db/test.sqlite3",
			CONFIG.load_database_yaml["test"]["database"]
		)
	end

	def test_database_configuration
		assert_equal(
			{
				"test" => {
					"adapter"  => "sqlite3",
					"database" => "db/test.sqlite3",
					"pool"     => 5,
					"timeout"  => 5000
				}
			},
			CONFIG.database_configuration
			"db/test.sqlite3",
			CONFIG.database_configuration["test"]["database"]
		)
	end

	def test_database_configuration_yaml
		assert_equal(
			"db/test.sqlite3",
			YAML_CONFIG.database_configuration["test"]["database"]
		)
	end