Skip to content

Commit

Permalink
Merge branch 'master' into inserted_at-value
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonLab committed Feb 5, 2019
2 parents c3f9c75 + 4355536 commit 8935531
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 20 additions & 3 deletions lib/alog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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 """
Expand Down Expand Up @@ -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

0 comments on commit 8935531

Please sign in to comment.