Skip to content

Instantly share code, notes, and snippets.

View alanpeabody's full-sized avatar

Alan Peabody alanpeabody

View GitHub Profile
@alanpeabody
alanpeabody / my_app.ex
Last active March 24, 2024 13:28
Websockets in Elixir with Cowboy and Plug
defmodule MyApp do
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
Plug.Adapters.Cowboy.child_spec(:http, MyApp.Router, [], [
dispatch: dispatch
])
@alanpeabody
alanpeabody / model.ex
Last active January 3, 2024 03:29
Ecto change password w/ confirmation.
defmodule User do
use Ecto.Model
schema "users" do
field :email, :string
field :hashed_password, :string
field :password, :string, virtual: true
field :password_confirmation, virtual: true
timestamps
@alanpeabody
alanpeabody / best_practices.md
Last active October 28, 2020 23:33
Ember Best Practices

Ember Best Practices

Some thoughts and ideas on best practices building Ember apps after 2 years building and maintaining 6+ apps. This is less about the obvious best practices, like use ember-cli, and more along the lines of when to use what technique. As with every best practice there are exceptions to every rule.

Ember data

Ember data relationships

Routing

@alanpeabody
alanpeabody / dev_code_reload_plug.ex
Created June 30, 2015 14:42
dev_code_reload_plug.ex
# Reload/recompile code before each request in development only using Plug & Mix.
# Assumes MyApp.Router is a plug that handles actual routing.
def MyApp.Main do
use Plug.Builder
plug :reload
plug :dispatch
def reload(conn, _opts) do
if Mix.env == :dev, do: Mix.Tasks.Compile.Elixir.run(["--ignore-module-conflict"])
@alanpeabody
alanpeabody / A_README.md
Created July 17, 2016 15:33
Running a elixir dependency's migrations from parent app (in parent repo)

Goal

To pull in dependencies and have them run their migrations in the parent apps' repo.

Setup

MyChild - A mix application with ecto migrations, schemas, etc. MyParent - A phoenix application that wants to leverage the child application.

In this case MyParent has added MyChild as a mix dependency via github, eg:

@alanpeabody
alanpeabody / local_storage.ex
Last active July 16, 2017 15:30
JS's LocalStorage in Elixir - A super simple intro to GenServer for js devs
defmodule LocalStorage do
use GenServer
# API
def start_link do
GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
end
def set_item(key, val) do
@alanpeabody
alanpeabody / output
Created September 3, 2016 14:36
mix dialyzer
Unknown functions:
'Elixir.JaSerializer.Formatter.Function':'__impl__'/1
'Elixir.JaSerializer.Formatter.Map':'__impl__'/1
'Elixir.JaSerializer.Formatter.PID':'__impl__'/1
'Elixir.JaSerializer.Formatter.Port':'__impl__'/1
'Elixir.JaSerializer.Formatter.Reference':'__impl__'/1
'Elixir.JaSerializer.Formatter.Tuple':'__impl__'/1
@alanpeabody
alanpeabody / silly_long_elixir_function_pattern_match.ex
Created December 3, 2014 01:36
silly_long_elixir_function_pattern_match.ex
defmodule Post do
def create(%{title: t, body: b, author_id: a} = atts) when is_binary(t) and is_binary(b) and is_binary(a) do
{:ok, Dict.merge(atts, :id, 1234)}
end
def create(_atts) do
{:error, %{error: "Invalid attributes"}}
end
end

Keybase proof

I hereby claim:

  • I am alanpeabody on github.
  • I am alanpeabody (https://keybase.io/alanpeabody) on keybase.
  • I have a public key ASBfa3-DEWUJUKe4a5LeMKEeNJ3dyBwn93SXcewQPoDMpQo

To claim this, I am signing this object:

@alanpeabody
alanpeabody / ls
Created January 13, 2014 02:49
npm ls
Project@0.0.1 /project_path/
├─┬ browserify@3.19.1
│ ├── assert@1.1.0
│ ├─┬ browser-pack@2.0.1
│ │ ├─┬ combine-source-map@0.3.0
│ │ │ ├── convert-source-map@0.3.3
│ │ │ ├── inline-source-map@0.3.0
│ │ │ └─┬ source-map@0.1.31
│ │ │ └── amdefine@0.1.0