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 / 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 / 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 / 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]
@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:
@abitdodgy
abitdodgy / s3_pre_signed_url.rb
Created January 10, 2014 16:01
Generate a pre-signed url for S3 direct upload.
class SignedUrlsController < ApplicationController
def s3
# Create client and set bucket
client = AWS::S3.new
bucket = client.buckets[ENV['S3_BUCKET']]
# Extract file extension since we're renaming the file
extension = File.extname(params[:image_name]).downcase
# Get mime-type from extension

Estrutura anterior:

/
  + I001
    + new
    + processed
  + I002
    + new
 + processed
@abitdodgy
abitdodgy / Ubuntu Setup Guide for Rails Deployment.md
Last active January 29, 2018 16:02
A guide for setting up an Ubuntu server for Rails deployment

Rails Deployment with Ubuntu 12.10, RVM, Postgresql, Nginx, and Unicorn.

This setup was used successfully on a DigitalOcean VPS. After much trial and error, and following a number of disparate guides and tutorials, I was able to distil the process to the information found in this gist.

Before getting started, you should create a new server droplet. Login to verify that everything is working correctly.

ssh root@xxx.xxx.xx.xx
@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