Skip to content

Instantly share code, notes, and snippets.

View f-mer's full-sized avatar

Fabian Mersch f-mer

View GitHub Profile
@fractaledmind
fractaledmind / artifact_interface_concern.rb
Created September 9, 2022 17:27
Ruby on Rails model interfaces
# frozen_string_literal: true
module ArtifactInterface
extend ActiveSupport::Concern
extend Interfaceable
def key
raise NotImplementedError
end
@dixpac
dixpac / current_helpers.js
Last active January 25, 2023 12:12
Helpers esbuild alias
// helpers/current_helpers.js
// On-demand JavaScript objects from "current" HTML <meta> elements. Example:
//
// <meta name="current-account-id" content="123">
// <meta name="current-account-subdomain" content="Wizard Lab">
//
// >> current.account
// => { id: "123", subdomain: "Wizard Lab" }
//
# Rails production setup via SQLite3 made durable by https://litestream.io/
# Copy this to Dockerfile on a fresh rails app. Deploy to fly.io or any other container engine.
#
# try locally: docker build . -t rails && docker run -p3000:3000 -it rails
#
# in production you might want to map /data to somewhere on the host,
# but you don't have to!
#
FROM ruby:3.0.2
@palkan
palkan / normalize.rb
Created December 24, 2021 14:47
ActiveModel .normalize
# frozen_string_literal: true
require "bundler/inline"
gemfile(true, quiet: true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "activesupport", "~> 7.0.0", require: false
@fractaledmind
fractaledmind / snapshotable.rb
Last active October 7, 2022 08:30
ActiveRecord Concern to mark a slice of a model's association tree as freezable, to freeze it at some point, and then to work with frozen versions of those objects moving forward
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
@steveruizok
steveruizok / getPerfectDashProps.ts
Last active February 19, 2024 00:38
Get balanced stroke dash array and stroke dash offset for an ellipse.
/**
* Get balanced dash-strokearray and dash-strokeoffset properties for a path of a given length.
* @param length The length of the path.
* @param strokeWidth The shape's stroke-width property.
* @param style The stroke's style: "dashed" or "dotted" (default "dashed").
* @param snap An interval for dashes (e.g. 4 will produce arrays with 4, 8, 16, etc dashes).
*/
export function getPerfectDashProps(
length: number,
@dhh
dhh / pagination_controller.js
Last active April 24, 2024 10:53
HEY's Stimulus Pagination Controller
/*
ERB template chunk from The Feed's display of emails:
<section class="postings postings--feed-style" id="postings"
data-controller="pagination" data-pagination-root-margin-value="40px">
<%= render partial: "postings/snippet", collection: @page.records, as: :posting, cached: true %>
<%= link_to(spinner_tag, url_for(page: @page.next_param),
class: "pagination-link", data: { pagination_target: "nextPageLink", preload: @page.first? }) unless @page.last? %>
</section>
# ----------------------------- Gem -----------------------------
module PricingEngine
module PricingRepositoryInterface
extend T::Sig
extend T::Helpers
interface!
sig { abstract.params(ids: T::Array[Integer]).returns(T::Array[Schema::Variant]) }
def variants_by_ids(ids);end
@rainerborene
rainerborene / dom.js
Last active February 17, 2022 14:54
rails-ujs is a past thing. Here is how you can accomplish almost the same behaviour with Stimulus and Hotwire.
export function reduceToParams(elements) {
return elements.reduce((hash, node) => Object.assign(hash, { [node.name]: node.value }), {})
}
export function stopEverything(e) {
e.preventDefault()
e.stopPropagation()
e.stopImmediatePropagation()
}
@mdchaney
mdchaney / README
Last active February 6, 2024 22:20
Fix string encoding in Ruby
This function takes a string that is in Latin-1 (ISO-8859-1), UTF-8, or plain ASCII and returns the string properly encoded as UTF-8. In addition, any Microsoft smart quotes (stupid quotes) are replaced with plain ASCII equivalents. This is useful for reading CSV files or other text files from a web upload and getting to a "known good" state.