Skip to content

Commit 082cb3b

Browse files
authored
Fixes seeds in an ugly, temporary way. (#1276)
1 parent 19afd82 commit 082cb3b

File tree

2 files changed

+57
-20
lines changed

2 files changed

+57
-20
lines changed

priv/repo/seeds.exs

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
alias CodeCorps.{
22
Category, Organization, ProjectUser, Project, ProjectCategory, ProjectSkill,
3-
Repo, Role, Skill, Task, User, UserCategory, UserRole, UserSkill
3+
Repo, Role, Skill, Task, User, UserCategory, UserRole, UserSkill,
44
}
55

66
users = [
@@ -56,15 +56,55 @@ organizations = [
5656
}
5757
]
5858

59-
case Repo.all(Organization) do
60-
[] ->
61-
organizations
62-
|> Enum.each(fn params ->
59+
defmodule SeedChangeset do
60+
@moduledoc false
61+
62+
alias CodeCorps.{
63+
TaskList,
64+
Organization,
65+
SluggedRoute,
66+
Helpers.Slug,
67+
Helpers.RandomIconColor,
68+
Services.MarkdownRendererService
69+
}
70+
alias Ecto.Changeset
71+
72+
def organization(%{} = params) do
73+
changeset =
6374
%Organization{}
64-
|> Organization.create_changeset(params)
65-
|> Repo.insert!()
66-
end)
67-
_ -> IO.puts "Organizations detected, aborting organization seed."
75+
|> Changeset.cast(params, [:name, :description, :owner_id])
76+
|> Changeset.assoc_constraint(:owner)
77+
|> Changeset.put_change(:approved, true)
78+
|> Slug.generate_slug(:name, :slug)
79+
|> RandomIconColor.generate_icon_color(:slugged_route_changeset)
80+
81+
slug = changeset |> Changeset.get_field(:slug)
82+
slugged_route_changeset =
83+
%SluggedRoute{} |> SluggedRoute.create_changeset(%{slug: slug})
84+
85+
changeset
86+
|> Changeset.put_assoc(:slugged_route, slugged_route_changeset)
87+
end
88+
89+
def project(%{} = params) do
90+
%Project{}
91+
|> Changeset.cast(params, [:description, :long_description_markdown, :title, :organization_id])
92+
|> MarkdownRendererService.render_markdown_to_html(:long_description_markdown, :long_description_body)
93+
|> Changeset.put_assoc(:task_lists, TaskList.default_task_lists())
94+
|> Changeset.assoc_constraint(:organization)
95+
|> RandomIconColor.generate_icon_color(:default_color)
96+
|> Slug.generate_slug(:title, :slug)
97+
|> Ecto.Changeset.put_change(:approved, true)
98+
end
99+
end
100+
101+
case Repo.aggregate(Organization, :count, :id) do
102+
0 ->
103+
organizations
104+
|> Enum.map(&SeedChangeset.organization/1)
105+
|> Enum.each(&Repo.insert!/1)
106+
_more_than_0 ->
107+
IO.puts "Organizations detected, aborting organization seed."
68108
end
69109

70110
projects = [
@@ -82,16 +122,13 @@ projects = [
82122
}
83123
]
84124

85-
case Repo.all(Project) do
86-
[] ->
125+
case Repo.aggregate(Project, :count, :id) do
126+
0 ->
87127
projects
88-
|> Enum.each(fn params ->
89-
%Project{}
90-
|> Project.create_changeset(params)
91-
|> Ecto.Changeset.put_change(:approved, true)
92-
|> Repo.insert!()
93-
end)
94-
_ -> IO.puts "Projects detected, aborting project seed."
128+
|> Enum.map(&SeedChangeset.project/1)
129+
|> Enum.each(&Repo.insert!/1)
130+
_more_than_0 ->
131+
IO.puts "Projects detected, aborting project seed."
95132
end
96133

97134
project_users = [

priv/repo/structure.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
-- PostgreSQL database dump
33
--
44

5-
-- Dumped from database version 9.5.10
6-
-- Dumped by pg_dump version 10.1
5+
-- Dumped from database version 10.0
6+
-- Dumped by pg_dump version 10.0
77

88
SET statement_timeout = 0;
99
SET lock_timeout = 0;

0 commit comments

Comments
 (0)