-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathpaper_trail.ex
133 lines (123 loc) · 4.62 KB
/
paper_trail.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
defmodule PaperTrail do
alias PaperTrail.Version
alias PaperTrail.Serializer
defdelegate get_version(record), to: PaperTrail.VersionQueries
defdelegate get_version(model_or_record, id_or_options), to: PaperTrail.VersionQueries
defdelegate get_version(model, id, options), to: PaperTrail.VersionQueries
defdelegate get_versions(record), to: PaperTrail.VersionQueries
defdelegate get_versions(model_or_record, id_or_options), to: PaperTrail.VersionQueries
defdelegate get_versions(model, id, options), to: PaperTrail.VersionQueries
defdelegate get_current_model(version), to: PaperTrail.VersionQueries
defdelegate make_version_struct(version, model, options), to: Serializer
defdelegate serialize(data), to: Serializer
defdelegate get_sequence_id(table_name), to: Serializer
defdelegate add_prefix(schema, prefix), to: Serializer
defdelegate get_item_type(data), to: Serializer
defdelegate get_model_id(model), to: Serializer
@doc """
Inserts a record to the database with a related version insertion in one transaction
"""
@spec insert(changeset :: Ecto.Changeset.t(model), options :: Keyword.t()) ::
{:ok, %{model: model, version: Version.t()}} | {:error, Ecto.Changeset.t(model) | term}
when model: struct
def insert(
changeset,
options \\ [
origin: nil,
meta: nil,
originator: nil,
prefix: nil,
model_key: :model,
version_key: :version,
ecto_options: []
]
) do
PaperTrail.Multi.new()
|> PaperTrail.Multi.insert(changeset, options)
|> PaperTrail.Multi.commit()
end
@doc """
Same as insert/2 but returns only the model struct or raises if the changeset is invalid.
"""
@spec insert!(changeset :: Ecto.Changeset.t(model), options :: Keyword.t()) :: model
when model: struct
def insert!(
changeset,
options \\ [
origin: nil,
meta: nil,
originator: nil,
prefix: nil,
model_key: :model,
version_key: :version,
ecto_options: []
]
) do
changeset
|> insert(options)
|> model_or_error(:insert)
end
@doc """
Updates a record from the database with a related version insertion in one transaction
"""
@spec update(changeset :: Ecto.Changeset.t(model), options :: Keyword.t()) ::
{:ok, %{model: model, version: Version.t()}} | {:error, Ecto.Changeset.t(model) | term}
when model: struct
def update(changeset, options \\ [origin: nil, meta: nil, originator: nil, prefix: nil]) do
PaperTrail.Multi.new()
|> PaperTrail.Multi.update(changeset, options)
|> PaperTrail.Multi.commit()
end
@doc """
Same as update/2 but returns only the model struct or raises if the changeset is invalid.
"""
@spec update!(changeset :: Ecto.Changeset.t(model), options :: Keyword.t()) :: model
when model: struct
def update!(changeset, options \\ [origin: nil, meta: nil, originator: nil, prefix: nil]) do
changeset
|> update(options)
|> model_or_error(:update)
end
@doc """
Deletes a record from the database with a related version insertion in one transaction
"""
@spec delete(model_or_changeset :: model | Ecto.Changeset.t(model), options :: Keyword.t()) ::
{:ok, %{model: model, version: Version.t()}} | {:error, Ecto.Changeset.t(model) | term}
when model: struct
def delete(
model_or_changeset,
options \\ [origin: nil, meta: nil, originator: nil, prefix: nil]
) do
PaperTrail.Multi.new()
|> PaperTrail.Multi.delete(model_or_changeset, options)
|> PaperTrail.Multi.commit()
end
@doc """
Same as delete/2 but returns only the model struct or raises if the changeset is invalid.
"""
@spec delete!(model_or_changeset :: model | Ecto.Changeset.t(model), options :: Keyword.t()) ::
model
when model: struct
def delete!(
model_or_changeset,
options \\ [origin: nil, meta: nil, originator: nil, prefix: nil]
) do
model_or_changeset
|> delete(options)
|> model_or_error(:delete)
end
@spec model_or_error(result :: {:ok, %{model: model}}, action :: :insert | :update | :delete) ::
model
when model: struct()
defp model_or_error({:ok, %{model: model}}, _action) do
model
end
@spec model_or_error(result :: {:error, reason :: term}, action :: :insert | :update | :delete) ::
no_return
defp model_or_error({:error, %Ecto.Changeset{} = changeset}, action) do
raise Ecto.InvalidChangesetError, action: action, changeset: changeset
end
defp model_or_error({:error, reason}, _action) do
raise reason
end
end