forked from code-corps/code-corps-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.ex
More file actions
77 lines (64 loc) · 1.79 KB
/
task.ex
File metadata and controls
77 lines (64 loc) · 1.79 KB
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
defmodule CodeCorps.Task do
use CodeCorps.Web, :model
import CodeCorps.ModelHelpers
alias CodeCorps.MarkdownRenderer
schema "tasks" do
field :body, :string
field :markdown, :string
field :number, :integer
field :task_type, :string
field :state, :string
field :status, :string, default: "open"
field :title, :string
belongs_to :project, CodeCorps.Project
belongs_to :user, CodeCorps.User
has_many :comments, CodeCorps.Comment
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:title, :markdown, :task_type])
|> validate_required([:title, :markdown, :task_type])
|> validate_inclusion(:task_type, task_types)
|> MarkdownRenderer.render_markdown_to_html(:markdown, :body)
end
def create_changeset(struct, params) do
struct
|> changeset(params)
|> cast(params, [:project_id, :user_id])
|> validate_required([:project_id, :user_id])
|> assoc_constraint(:project)
|> assoc_constraint(:user)
|> put_change(:state, "published")
|> put_change(:status, "open")
end
def update_changeset(struct, params) do
struct
|> changeset(params)
|> cast(params, [:status])
|> validate_inclusion(:status, statuses)
|> put_change(:state, "edited")
end
defp task_types do
~w{ idea issue task }
end
defp statuses do
~w{ open closed }
end
def index_filters(query, params) do
query
|> project_filter(params)
|> newest_first_filter
end
def task_type_filters(query, params) do
query |> task_type_filter(params)
end
def task_status_filters(query, params) do
query |> task_status_filter(params)
end
def show_project_task_filters(query, params) do
query
|> number_as_id_filter(params)
|> project_filter(params)
end
end