Skip to content

Instantly share code, notes, and snippets.

View nshafer's full-sized avatar

Nathan Shafer nshafer

View GitHub Profile
@nshafer
nshafer / debug.ex
Created August 31, 2025 12:55
Macro debug helper `dbg_m`
defmodule Debug do
defmacro dbg_m(ast, options \\ []) do
options = Keyword.put_new(options, :header, format_dbg_m_header(__CALLER__))
ast
|> Macro.expand(__CALLER__)
|> write_ast(options)
ast
end
@nshafer
nshafer / uri.ex
Created August 16, 2025 19:27
with_params
defmodule Myapp.URI
@doc """
Builds a URL or path with the given query parameters.
Accepts a URL or path and a keyword list or map of parameters. Handles merging, overriding, and removing
parameters, including support for complex values (lists, maps and keyword lists as supported by `Plug.Conn.Query/2`).
If a value of `[value: v, default: d]` option is given, the parameter is omitted if `v == d`. In this format,
`:value` must come first, otherwise it is treated as a list. If you must provide a list with `{:value, _something}`,
then either make sure it's not first, or pass it as: `foo: [value: [value: "bar"]]`.

Slot propogation

Mix.install([
  {:phoenix_playground, "~> 0.1.6"}
])

Debug

@nshafer
nshafer / refresh.diff
Last active March 15, 2025 19:58
Refresh remember_me cookie for mix.gen.auth
diff --git a/lib/myapp_web/user_auth.ex b/lib/myapp_web/user_auth.ex
index 37dfa43..fa1fa74 100644
--- a/lib/myapp_web/user_auth.ex
+++ b/lib/myapp_web/user_auth.ex
@@ -13,6 +13,10 @@ defmodule MyappWeb.UserAuth do
@remember_me_cookie "_myapp_web_user_remember_me"
@remember_me_options [sign: true, max_age: @max_age, same_site: "Lax"]
+ # Refresh the user_token and remember-me cookie every hour, so an active
+ # user will have their login credentials refreshed
@nshafer
nshafer / upload_live.ex
Created June 17, 2024 14:53
Process uploaded file
def handle_event("import", _params, socket) do
results =
consume_uploaded_entries(socket, :import, fn %{path: path}, _entry ->
temp_dir = Briefly.create!(type: :directory)
csv_file = Path.join(temp_dir, "import.csv")
# Attempt to create a new hard link to the file
case File.ln(path, csv_file) do
:ok ->
{:ok, csv_file}
@nshafer
nshafer / form_modal_live.ex
Created November 16, 2023 21:51
FormModalLive
defmodule Default1710Web.FormModalLive do
use Default1710Web, :live_view
def render(assigns) do
~H"""
<.live_component module={Default1710Web.InnerForm} id="form" />
<button phx-click="open_modal">Open Modal</button>
<.modal :if={@show_modal} id="modal" show on_cancel={JS.push("close_modal")}>
@nshafer
nshafer / adlist.json
Created October 9, 2023 17:43
Pihole lists
[
{
"id": 1,
"address": "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts",
"enabled": 1,
"date_added": 1589393137,
"date_modified": 1678635720,
"comment": "Migrated from /etc/pihole/adlists.list",
"date_updated": 1696765562,
"number": 137890,
@nshafer
nshafer / gist:8e12aa557cc91c6b16a6057e83e4741e
Created September 1, 2022 15:29
New allowed wordle words 8/29
aapas
abaht
abeat
abeer
abeng
abeys
abius
abjad
abjud
abnet
@nshafer
nshafer / custom_workon.sh
Last active July 14, 2022 16:27
Extend the virtualenvwrapper's `workon` command with other non-python project directories
#!/bin/bash
# Extend the `workon` command to include just random directories to CD into without activating
# a virtualenv.
#
# Source into .bashrc or .bash_profile
#
# source ~/custom_workon.sh
declare -A projects
@nshafer
nshafer / admin.py
Created August 30, 2019 21:32
Session Admin for debugging
from django.contrib.sessions.models import Session
class SessionAdmin(admin.ModelAdmin):
list_display = ('session_key', 'decoded_session_data', 'expire_date')
readonly_fields = ('decoded_session_data',)
exclude = ('session_data',)
ordering = ('expire_date',)
search_fields = ('session_key',)
def decoded_session_data(self, obj):