Skip to content

Instantly share code, notes, and snippets.

😆
Out to lunch!

Mohamad El-Husseini abitdodgy

😆
Out to lunch!
Block or report user

Report or block abitdodgy

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View file.md

Estrutura anterior:

/
  + I001
    + new
    + processed
  + I002
    + new
    + processed
@abitdodgy
abitdodgy / resize_iframe.js
Last active Sep 23, 2017
Resizes iframe to fit content on window resize and load events.
View resize_iframe.js
var _pc_resizeIFrame = function(iframe) {
iframe.height = iframe.contentDocument.body.scrollHeight;
}
document.querySelectorAll(".pc-iframe").forEach(function(iframe) {
window.onload = function () {
_pc_resizeIFrame(iframe);
};
window.onresize = function () {
_pc_resizeIFrame(iframe);
@abitdodgy
abitdodgy / membership.ex
Created Nov 21, 2016
Medium Article: Medium Article: Building Many-to-Many Associations with cast_assoc and Nested Forms in Phoenix and Ecto
View membership.ex
defmodule App.Membership do
# ...
def changeset(struct, params \\ %{}) do
struct
# ...
|> put_role
|> cast_assoc(:user, required: true)
end
defp put_role(changeset) do
@abitdodgy
abitdodgy / membership.ex
Created Nov 21, 2016
Medium Article: Medium Article: Building Many-to-Many Associations with cast_assoc and Nested Forms in Phoenix and Ecto
View membership.ex
defmodule App.Membership do
schema "memberships" do
field :role
end
end
@abitdodgy
abitdodgy / schemas.ex
Created Nov 21, 2016
Medium Article: Medium Article: Building Many-to-Many Associations with cast_assoc and Nested Forms in Phoenix and Ecto
View schemas.ex
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 Nov 21, 2016
Medium Article: Medium Article: Building Many-to-Many Associations with cast_assoc and Nested Forms in Phoenix and Ecto
View registration_controller.ex
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 Nov 21, 2016
Medium Article: Medium Article: Building Many-to-Many Associations with cast_assoc and Nested Forms in Phoenix and Ecto
View new.html.eex
<%= 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 Nov 21, 2016
Medium Article: Building Many-to-Many Associations with cast_assoc and Nested Forms in Phoenix and Ecto
View registration_controller.ex
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 Nov 21, 2016
Medium Article: Building Many-to-Many Associations with cast_assoc and Nested Forms in Phoenix and Ecto
View params.txt
%{"account" => %{
"name" => "Ecto Phoenix Demo",
"memberships" => %{
"0" => %{
"user" => %{
"email" => "mohamad@example.com"}}}}
@abitdodgy
abitdodgy / schemas.ex
Created Nov 21, 2016
Medium Article: Building Many-to-Many Associations with cast_assoc and Nested Forms in Phoenix and Ecto
View schemas.ex
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])
You can’t perform that action at this time.