Skip to content

Instantly share code, notes, and snippets.

Avatar
🌏
Working from Asia

Imran Ismail imranismail

🌏
Working from Asia
View GitHub Profile
@mgwidmann
mgwidmann / ecto_polymorphism.ex
Last active September 29, 2020 14:45
Ecto Polymorphism
View ecto_polymorphism.ex
defmodule Ecto.Polymorphic do
defmacro __using__(_) do
quote do
require Ecto.Schema
import Ecto.Schema, except: [belongs_to: 2, belongs_to: 3]
import unquote(__MODULE__)
end
end
@syamilmj
syamilmj / enum.ex
Created October 29, 2016 07:31
Add enum datatype support to Ecto
View enum.ex
defmodule Inventory.Enum do
@moduledoc """
Provides macro to support Enum datatype with Ecto
## Usage
In your model:
import Inventory.Enum
View epmdless.ex
# Requires Erlang/OTP 19.0. Invoke as:
#
# iex --erl "-proto_dist Elixir.Epmdless -start_epmd false -epmd_module Elixir.Epmdless_epmd_client" --name frobozz3
# A module containing the function that determines the port number
# based on a node name.
defmodule Epmdless do
def dist_port(name) when is_atom(name) do
dist_port Atom.to_string name
end
View authenticator_service.ex
defmodule Reactor.Service.Authenticator do
@behaviour Reactor.Contract.Service.Authenticator
@moduledoc """
User authentication service.
"""
use Reactor.Resolver, repositories: [:user]
alias Comeonin.Bcrypt, as: Comeonin
alias Reactor.Entity.User, as: UserEntity
@ahmadshah
ahmadshah / README.md
Last active January 6, 2021 15:21
Ecto Soft Delete
View README.md

Soft Delete Ecto Repo

The goal is to support soft delete functionality in Ecto.Repo. With the suggestion by @imranismail, another repo is created and the remaining functionalities are delegate to the original MyApp.Repo.

The new repo get/2 and all/1 functions will exclude the soft deleted record by default. delete/1 and delete_all/1 will update the delete_at column by default instead of deleting.

Example

MyApp.Repo.get(MyApp.User, 1) //will return nil if record is in soft delete state
@Rich-Harris
Rich-Harris / service-workers.md
Last active January 26, 2023 22:58
Stuff I wish I'd known sooner about service workers
View service-workers.md

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 / spectacle_app_extractor.sh
Created September 26, 2015 05:23
Spectacleapp Preference Extractor
View spectacle_app_extractor.sh
for key in MoveToPreviousThird MoveToRightDisplay MakeLarger MoveToRightHalf MakeSmaller MoveToTopDisplay MoveToBottomDisplay MoveToTopHalf MoveToBottomHalf MoveToUpperLeft MoveToCenter MoveToUpperRight MoveToFullscreen MoveToLeftDisplay MoveToLeftHalf MoveToLowerLeft RedoLastMove MoveToLowerRight MoveToNextDisplay MoveToNextThird MoveToPreviousDisplay UndoLastMove; do
printf "defaults write com.divisiblebyzero.Spectacle ${key} -data ";
defaults read com.divisiblebyzero.Spectacle ${key} | sed 's/[^0-9a-f]//g';
done
@rbishop
rbishop / README.md
Last active April 26, 2022 15:38
A super simple Elixir server for sending Server Sent Events to the browser.
View README.md

Generate a new Elixir project using mix and add cowboy and plug as dependencies in mix.exs:

  defp deps do
    [
      {:cowboy, "~> 1.0.0"},
      {:plug, "~> 0.8.1"}
    ]
  end
@jed
jed / how-to-set-up-stress-free-ssl-on-os-x.md
Last active January 31, 2023 19:00
How to set up stress-free SSL on an OS X development machine
View how-to-set-up-stress-free-ssl-on-os-x.md

How to set up stress-free SSL on an OS X development machine

One of the best ways to reduce complexity (read: stress) in web development is to minimize the differences between your development and production environments. After being frustrated by attempts to unify the approach to SSL on my local machine and in production, I searched for a workflow that would make the protocol invisible to me between all environments.

Most workflows make the following compromises:

  • Use HTTPS in production but HTTP locally. This is annoying because it makes the environments inconsistent, and the protocol choices leak up into the stack. For example, your web application needs to understand the underlying protocol when using the secure flag for cookies. If you don't get this right, your HTTP development server won't be able to read the cookies it writes, or worse, your HTTPS production server could pass sensitive cookies over an insecure connection.

  • Use production SSL certificates locally. This is annoying