-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathintegration.ex
67 lines (55 loc) · 2.03 KB
/
integration.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
defmodule Core.Schema.Integration do
use Piazza.Ecto.Schema
use Waffle.Ecto.Schema
alias Core.Schema.{Publisher, Repository, ResourceDefinition, Tag}
schema "integrations" do
field :name, :string
field :icon, Core.Storage.Type
field :icon_id, :binary_id
field :source_url, :string
field :description, :string
field :type, :string
field :spec, :map
belongs_to :publisher, Publisher
belongs_to :repository, Repository
has_many :tags, Tag,
where: [resource_type: :integration],
foreign_key: :resource_id,
on_replace: :delete
timestamps()
end
def for_repository(query \\ __MODULE__, repo_id),
do: from(i in query, where: i.repository_id == ^repo_id)
def for_tag(query \\ __MODULE__, tag) do
from(i in query,
join: t in assoc(i, :tags),
where: t.tag == ^tag
)
end
def for_type(query \\ __MODULE__, type) do
from(i in query, where: i.type == ^type)
end
def ordered(query \\ __MODULE__, order \\ [asc: :name]),
do: from(i in query, order_by: ^order)
@valid ~w(name source_url description spec publisher_id type)a
def changeset(model, attrs \\ %{}) do
model
|> cast(attrs, @valid)
|> cast_assoc(:tags, with: &Tag.tag_changeset(&1, &2, :integration))
|> foreign_key_constraint(:publisher_id)
|> foreign_key_constraint(:repository_id)
|> unique_constraint(:name, name: index_name(:integrations, [:repository_id, :name]))
|> generate_uuid(:icon_id)
|> cast_attachments(attrs, [:icon], allow_urls: true)
|> validate_required([:name, :spec])
end
def validate(changeset, %ResourceDefinition{} = resource) do
validate_change(changeset, :spec, fn :spec, val ->
case ResourceDefinition.validate(resource, val) do
:ok -> []
{:error, errors} -> [spec: "Invalid specification: #{Enum.join(errors, ", ")}"]
end
end)
end
def validate(changeset, _), do: add_error(changeset, :spec, "No resource definition present for this repository")
end