diff --git a/README.md b/README.md index ea7a842..f33c649 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ Extra options: App config can be used to set a global default to apply to all resources unless overridden in their individual config, or the LiveAdmin instance. -When set at the application level, resource-level keys are read at **compile time**, and must be configured in `config/config.exs` (or another compile-time config file). Setting any of them in `config/runtime.exs` (or otherwise at runtime) will raise on boot. +When set at the application level, resource-level keys are read at **compile time** via `Application.compile_env/2` and must be configured in `config/config.exs` (or another compile-time config file). Setting any of them in `config/runtime.exs` will cause Elixir to raise on boot due to the compile/runtime mismatch. Compile-time keys: `components`, `query_with`, `render_with`, `delete_with`, `create_with`, `update_with`, `validate_with`, `label_with`, `title_with`, `hidden_fields`, `immutable_fields`, `actions`, `tasks` diff --git a/README.md.eex b/README.md.eex index ea7a842..f33c649 100644 --- a/README.md.eex +++ b/README.md.eex @@ -117,7 +117,7 @@ Extra options: App config can be used to set a global default to apply to all resources unless overridden in their individual config, or the LiveAdmin instance. -When set at the application level, resource-level keys are read at **compile time**, and must be configured in `config/config.exs` (or another compile-time config file). Setting any of them in `config/runtime.exs` (or otherwise at runtime) will raise on boot. +When set at the application level, resource-level keys are read at **compile time** via `Application.compile_env/2` and must be configured in `config/config.exs` (or another compile-time config file). Setting any of them in `config/runtime.exs` will cause Elixir to raise on boot due to the compile/runtime mismatch. Compile-time keys: `components`, `query_with`, `render_with`, `delete_with`, `create_with`, `update_with`, `validate_with`, `label_with`, `title_with`, `hidden_fields`, `immutable_fields`, `actions`, `tasks` diff --git a/lib/application.ex b/lib/application.ex index a7faef4..b1c55dc 100644 --- a/lib/application.ex +++ b/lib/application.ex @@ -1,38 +1,6 @@ defmodule LiveAdmin.Application do use Application - @compile_time_app_keys [ - :components, - :query_with, - :render_with, - :delete_with, - :create_with, - :update_with, - :validate_with, - :label_with, - :title_with, - :hidden_fields, - :immutable_fields, - :actions, - :tasks - ] - - @compile_time_app_config %{ - components: Application.compile_env(:live_admin, :components), - query_with: Application.compile_env(:live_admin, :query_with), - render_with: Application.compile_env(:live_admin, :render_with), - delete_with: Application.compile_env(:live_admin, :delete_with), - create_with: Application.compile_env(:live_admin, :create_with), - update_with: Application.compile_env(:live_admin, :update_with), - validate_with: Application.compile_env(:live_admin, :validate_with), - label_with: Application.compile_env(:live_admin, :label_with), - title_with: Application.compile_env(:live_admin, :title_with), - hidden_fields: Application.compile_env(:live_admin, :hidden_fields), - immutable_fields: Application.compile_env(:live_admin, :immutable_fields), - actions: Application.compile_env(:live_admin, :actions), - tasks: Application.compile_env(:live_admin, :tasks) - } - def start(_type, _args) do opts = [strategy: :one_for_one, name: LiveAdmin.Supervisor] @@ -46,32 +14,9 @@ defmodule LiveAdmin.Application do NimbleOptions.validate!(Application.get_all_env(:live_admin), global_options_schema) - validate_compile_time_config!() - Supervisor.start_link(children(), opts) end - @doc false - def validate_compile_time_config! do - @compile_time_app_keys - |> Enum.filter(fn key -> - Map.fetch!(@compile_time_app_config, key) != Application.get_env(:live_admin, key) - end) - |> case do - [] -> - :ok - - mismatches -> - raise """ - The following :live_admin config keys have been set at runtime, but they must be set at compile time: - - #{Enum.map_join(mismatches, "\n", &" * #{inspect(&1)}")} - - Move these into config/config.exs (or another compile-time config file). - """ - end - end - defp children do [ {LiveAdmin.Session.Agent, %{}}, diff --git a/lib/live_admin.ex b/lib/live_admin.ex index 9215220..2d67212 100644 --- a/lib/live_admin.ex +++ b/lib/live_admin.ex @@ -121,7 +121,7 @@ defmodule LiveAdmin do Used internally to validate configuration in apps using LiveAdmin. - When set at the application level, every option in this schema *except* `ecto_repo` is read at compile time. Configure them in `config/config.exs` (or another compile-time config file); setting them at runtime will raise on boot. + When set at the application level, every option in this schema *except* `ecto_repo` is read at compile time via `Application.compile_env/2`. Configure them in `config/config.exs` (or another compile-time config file); setting them in `config/runtime.exs` will cause Elixir to raise on boot due to the compile/runtime mismatch. Supported options: #{@options_schema |> NimbleOptions.new!() |> NimbleOptions.docs()} diff --git a/test/live_admin/application_test.exs b/test/live_admin/application_test.exs deleted file mode 100644 index b8f80a1..0000000 --- a/test/live_admin/application_test.exs +++ /dev/null @@ -1,24 +0,0 @@ -defmodule LiveAdmin.ApplicationTest do - use ExUnit.Case, async: false - - describe "validate_compile_time_config! when a resource option is set at runtime" do - setup do - Application.put_env(:live_admin, :create_with, {RuntimeMod, :runtime_fun}) - on_exit(fn -> Application.delete_env(:live_admin, :create_with) end) - - :ok - end - - test "raises pointing the user to compile-time config" do - assert_raise RuntimeError, ~r/create_with/, fn -> - LiveAdmin.Application.validate_compile_time_config!() - end - end - end - - describe "validate_compile_time_config! when no resource options diverge between compile and runtime env" do - test "returns :ok" do - assert :ok == LiveAdmin.Application.validate_compile_time_config!() - end - end -end