Skip to content

Instantly share code, notes, and snippets.

View alanpeabody's full-sized avatar

Alan Peabody alanpeabody

View GitHub Profile
@alanpeabody
alanpeabody / .projections.json
Last active August 29, 2015 13:56
Ember App Kit & vim-projectile
{
"app/models/*.js": {
"command": "model",
"template": [ "export default DS.Model.extend({", "});" ]
},
"app/router.js": { "command": "router"},
"app/routes/*.js": {
"command": "route",
"template": [ "export default Ember.Route.extend({", "});" ],
"alternate": "app/controllers/%s.js"
@alanpeabody
alanpeabody / notes.md
Last active August 29, 2015 14:10
Web apps in elixir
@alanpeabody
alanpeabody / Runner.ex
Created February 7, 2015 15:25
drop_if_exists
defp execute_in_direction(repo, :forward, level, {:create_if_not_exists, %Index{}=index}) do
if !repo.adapter.ddl_exists?(repo, index, @opts) do
repo.adapter.execute_ddl(repo, {:create, index}, @opts)
end
end
defp execute_in_direction(repo, :backward, level, {:drop_if_exists, %Index{}=index}) do
if !repo.adapter.ddl_exists?(repo, index, @opts) do
repo.adapter.execute_ddl(repo, {:create, index}, @opts)
end
@alanpeabody
alanpeabody / config.ex
Last active August 29, 2015 14:16
embedding elixir structs part 1
# config/config.ex
config :app, App.Repo,
database: "my_app_dev",
username: "postgres",
password: "nope",
hostname: "localhost",
extensions: [{Extensions.Json, library: Poison}]
@alanpeabody
alanpeabody / json.ex
Created March 11, 2015 18:31
embedding elixir structs part 2
defmodule Extensions.JSON do
alias Postgrex.TypeInfo
@behaviour Postgrex.Extension
@json ["json", "jsonb"]
def init(_parameters, opts),
do: Keyword.fetch!(opts, :library)
def matching(_library),
@alanpeabody
alanpeabody / migration.ex
Last active August 29, 2015 14:16
embedding elixir structs step 3
# Generated with mix ecto.gen.migration add_user_settings_and_address
defmodule App.Repo.Migrations.AddUserSettingsAndAddress do
use Ecto.Migration
def change do
alter table(:users) do
add :settings, :json
add :addresses, :json
end
end
@alanpeabody
alanpeabody / user.ex
Created March 11, 2015 22:20
embedding elixir structs step 4
defmodule App.Models.User do
use Ecto.Model
schema "users" do
field :email, :string
field :crypted_pass, :string
field :settings, App.Models.Settings.Type
field :addresses, App.Models.Address.Type
timestamps
@alanpeabody
alanpeabody / settings.ex
Last active August 29, 2015 14:16
embedding elixir structs step 6
defmodule App.Models.Settings do
defstruct [
newsletter: false,
publish_profile: true,
email_notifications: true
]
defmodule Type do
@behaviour Ecto.Type
alias App.Models.Settings
@alanpeabody
alanpeabody / address.ex
Created March 11, 2015 23:50
elixir embedding step 7
defmodule App.Models.Address do
defstruct [:address, :address2, :city, :region, :postal, :country]
defmodule Type do
@behaviour Ecto.Type
alias App.Models.Address
def type, do: :json
def cast(addrs) when is_list(addrs) do
@alanpeabody
alanpeabody / plug_overview.ex
Last active August 29, 2015 14:20
Plug 101 - Part 1
conn
|> set_current_user
|> parse_json_body
|> create_model
|> set_exp_headers