Skip to content

Instantly share code, notes, and snippets.

@PJUllrich
PJUllrich / http.ex
Last active January 25, 2024 09:09
HTTP Wrapper with optional Caching
defmodule MyApp.Http do
@moduledoc """
Exposes functions to make HTTP requests and optionally cache the response.
If you want to cache the request, simply add `cache: true` to the
request options. You can also define an option time-to-live (TTL) with
`cache_ttl: ttl_in_milliseconds`. The default TTL is 5min.
Only caches 2xx responses.
"""
@mxgrn
mxgrn / lv_modal_close_confirm.md
Last active June 15, 2024 14:18
LiveView modal close confirmation on dirty form
@toranb
toranb / bert_medium_training.ex
Last active November 14, 2023 15:56
The bumblebee fine tuning example with one of the smaller Pytorch pre-trained BERT variants
defmodule Training.Example do
def train() do
Nx.default_backend(EXLA.Backend)
{:ok, spec} =
Bumblebee.load_spec({:hf, "prajjwal1/bert-medium"},
module: Bumblebee.Text.Bert,
architecture: :for_sequence_classification
)
@arjan
arjan / live_acl.ex
Created March 7, 2023 11:13
LiveView ACL check example code
defmodule ExampleApp.Web.LiveAcl do
@moduledoc """
ACL checks for LiveView
```
use ExampleApp.Web.LiveAcl
```
Provides an `on_mount` hook to do ACL checks on mount, for instance
to ensure the current user has access to a given invoice ID.
@andreaseriksson
andreaseriksson / convert_to_verified_routes.ex
Last active March 31, 2024 12:29
This is a mix task for converting old Phoenix routes to new verified routes
defmodule Mix.Tasks.ConvertToVerifiedRoutes do
@shortdoc "Fix routes"
use Mix.Task
@regex ~r/(Routes\.)(.*)_(path|url)\(.*?\)/
@web_module MyAppWeb
def run(_) do
Path.wildcard("lib/**/*.*ex")
@jmatsushita
jmatsushita / README
Last active November 3, 2024 15:40
Setup nix, nix-darwin and home-manager from scratch on an M1 Macbook Pro
###
### [2023-06-19] UPDATE: Just tried to use my instructions again on a fresh install and it failed in a number of places.
###. Not sure if I'll update this gist (though I realise it seems to still have some traffic), but here's a list of
###. things to watch out for:
### - Check out the `nix-darwin` instructions, as they have changed.
### - There's a home manager gotcha https://github.com/nix-community/home-manager/issues/4026
###
# I found some good resources but they seem to do a bit too much (maybe from a time when there were more bugs).
# So here's a minimal Gist which worked for me as an install on a new M1 Pro.
@fuelen
fuelen / ecto_cond.ex
Created November 12, 2020 15:17
Ecto cond
defmodule Ecto.Extension.Query.API do
defmacro cond_(do: block) do
bindings =
Enum.reduce(block, [], fn
{:->, _, [[clause], branch]}, acc ->
[branch, clause | acc]
end)
|> Enum.reverse()
bindings_number = length(bindings)
@wosephjeber
wosephjeber / instructions.md
Last active October 24, 2024 12:43
Ecto migration for renaming table with indexes and constraints

Renaming table in Ecto migration

I recently wanted to rename a model and its postgres table in a Phoenix app. Renaming the table was simple and documented, but the table also had constraints, sequences, and indexes that needed to be updated in order for the Ecto model to be able to rely on default naming conventions. I couldn't find any examples of what this would look like but was eventually able to figure it out. For anyone else in the same situation, hopefully this example helps.

In the example below, I'm renaming the Permission model to Membership. This model belongs to a User and an Account, so it has foreign key constraints that need to be renamed.

defmodule MyApp.Repo.Migrations.RenamePermissionsToMemberships do
  use Ecto.Migration
@isubasti
isubasti / Profilling and Debugging Phoenix Erlang VM.md
Created March 15, 2018 07:20
Profilling and Debugging Phoenix Erlang VM inside/outside nanobox