Skip to content

Instantly share code, notes, and snippets.

View danhawkins's full-sized avatar

Danny Hawkins danhawkins

View GitHub Profile
@danhawkins
danhawkins / mix.exs
Last active April 19, 2022 15:41
Add releases
defmodule TestSvc.MixProject do
use Mix.Project
def project do
[
app: :test_svc,
version: "0.1.0",
elixir: "~> 1.12",
elixirc_paths: elixirc_paths(Mix.env()),
compilers: [:gettext] ++ Mix.compilers(),
@danhawkins
danhawkins / env.sh.eex
Created April 19, 2022 15:31
Elixir env.sh for K8S
#!/bin/sh
export POD_A_RECORD=$(echo $POD_IP | sed 's/\./-/g')
export RELEASE_DISTRIBUTION=name
export RELEASE_NODE=test_svc@${POD_A_RECORD}.${NAMESPACE}.pod.cluster.local
@danhawkins
danhawkins / live_helpers.ex
Last active April 18, 2022 02:40
Live Helpers for DateTime
defmodule QuiqupFleetWeb.LiveHelpers do
@moduledoc """
Helpers for Web components used in live view
"""
import Phoenix.LiveView
import Phoenix.LiveView.Helpers
@doc """
Renders a datetime using the browser based toLocaleString methods.
@danhawkins
danhawkins / config.exs
Created April 13, 2022 22:39
esbuild-config
# Phoenix
config :esbuild,
version: "0.12.18",
default: [
args:
~w(js/app.js --bundle --target=es2019 --outdir=../priv/static/assets --external:/fonts/* --external:/images/*),
cd: Path.expand("../assets", __DIR__),
env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)}
]
def list_groups(region \\ nil, search \\ nil) do
query =
from(g in Group)
|> filter_region(region)
|> add_rank(region)
|> search(search)
query |> Repo.all()
end
def list_groups(region \\ nil, search \\ nil) do
query =
from(g in Group)
|> filter_region(region)
|> add_rank(region)
query |> Repo.all()
end
# When no region is past we don't need to partition, we have global rank
def list_groups(region \\ nil, search \\ nil) do
query =
from(g in Group)
|> filter_region(region)
query |> Repo.all()
end
defp filter_region(query, nil), do: query
# lib/groups.ex
defmodule TestApp.Groups do
import Ecto.Query
alias TestApp.Repo
alias TestApp.Groups.Group
@doc """
Create a new group
# test/support/fixtures/group_fixtures.exs
defmodule TestApp.GroupFixtures do
@doc """
Generate groups.
"""
def group_fixtures() do
[
{"AE Group 1", "UAE", 10},
{"AE Group 2", "UAE", 30},
# priv/repo/migrations/20220319222511_create_groups.exs
defmodule TestApp.Repo.Migrations.CreateGroups do
use Ecto.Migration
def change do
create table(:groups) do
add :name, :string, null: false
add :region, :string, null: false
add :score, :integer, null: false, default: 0