diff --git a/README.md b/README.md index 285586c..4ef930a 100644 --- a/README.md +++ b/README.md @@ -33,9 +33,16 @@ This module provides some helper functions to make it easy to insert and retriev ``` ## Repo + + You can set the repo you want Alog to use in a config file: + + ``` elixir + config :alog, Alog, + repo: MyApp.Repo + ``` - Alog expects your `Repo` to belong to the same base module as the schema. - So if your schema is `MyApp.User`, or `MyApp.Accounts.User`, your Repo should be `MyApp.Repo`. + If you do not explicitly set a Repo, Alog will try to find it using your application name. + So if your app is `MyApp` and your schema is `MyApp.User`, or `MyApp.Accounts.User`, your Repo should be `MyApp.Repo`. ## Uniqueness diff --git a/lib/alog.ex b/lib/alog.ex index 1a75f28..c967446 100644 --- a/lib/alog.ex +++ b/lib/alog.ex @@ -28,8 +28,13 @@ defmodule Alog do end end - Alog expects your `Repo` to belong to the same base module as the schema. - So if your schema is `MyApp.User`, or `MyApp.Accounts.User`, your Repo should be `MyApp.Repo`. + You can set the repo you want Alog to use in a config file: + + config :alog, Alog, + repo: MyApp.Repo + + If you do not explicitly set a Repo, Alog will try to find it using your application name. + So if your app is `MyApp` and your schema is `MyApp.User`, or `MyApp.Accounts.User`, your Repo should be `MyApp.Repo`. Any schema that uses Alog must include the fields `:deleted` of type `:boolean` and default false, and `:entry_id` of type `:string`. @@ -61,7 +66,7 @@ defmodule Alog do import Ecto.Query import Ecto.Query.API, only: [field: 2] - @repo __MODULE__ |> Module.split() |> List.first() |> Module.concat("Repo") + @repo apply(unquote(__MODULE__), :get_repo, [__MODULE__]) if not Map.has_key?(%__MODULE__{}, :deleted) || not is_boolean(%__MODULE__{}.deleted) do raise """ @@ -415,4 +420,16 @@ defmodule Alog do defoverridable Alog end end + + def get_repo(module) do + with config when not is_nil(config) <- Application.get_env(:alog, Alog), + repo when not is_nil(repo) <- Keyword.get(config, :repo) do + else + _ -> + module + |> Module.split() + |> List.first() + |> Module.concat("Repo") + end + end end