Skip to content

Instantly share code, notes, and snippets.

View leobessa's full-sized avatar

Leonardo Bessa leobessa

View GitHub Profile
defmodule MyApp.Books.Book do
use Ecto.Schema
import Ecto.Query, warn: false
import Ecto.Changeset
import MyApp.ChangesetHelpers
schema "books" do
field :name, :string
field :genres, {:array, :string}, default: []
@brentjanderson
brentjanderson / README.md
Created June 14, 2022 15:53
Elixir runtime-controlled supervision tree using feature flags (circuit breaker)

These snippets provide a foundation for starting and stopping supervision trees at runtime using feature flags (e.g. Launch Darkly).

Some things to note when adapting these snippets:

  1. application.ex needs to be adapted into an existing application. The important part is that each child spec provided is compliant, and that there is a feature flag (ld_key) specified.
  2. As written, if a feature flag fails for some reason, it defaults to starting all children. There is room for adaptation here as needed.
  3. This implementation will still require a FeatureFlags module to be available that implements is_on?/2. Adjust as needed to accomodate your own feature flag setup.
@LostKobrakai
LostKobrakai / form_live.ex
Last active April 12, 2024 18:56
Phoenix LiveView form with nested embeds and add/delete buttons
defmodule NestedWeb.FormLive do
use NestedWeb, :live_view
require Logger
defmodule Form do
use Ecto.Schema
import Ecto.Changeset
embedded_schema do
field :name, :string
@mrdotb
mrdotb / ex
Last active March 7, 2024 21:46
Phoenix hosts management
defmodule AppWeb.Host do
@moduledoc """
Conveniences for working with host.
"""
def root do
Keyword.get(get_config(), :root)
end
def root_uri do
Keyword.get(get_config(), :root_uri)
@angelikatyborska
angelikatyborska / gettext.ex
Last active March 11, 2024 13:50
A custom `gettext_with_link` macro for easily putting inline links into gettext strings
# Has one external dependency except for Gettext: https://github.com/rrrene/html_sanitize_ex
defmodule MyApp.Gettext do
@doc """
A helper for translations with links.
Pass in the translation string which must include
`%{link_start}`/`%{link_end}`. For multiple URLs, use
`%{link_start_<0,1,2...>}`.
@PJUllrich
PJUllrich / big-o.md
Last active April 28, 2024 14:19
Big-O Time Complexities for Elixir Data Structures

Big-O Time Complexities for Elixir data structures

Map [1]

Operation Time Complexity
Access O(log n)
Search O(log n)
Insertion O(n) for <= 32 elements, O(log n) for > 32 elements [2]
Deletion O(n) for <= 32 elements, O(log n) for > 32 elements
@basnijholt
basnijholt / cto_line.pine
Created September 18, 2021 08:23
CTO Line indicator for TradingView
//@version=4
study(title="CTO Line", shorttitle="CTO", overlay=true, resolution="")
smma(src, length) =>
smma = 0.0
smma := na(smma[1]) ? sma(src, length) : (smma[1] * (length - 1) + src) / length
smma
v1 = smma(hl2, 15)
m1 = smma(hl2, 19)
m2 = smma(hl2, 25)
v2 = smma(hl2, 29)
@alex-min
alex-min / InfiniteScroll.js
Created January 1, 2021 14:24
Infinite Scrool hook for phoenix Live View and fattable.js
require('fattable/fattable.js');
/*
* This variable is used to keep the scroll position when the live view navigation changes.
* This is useful for modals.
*/
let keepScroll = {};
export default {
@goofansu
goofansu / app.js
Last active August 5, 2023 04:05
LiveView upload directly to AWS China S3
let Uploaders = {}
Uploaders.S3 = function (entries, onViewError) {
entries.forEach(entry => {
let xhr = new XMLHttpRequest()
onViewError(() => xhr.abort())
xhr.onload = () => (xhr.status === 200 ? entry.done() : entry.error())
xhr.onerror = () => entry.error()
xhr.upload.addEventListener("progress", event => {
if (event.lengthComputable) {
@doc """
Asserts that the given changeset is invalid, and that when the
assertion_expression is applied to the error_message it results in a truthy
value.
"""
defmacro assert_invalid(changeset, field, assertion_expression) when is_atom(field) do
expr = Macro.to_string(assertion_expression)
quote do
c = unquote(changeset)