Skip to content

Instantly share code, notes, and snippets.

@goromlagche
goromlagche / pagination.rb
Last active December 8, 2023 13:01
Cursor pagination
# frozen_string_literal: true
# drop this file in app/lib/pagination.rb
module Pagination
DEFAULT_PAGE_LIMIT = 10
def pages(records:, url:)
paginate = Paginate.new(records: records,
url: url,
limit: limit,
@dhh
dhh / Gemfile
Created June 24, 2020 22:23
HEY's Gemfile
ruby '2.7.1'
gem 'rails', github: 'rails/rails'
gem 'tzinfo-data', '>= 1.2016.7' # Don't rely on OSX/Linux timezone data
# Action Text
gem 'actiontext', github: 'basecamp/actiontext', ref: 'okra'
gem 'okra', github: 'basecamp/okra'
# Drivers
@DavidKuennen
DavidKuennen / minimal-analytics-snippet.js
Last active June 9, 2024 19:08
Minimal Analytics Snippet
(function (context, trackingId, options) {
const history = context.history;
const doc = document;
const nav = navigator || {};
const storage = localStorage;
const encode = encodeURIComponent;
const pushState = history.pushState;
const typeException = 'exception';
const generateId = () => Math.random().toString(36);
const getId = () => {
@coreyhaines
coreyhaines / Editable.elm
Last active August 25, 2022 05:11
type Editable
module Editable exposing (..)
type Editable ofType
= NotEditing { value : ofType }
| Editing { originalValue : ofType, buffer : ofType }
value : Editable ofType -> ofType
value editable =
@danielberkompas
danielberkompas / scheduler.ex
Created October 26, 2016 17:59
A simple mix task scheduler for Elixir apps
defmodule MyApp.Scheduler do
@moduledoc """
Schedules a Mix task to be run at a given interval in milliseconds.
## Options
- `:task`: The name of the Mix task to run.
- `:args`: A list of arguments to pass to the Mix task's `run/1` function.
- `:interval`: The time interval in millisconds to rerun the task.
@sergio1990
sergio1990 / ecto_helper.ex
Last active February 27, 2020 15:20
Prettify Ecto errors
defmodule EctoHelper do
@moduledoc """
Provides helper functions
"""
@doc """
Prettifies changeset error messages.
By default `changeset.errors` returns errors as keyword list, where key is name of the field
and value is part of message. For example, `[body: "is required"]`.
This method transforms errors in list which is ready to pass it, for example, in response of
@mike-north
mike-north / user.ex
Created May 28, 2016 04:58
ELIXIR - Validating a password for length and complexity in Ecto
defmodule MyApp.User do
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
|> validate_format(:email, ~r/@/)
|> validate_length(:password, min: 8)
|> validate_format(:password, ~r/[0-9]+/, message: "Password must contain a number") # has a number
|> validate_format(:password, ~r/[A-Z]+/, message: "Password must contain an upper-case letter") # has an upper case letter
|> validate_format(:password, ~r/[a-z]+/, message: "Password must contain a lower-case letter") # has a lower case letter
|> validate_format(:password, ~r/[#\!\?&@\$%^&*\(\)]+/, message: "Password must contain a symbol") # Has a symbol
@sorentwo
sorentwo / email_view.ex
Created March 7, 2016 16:09
Elixir/Phoenix markdown email templates
defmodule MyApp.EmailView do
use MyApp.Web, :view
def render(template, format, %{assigns: assigns}) do
rendered = render_to_string(__MODULE__, template, assigns)
case format do
:text -> rendered
:html -> Earmark.to_html(rendered)
end
@manigandham
manigandham / rich-text-html-editors.md
Last active June 10, 2024 15:49
Rich text / HTML editors and frameworks

Strictly Frameworks

Abstracted Editors

These use separate document structures instead of HTML, some are more modular libraries than full editors

@wesbos
wesbos / you-are-welcome.sh
Last active March 28, 2017 13:36
Listen to all the voices inside your Mac computer
# paste this into your console and hit enter
for voice in Agnes Kathy Princess Vicki Victoria Alex Bruce Fred Junior Ralph Albert Bad News Bahh Bells Boing Bubbles Cellos Deranged Good News Hysterical Pipe Organ Trinoids Whisper Zarvox; do say "Hi `whoami` my name is ${voice}" -v ${voice}; done