-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
model.ex
64 lines (50 loc) · 1.56 KB
/
model.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
defmodule Ecto.Model do
@moduledoc """
Warning: this module is currently deprecated. Instead
`use Ecto.Schema` and the functions in the `Ecto` module.
`Ecto.Model` is built on top of `Ecto.Schema`. See
`Ecto.Schema` for documentation on the `schema/2` macro,
as well which fields, associations, types are available.
## Using
When used, `Ecto.Model` imports itself, as well as the functions
in `Ecto.Changeset` and `Ecto.Query`.
All the modules existing in `Ecto.Model.*` are brought in too:
* `use Ecto.Model.Callbacks` - provides lifecycle callbacks
* `use Ecto.Model.OptimisticLock` - makes the `optimistic_lock/1` macro
available
However, you can avoid using `Ecto.Model` altogether in favor
of cherry-picking any of the functionality above.
"""
@doc false
defmacro __using__(_opts) do
quote do
use Ecto.Schema
import Ecto.Model
import Ecto.Changeset
import Ecto.Query, only: [from: 2]
use Ecto.Model.OptimisticLock
use Ecto.Model.Callbacks
end
end
@type t :: %{__struct__: atom}
@doc false
def primary_key(struct) do
Ecto.primary_key(struct)
end
@doc false
def primary_key!(struct) do
Ecto.primary_key!(struct)
end
@doc false
def build(struct, assoc, attributes \\ %{}) do
Ecto.build_assoc(struct, assoc, attributes)
end
@doc false
def assoc(model_or_models, assoc) do
Ecto.assoc(model_or_models, assoc)
end
@doc false
def put_source(model, new_source, new_prefix \\ nil) do
put_in model.__meta__.source, {new_prefix, new_source}
end
end