-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
replace_timestamps.ex
43 lines (35 loc) · 1.1 KB
/
replace_timestamps.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
defmodule Ash.Resource.Transformers.ReplaceTimestamps do
@moduledoc "Replaces a single `timestamps()` attribute with `inserted_at` and `updated_at` timestamps."
use Ash.Dsl.Transformer
alias Ash.Dsl.Transformer
def transform(_resource, dsl_state) do
attributes =
dsl_state
|> Transformer.get_entities([:attributes])
|> Enum.reduce([], fn
%{name: :__timestamps__}, attrs ->
attrs ++ timestamp_attributes()
attr, attrs ->
attrs ++ [attr]
end)
new_dsl_state = put_in(dsl_state, [[:attributes], :entities], attributes)
{:ok, new_dsl_state}
end
defp timestamp_attributes do
%{
inserted_at: Ash.Resource.Attribute.create_timestamp_schema(),
updated_at: Ash.Resource.Attribute.update_timestamp_schema()
}
|> Enum.map(&build_attribute/1)
|> Enum.map(fn {:ok, attr} -> attr end)
end
defp build_attribute({name, schema}) do
params = %{
target: Ash.Resource.Attribute,
schema: schema,
auto_set_fields: [name: name],
transform: nil
}
Ash.Dsl.Entity.build(params, [], [])
end
end