Skip to content

Instantly share code, notes, and snippets.

View nicolasblanco's full-sized avatar
🎯
Focusing

Nicolas Blanco nicolasblanco

🎯
Focusing
View GitHub Profile
defmodule MyTwitterWeb.CounterLive do
use MyTwitterWeb, :live_view
def render(assigns) do
~H"""
<div>
<.button phx-click="dec">-</.button>
<%= @counter %>
<.button phx-click="inc">+</.button>
</div>
@nicolasblanco
nicolasblanco / pagination.ex
Created August 21, 2022 16:38
Pagination library using Ecto
defmodule Pagination do
import Ecto.Query
alias Snippets.Repo
#
# ## Example
#
# Snippets.Snippet
# |> order_by(desc: :inserted_at)
# |> Pagination.page(0, per_page: 10)
#
@nicolasblanco
nicolasblanco / many_to_many_helper.ex
Created May 3, 2021 18:47
many_to_many_helper.ex
defmodule ManyToManyHelper do
defmacro __using__(_opts) do
quote do
import Ecto.Query, only: [from: 2]
import unquote(__MODULE__)
end
end
defmacro many_to_many_params(opts) do
association = Keyword.fetch!(opts, :association)
defmodule MyApp do
def split_and_reverse(string) do
string
|> String.split()
|> Enum.reverse()
end
end
"This is a test"
|> MyApp.split_and_reverse()
@nicolasblanco
nicolasblanco / app.js
Last active September 18, 2023 19:50
Stripe.js elements integration inside a Elixir LiveView (Bootstrap styles)
// Code handling the card payment
const payWithCard = function (stripe, card, clientSecret, liveView) {
stripe
.confirmCardPayment(clientSecret, {
payment_method: {
card: card,
},
})
.then(function (result) {
if (result.error) {
@nicolasblanco
nicolasblanco / gist:d1549bf96616eb22b87c7919a085035b
Created May 31, 2019 16:16
Phoenix app crashing multiple times in loop in dev env with each compilation
[info] GET /dashboard/tos_acceptance/edit
[debug] QUERY OK source="users" db=0.4ms
SELECT u0."id", u0."first_name", u0."last_name", u0."address", u0."city", u0."country", u0."nationality", u0."email", u0."company_name", u0."zip_code", u0."region_code", u0."phone_number", u0."date_of_birth", u0."description", u0."facebook_uid", u0."google_uid", u0."coordinates", u0."lat", u0."lng", u0."images_group_url", u0."banner_image_url", u0."retrieval_information", u0."encrypted_password", u0."role", u0."reset_password_token", u0."reset_password_sent_at", u0."general_tos_accepted_at", u0."producer_tos_accepted_at", u0."mangopay_user_id", u0."slug", u0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (u0."id" = $1) [2]
[debug] Processing with DokkitoWeb.Dashboard.TosAcceptanceController.edit/2
Parameters: %{}
Pipelines: [:browser, :dashboard]
[info] Sent 200 in 1ms
[info] Replied phoenix:live_reload :ok
[error] #PID<0.2434.0> running DokkitoWeb.Endpoint (connection #PID<0.2433.0>, stream id 1) terminated
Server
with {:ok, result} <- do_some_heavy_stuff,
{:ok, second_result} <- do_other_stuff(result) do
send_email(second_result)
else
error -> # ...
end
if result = do_some_heavy_stuff
if second_result = do_other_stuff(result)
send_email(second_result)
else
# ...
end
else
# ...
end
defmodule EctoAutoslugField.ForceSlugGeneration do
alias EctoAutoslugField.SlugBase
defmacro __using__(_opts) do
quote do
def force_generate_slug(changeset) do
opts = [
from: @from,
to: @to,
always_change: true,
defmodule DokkitoWeb.AuthController do
use DokkitoWeb, :controller
# ...
def callback(%{assigns: %{ueberauth_auth: auth}} = conn, _params) do
case Auth.find_or_create(auth) do
{:ok, user} ->
conn
|> put_flash(:info, "Successfully authenticated.")