Skip to content

Instantly share code, notes, and snippets.

View narrowtux's full-sized avatar
:shipit:
Shipping it

Moritz Schmale narrowtux

:shipit:
Shipping it
View GitHub Profile
def descendants(organization_id, truncation, filter \\ from o in __MODULE__) do
sub = from os in filter, where: os.organization_id == ^organization_id and os.truncation == ^truncation, select: [os.organization_id, os.parent_organization_id, os.id, ^1, os.datetime, os.truncation]
query = from outer in __MODULE__,
join: t in fragment("""
WITH RECURSIVE search_orgs(organization_id, parent_organization_id, id, depth, datetime, truncation) AS (
?
UNION ALL
SELECT
o.organization_id,
def descendants(organization_id, truncation, filter \\ from o in __MODULE__) do
sub = from os in filter, where: os.organization_id == ^organization_id and os.truncation == ^truncation, select: [os.organization_id, os.parent_organization_id, os.id, ^1, os.datetime, os.truncation]
query = from outer in __MODULE__,
join: t in fragment("""
WITH RECURSIVE search_orgs(organization_id, parent_organization_id, id, depth, datetime, truncation) AS (
?
UNION ALL
SELECT
o.organization_id,
def descendants(organization_id, truncation, filter \\ from o in __MODULE__) do
sub = from os in filter, where: os.organization_id == ^organization_id and os.truncation == ^truncation, select: [os.organization_id, os.parent_organization_id, os.id, ^1, os.datetime, os.truncation]
query = from outer in __MODULE__,
join: t in fragment("""
WITH RECURSIVE search_orgs(organization_id, parent_organization_id, id, depth, datetime, truncation) AS (
?
UNION ALL
SELECT
o.organization_id,
@narrowtux
narrowtux / collection.ex
Last active November 15, 2016 08:04
remove and difference functions for Enum
defmodule Coll do
@doc """
Removes all values from the subject
iex> Coll.remove([1, 2, 3], [2, 3])
[1]
iex> Coll.remove([1, 2], [2, 3, 4])
[1]
"""
@narrowtux
narrowtux / consumer_channel.ex
Created October 24, 2016 11:10
Phoenix.Channel + GenStage
defmodule MyApp.Channel do
alias Experimental.GenStage
use GenStage
use MyApp.Web, :channel
def init(socket) do
{:consumer, socket, subscribe_to: [MyApp.BroadcastStage]}
end
def handle_events(events, _from, socket) do
# get events pushed directly to each channel connection
Enum.each events, &Phoenix.Channel.push(socket, "message", &1)
defmodule Range
def range(num, min_value, max_value) do
num
|> min(max_value)
|> max(min_value)
end
end
@narrowtux
narrowtux / model.ex
Last active February 16, 2023 08:39
Recursive models with ecto
defmodule Model do
schema "models" do
field :foo, :string
has_many :children, Model, foreign_key: :parent_id
belongs_to :parent, Model, foreign_key: :parent_id
end
@doc """
Recursively loads parents into the given struct until it hits nil
"""
@narrowtux
narrowtux / OS X Shortcuts.md
Last active November 5, 2021 16:26
OS X Shortcuts

OS X Shortcuts

This gist aims to be a fairly complete list of all known shortcuts all around OS X (and some OS X applications).

How to ergonomically press shortcuts

In this gist, there are sometimes multiple shortcuts for the same action. In that case, the shortcuts are ordered by how ergonomically you can press them, in descending order. That means that you should always learn the first shortcut in the list.

@narrowtux
narrowtux / SimpleRandom.java
Created May 8, 2015 10:47
Pseudorandom implementation that is deterministic across the platforms Java and Arduino(AVR)
import java.util.Random;
public class SimpleRandom extends Random {
long seed;
public SimpleRandom(long seed) {
setSeed(seed);
}
@Override
@narrowtux
narrowtux / sevene1serial.h
Created March 30, 2015 15:37
Arduino SoftwareSerial 7E1 read support
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
#ifndef SEVEN_E_1_SERIAL
#define SEVEN_E_1_SERIAL
#define _DEBUG 0
#define _DEBUG_PIN1 11