Skip to content

Instantly share code, notes, and snippets.

View imranismail's full-sized avatar

Imran Ismail imranismail

View GitHub Profile
@imranismail
imranismail / issue.md
Last active November 1, 2016 18:02
How to normalize "belongs to" association redux?

Given this kind of relationship from

GET /projects/1

const response = {
  id: 1,
  name: 'Normalized Redux',
  type: 'project',
  task_lists: [
@imranismail
imranismail / router_factory.ex
Last active November 17, 2016 18:21
router_factory.ex
# router_factory.ex
defmodule App.RouterFactory do
use Plug.Builder
def init(opts) do
opts
end
def call(conn, opts) do
router = make(conn.req_headers)
@imranismail
imranismail / turbolinks.ex
Last active November 24, 2020 09:49
Turbolinks Plug
@imranismail
imranismail / service-workers.md
Created November 18, 2016 08:25 — forked from Rich-Harris/service-workers.md
Stuff I wish I'd known sooner about service workers

Stuff I wish I'd known sooner about service workers

I recently had several days of extremely frustrating experiences with service workers. Here are a few things I've since learned which would have made my life much easier but which isn't particularly obvious from most of the blog posts and videos I've seen.

I'll add to this list over time – suggested additions welcome in the comments or via twitter.com/rich_harris.

Use Canary for development instead of Chrome stable

Chrome 51 has some pretty wild behaviour related to console.log in service workers. Canary doesn't, and it has a load of really good service worker related stuff in devtools.

@imranismail
imranismail / README.md
Last active July 28, 2017 08:48
Repo composition

Repo Composition

Motive

Ecto.Repo by default doesn't allow overrides. So when you need to compose functions but maintain an API parity with the Ecto.Repo. You'd need to compose the functions in another module and things can get messy really fast that way.

Can't invoke super

== Compilation error on file lib/app/repo.ex ==
** (CompileError) lib/app/repo.ex:6: no super defined for all/2 in module App.Repo. Overridable functions available are:
@imranismail
imranismail / README.md
Last active December 27, 2016 09:10
Enum for Ecto
@imranismail
imranismail / README.md
Last active January 5, 2017 15:37
Implementing Ruby's lonely operator using Elixir's custom infix function

Implementing Ruby's lonely operator using Elixir's custom infix function

iex> import ViewHelper
iex> user = %{name: "Imran", address: %{postcode: 33160}}
iex> user ~> :name
# "Imran"
iex> user ~> :address ~> :postcode
# 33160
iex> user ~> :address ~> :country
@imranismail
imranismail / attachment.ex
Created January 1, 2017 16:54
Media Prototype
defmodule Blog.Attachment do
use Blog, :schema
@primary_key {:id, :binary_id, autogenerate: true}
@storage_path "attachments"
schema "attachments" do
field :file, :any, virtual: true
belongs_to :post, Post
@imranismail
imranismail / weekly_stats.ex
Last active March 8, 2017 08:42
Weekly Stats in Ecto
initial = %{sunday: 0, monday: 0, tuesday: 0, wednesday: 0, thursday: 0, friday: 0, saturday: 0}
from(o in Order,
where: o.inserted_at > fragment("DATE_TRUNC('week', CURRENT_TIMESTAMP)"),
group_by: fragment("EXTRACT(DOW FROM ?)", o.inserted_at),
order_by: fragment("EXTRACT(DOW FROM ?)", o.inserted_at),
select: {fragment("EXTRACT(DOW FROM ?)", o.inserted_at), count(o.id)}
)
|> Repo.all()
|> Enum.reduce(initial, fn
@imranismail
imranismail / cache.ex
Created March 21, 2017 02:33
Caching API with Maxwell and Cachex
defmodule Shopify.Cache do
use Maxwell.Middleware
alias :timer, as: Timer
@ttl Timer.minutes(5)
def call(conn, next, opts), do: get!(conn, fn _ -> super(conn, next, opts) end)
def get!(key, fallback \\ nil) do