-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
module.ex
54 lines (46 loc) · 1.37 KB
/
module.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
defmodule Pow.Ecto.Schema.Module do
@moduledoc """
Generates schema module content.
## Configuration options
* `:binary_id` - if the schema module should use binary id, default nil.
* `:user_id_field` - the user id field to use in the schema module,
defaults nil.
"""
alias Pow.Config
@template """
defmodule <%= inspect schema.module %> do
use Ecto.Schema
use Pow.Ecto.Schema<%= if schema.user_id_field do %>, user_id_field: <%= inspect(schema.user_id_field) %><% end %>
<%= if schema.binary_id do %>
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id<% end %>
schema <%= inspect schema.table %> do
pow_user_fields()
timestamps()
end
end
"""
@doc """
Generates schema module file content.
"""
@spec gen(map()) :: binary()
def gen(schema) do
EEx.eval_string(unquote(@template), schema: schema)
end
@doc """
Generates a schema module map.
"""
@spec new(atom(), binary(), binary(), Config.t()) :: map()
def new(base, schema_name, schema_plural, config \\ []) do
module = Module.concat([base, schema_name])
binary_id = config[:binary_id]
user_id_field = config[:user_id_field]
%{
schema_name: schema_name,
module: module,
table: schema_plural,
binary_id: binary_id,
user_id_field: user_id_field
}
end
end