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 / 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 / registration_controller.ex
Last active November 20, 2021 12:29
Medium Article: Building Many-To-Many Associations with Embedded Schemas in Ecto and Phoenix
defmodule App.RegistrationController do
use App.Web, :controller
alias App.{Registration, Repo}
def new(conn, _params) do
changeset = Registration.changeset(%Registration{})
render conn, :new, changeset: changeset
end
def create(conn, %{"registration" => registration_params}) do
@abitdodgy
abitdodgy / registration.ex
Last active August 23, 2021 15:37
Medium Article: Building Many-To-Many Associations with Embedded Schemas in Ecto and Phoenix
defmodule App.Registration do
use App.Web, :model
alias App.{Account, User, Membership, Repo}
embedded_schema do
field :email
field :org_name
end
@required_fields ~w(email org_name)a
@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
defmodule Event.Plug.API.VersionRouter do
import Plug.Conn
@default_version 1
def init(options) do
case Keyword.fetch!(options, :versions) do
versions ->
for {version, router} <- versions, into: %{} do
{version, {router, router.init([])}}
defmodule Event.Plug.API.VersionRouter do
import Plug.Conn
@default_version 1
def init(options) do
for {version, router} <- options, into: %{} do
{version, {router, router.init([])}}
end
end
@abitdodgy
abitdodgy / array_reverse.rb
Last active January 5, 2020 10:43
Array Algorithms
#
# 2 - REVERSAL
#
# The quickest way to reverse an array is to swap elements at either end, and repeat
# while moving up from the left and down from the right.
#
# [ a b c d e f ]
# [ f a ] - Step 1 we switched A[0] with A[-1]
# [ e b ] - Step 2 we switched A[1] with A[-2]
# [ d c ] - Step 3 we switched A[2] with A[-3]