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 / form.ex
Created April 27, 2020 22:30
ExBs Form
defmodule ExBs.Components.Form do
@moduledoc """
Helpers for building Phoenix forms with Bootstrap components.
"""
import ExBs.Config, only: [translation_fn: 0]
import ExBs.Components.Form.Input, only: [input: 3]
alias Phoenix.HTML.{Form, Tag}
@abitdodgy
abitdodgy / webpack_bootstrap_instructions.md
Last active October 22, 2019 19:24
Instructions for setting up Bootstrap with Webpack and Phoenix 1.4
  1. Install the following in assets:
npm install --save bootstrap jquery popper.js
npm install --save-dev sass-loader@7.3.1 node-sass
  1. Rename assets/app.css to assets/app.scss
  2. Edit webpack.config.js to add sass-loader and enable scss compilation:

Estrutura anterior:

/
  + I001
    + new
    + processed
  + I002
    + new
 + processed
@abitdodgy
abitdodgy / resize_iframe.js
Last active September 23, 2017 12:50
Resizes iframe to fit content on window resize and load events.
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 November 21, 2016 19:31
Medium Article: Medium Article: Building Many-to-Many Associations with cast_assoc and Nested Forms in Phoenix and Ecto
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 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