Skip to content

Commit a52b4e6

Browse files
authored
Merge pull request #719 from code-corps/709-make-user-owner-of-organization
Make user an owner of an organization directly
2 parents 0209b99 + c092628 commit a52b4e6

File tree

8 files changed

+69
-25
lines changed

8 files changed

+69
-25
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule CodeCorps.Repo.Migrations.AddOwnerToOrganization do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:organizations) do
6+
add :owner_id, references(:users, on_delete: :nothing)
7+
end
8+
end
9+
end

priv/repo/structure.sql

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ CREATE TABLE organizations (
197197
updated_at timestamp without time zone NOT NULL,
198198
approved boolean DEFAULT false,
199199
cloudinary_public_id character varying(255),
200-
default_color character varying(255)
200+
default_color character varying(255),
201+
owner_id integer
201202
);
202203

203204

@@ -2180,6 +2181,14 @@ ALTER TABLE ONLY organization_memberships
21802181
ADD CONSTRAINT organization_memberships_organization_id_fkey FOREIGN KEY (organization_id) REFERENCES organizations(id);
21812182

21822183

2184+
--
2185+
-- Name: organizations_owner_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
2186+
--
2187+
2188+
ALTER TABLE ONLY organizations
2189+
ADD CONSTRAINT organizations_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES users(id);
2190+
2191+
21832192
--
21842193
-- Name: previews_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
21852194
--
@@ -2528,5 +2537,5 @@ ALTER TABLE ONLY user_tasks
25282537
-- PostgreSQL database dump complete
25292538
--
25302539

2531-
INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170226050552);
2540+
INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170224233516), (20170226050552);
25322541

test/controllers/organization_controller_test.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ defmodule CodeCorps.OrganizationControllerTest do
4444

4545
describe "create" do
4646
@tag authenticated: :admin
47-
test "creates and renders resource when data is valid", %{conn: conn} do
48-
assert conn |> request_create(@valid_attrs) |> json_response(201)
47+
test "creates and renders resource when data is valid", %{conn: conn, current_user: user} do
48+
attrs = Map.merge(@valid_attrs, %{owner: user})
49+
assert conn |> request_create(attrs) |> json_response(201)
4950
end
5051

5152
@tag authenticated: :admin

test/models/organization_test.exs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,43 @@ defmodule CodeCorps.OrganizationTest do
33

44
alias CodeCorps.Organization
55

6-
@valid_attrs %{description: "Building a better future.", name: "Code Corps"}
7-
@invalid_attrs %{}
86

9-
test "changeset with valid attributes" do
10-
changeset = Organization.changeset(%Organization{}, @valid_attrs)
11-
assert changeset.valid?
12-
end
7+
describe "changeset" do
8+
@valid_attrs %{description: "Building a better future.", name: "Code Corps"}
9+
@invalid_attrs %{}
10+
test "with valid attributes" do
11+
changeset = Organization.changeset(%Organization{}, @valid_attrs)
12+
assert changeset.valid?
13+
end
1314

14-
test "changeset with invalid attributes" do
15-
changeset = Organization.changeset(%Organization{}, @invalid_attrs)
16-
refute changeset.valid?
15+
test "with invalid attributes" do
16+
changeset = Organization.changeset(%Organization{}, @invalid_attrs)
17+
refute changeset.valid?
18+
end
1719
end
1820

19-
test "create changeset with valid attributes" do
20-
changeset = Organization.create_changeset(%Organization{}, @valid_attrs)
21-
assert changeset.valid?
22-
assert changeset.changes.slug == "code-corps"
23-
end
21+
describe "create_changeset" do
22+
@valid_attrs %{owner_id: 1, description: "Building a better future.", name: "Code Corps"}
23+
@invalid_attrs %{}
24+
25+
test "with valid attributes" do
26+
changeset = Organization.create_changeset(%Organization{}, @valid_attrs)
27+
assert changeset.valid?
28+
assert changeset.changes.slug == "code-corps"
29+
end
30+
31+
test "with invalid attributes" do
32+
changeset = Organization.create_changeset(%Organization{}, @invalid_attrs)
33+
refute changeset.valid?
34+
end
35+
36+
test "ensures owner (user) exists" do
37+
changeset = Organization.create_changeset(%Organization{}, @valid_attrs)
38+
39+
{result, changeset} = changeset |> Repo.insert
2440

25-
test "create changeset with invalid attributes" do
26-
changeset = Organization.create_changeset(%Organization{}, @invalid_attrs)
27-
refute changeset.valid?
41+
assert result == :error
42+
changeset |> assert_error_message(:owner, "does not exist")
43+
end
2844
end
2945
end

test/support/factories.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ defmodule CodeCorps.Factories do
3232
def organization_factory do
3333
%CodeCorps.Organization{
3434
name: sequence(:username, &"Organization #{&1}"),
35+
owner: build(:user),
3536
slug: sequence(:slug, &"organization-#{&1}"),
3637
description: sequence(:email, &"Description of organization #{&1}"),
3738
}

test/views/organization_view_test.exs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ defmodule CodeCorps.OrganizationViewTest do
22
use CodeCorps.ViewCase
33

44
test "renders all attributes and relationships properly" do
5-
organization = insert(:organization, default_color: "blue")
6-
project = insert(:project, organization: organization)
75
user = insert(:user)
6+
organization = insert(:organization, owner: user, default_color: "blue")
7+
project = insert(:project, organization: organization)
88
organization_membership = insert(:organization_membership, member: user, organization: organization)
99
slugged_route = insert(:slugged_route, organization: organization)
1010
stripe_connect_account = insert(:stripe_connect_account, organization: organization)
@@ -27,6 +27,9 @@ defmodule CodeCorps.OrganizationViewTest do
2727
},
2828
"id" => organization.id |> Integer.to_string,
2929
"relationships" => %{
30+
"owner" => %{
31+
"data" => %{"id" => user.id |> Integer.to_string, "type" => "user"}
32+
},
3033
"organization-memberships" => %{
3134
"data" => [
3235
%{"id" => organization_membership.id |> Integer.to_string, "type" => "organization-membership"}

web/models/organization.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ defmodule CodeCorps.Organization do
2121
field :slug, :string
2222
field :approved, :boolean
2323

24+
belongs_to :owner, CodeCorps.User
25+
2426
has_one :slugged_route, CodeCorps.SluggedRoute
2527
has_one :stripe_connect_account, CodeCorps.StripeConnectAccount
2628

@@ -47,8 +49,10 @@ defmodule CodeCorps.Organization do
4749
def create_changeset(struct, params) do
4850
struct
4951
|> changeset(params)
52+
|> cast(params, [:owner_id])
5053
|> generate_slug(:name, :slug)
51-
|> validate_required([:slug, :description])
54+
|> validate_required([:description, :owner_id, :slug])
55+
|> assoc_constraint(:owner)
5256
|> validate_slug(:slug)
5357
|> put_slugged_route()
5458
|> generate_icon_color(:default_color)

web/views/organization_view.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule CodeCorps.OrganizationView do
22
alias CodeCorps.Cloudex.CloudinaryUrl
3-
use CodeCorps.PreloadHelpers, default_preloads: [:slugged_route, :stripe_connect_account, :organization_memberships, :projects]
3+
use CodeCorps.PreloadHelpers, default_preloads: [:owner, :organization_memberships, :projects, :slugged_route, :stripe_connect_account]
44
use CodeCorps.Web, :view
55
use JaSerializer.PhoenixView
66

@@ -9,6 +9,7 @@ defmodule CodeCorps.OrganizationView do
99
:icon_large_url, :name, :slug, :inserted_at, :updated_at
1010
]
1111

12+
has_one :owner, serializer: CodeCorps.UserView
1213
has_one :slugged_route, serializer: CodeCorps.SluggedRouteView
1314
has_one :stripe_connect_account, serializer: CodeCorps.StripeConnectAccountView
1415

0 commit comments

Comments
 (0)