Created
November 29, 2016 16:43
-
-
Save Donavan/e32f1d1f96d44771f7578306e7ee10e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Web.Team do | |
use Web.Web, :model | |
schema "teams" do | |
field :name, :string | |
many_to_many :users, join_through: 'team_members', on_delete: :delete_all | |
timestamps() | |
end | |
@doc """ | |
Builds a changeset based on the `struct` and `params`. | |
""" | |
def changeset(struct, params \\ %{}) do | |
struct | |
|> cast(params, [:name]) | |
|> validate_required([:name]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Web.TeamMember do | |
use Web.Web, :model | |
schema "team_members" do | |
has_one :user, User | |
has_one :team, Team | |
timestamps() | |
end | |
@doc """ | |
Builds a changeset based on the `struct` and `params`. | |
""" | |
def changeset(struct, params \\ %{}) do | |
struct | |
|> cast(params, [:user_id, :team_id]) | |
|> validate_required([:user_id, :team_id]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Web.User do | |
use Web.Web, :model | |
use Coherence.Schema | |
schema "users" do | |
field :name, :string | |
field :email, :string | |
coherence_schema | |
many_to_many :teams, Web.Team, join_through: 'team_members' | |
timestamps | |
end | |
def changeset(model, params \\ %{}) do | |
model | |
|> cast(params, [:name, :email] ++ coherence_fields) | |
|> validate_required([:name, :email]) | |
|> unique_constraint(:email) | |
|> validate_coherence(params) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment