Skip to content

Instantly share code, notes, and snippets.

View abitdodgy's full-sized avatar
😃
Out to lunch!

Mohamad El-Husseini abitdodgy

😃
Out to lunch!
View GitHub Profile
@abitdodgy
abitdodgy / membership.ex
Created November 21, 2016 19:30
Medium Article: Medium Article: Building Many-to-Many Associations with cast_assoc and Nested Forms in Phoenix and Ecto
defmodule App.Membership do
schema "memberships" do
field :role
end
end
@abitdodgy
abitdodgy / schemas.ex
Created November 21, 2016 19:30
Medium Article: Medium Article: Building Many-to-Many Associations with cast_assoc and Nested Forms in Phoenix and Ecto
defmodule App.Account do
# ...
def changeset(struct, params \\ %{}) do
struct
# ...
|> cast_assoc(:memberships, required: true)
end
end
defmodule App.Membership do
# ...
@abitdodgy
abitdodgy / registration_controller.ex
Created November 21, 2016 19:29
Medium Article: Medium Article: Building Many-to-Many Associations with cast_assoc and Nested Forms in Phoenix and Ecto
defmodule Class.RegistrationController do
# ...
def create(conn, %{"account" => params}) do
changeset = Account.changeset(%Account{}, params)
case Repo.insert(changeset) do
{:ok, account} ->
redirect conn, to: registration_path(conn, :new)
{:error, changeset} ->
changeset = %{changeset | action: :insert}
@abitdodgy
abitdodgy / new.html.eex
Created November 21, 2016 19:28
Medium Article: Medium Article: Building Many-to-Many Associations with cast_assoc and Nested Forms in Phoenix and Ecto
<%= form_for @changeset, registration_path(@conn, :create), fn f -> %>
<div class="form-group">
<%= label f, :name %>
<%= text_input f, :name, class: "form-control" %>
<%= error_tag f, :name %>
</div>
<div class="form-group">
<%= inputs_for f, :memberships, fn mf -> %>
<%= inputs_for mf, :user, fn uf -> %>
<div class="form-group">
@abitdodgy
abitdodgy / registration_controller.ex
Created November 21, 2016 19:27
Medium Article: Building Many-to-Many Associations with cast_assoc and Nested Forms in Phoenix and Ecto
defmodule App.RegistrationController do
# ...
def new(conn, _params) do
changeset =
Account.changeset(
%Account{memberships: [
%Membership{user: %User{}}]})
render conn, "new.html", changeset: changeset
end
end
@abitdodgy
abitdodgy / params.txt
Created November 21, 2016 19:26
Medium Article: Building Many-to-Many Associations with cast_assoc and Nested Forms in Phoenix and Ecto
%{"account" => %{
"name" => "Ecto Phoenix Demo",
"memberships" => %{
"0" => %{
"user" => %{
"email" => "mohamad@example.com"}}}}
@abitdodgy
abitdodgy / schemas.ex
Created November 21, 2016 19:25
Medium Article: Building Many-to-Many Associations with cast_assoc and Nested Forms in Phoenix and Ecto
defmodule App.Account do
schema "accounts" do
field :name, :string
has_many :memberships, App.Membership
has_many :users, through: [:memberships, :user]
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:name])
|> validate_required([:name])
@abitdodgy
abitdodgy / new.html.eex
Last active November 21, 2016 19:18
Medium Article: Building Many-To-Many Associations with Embedded Schemas in Ecto and Phoenix
<%= form_for @changeset, registration_path(@conn, :create), fn f -> %>
<div class="form-group">
<%= label f, :org_name %>
<%= text_input f, :org_name, class: "form-control" %>
<%= error_tag f, :org_name %>
</div>
<div class="form-group">
<%= label f, :email %>
<%= text_input f, :email, class: "form-control" %>
<%= error_tag f, :email %>
@abitdodgy
abitdodgy / README.md
Created November 8, 2016 00:02 — forked from raysegantii/README.md
Use bootstrap-sass npm package with Phoenix's brunch
  1. install npm packages
  2. update brunch-config.js
  3. rename web/static/css/app.css to web/static/css/app.scss
  4. update web/static/css/app.scss
@abitdodgy
abitdodgy / version_router_exception.ex
Created May 15, 2016 16:56
API Version router using `send_resp` vs a custom exceptin.
defmodule Event.Plug.API.VersionRouter do
@default_api_version "1"
def init(options) do
case Keyword.fetch!(options, :versions) do
versions ->
for {version, router} <- versions, into: %{} do
{version, {router, router.init([])}}
end
end