Skip to content

Instantly share code, notes, and snippets.

View joelmoss's full-sized avatar

Joel Moss joelmoss

View GitHub Profile
@ryanb
ryanb / application.html.erb
Created April 20, 2024 05:21
Simple GetText solution for JavaScript for use with translation.io
<%# app/views/layouts/application.html.erb %>
...
<%= javascript_include_tag "locales/#{I18n.locale}", nonce: true %>
@hopsoft
hopsoft / build_insert_query.rb
Created March 15, 2024 18:00
Get the SQL for an insert statement from ActiveRecord
# Builds an SQL insert query for a given record
#
# @param record [ActiveRecord::Base] Record used to build the SQL insert query
# @return [String] SQL insert query
def build_insert_query(record)
columns = record.class.columns.reject { |col| col.name == record.class.primary_key }
values = columns.map { |col| record[col.name] }
insert_manager = Arel::InsertManager.new
insert_manager.into(record.class.arel_table)
insert_manager.insert(columns.zip(values)).to_sql
@bradgessler
bradgessler / phlex_element.rb
Last active January 3, 2024 22:21
The Phlex::Element class makes creating simple Phlex components easier
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "phlex"
end
require "phlex/testing/view_helper"
include Phlex::Testing::ViewHelper
@marvinhagemeister
marvinhagemeister / bind-plugin.ts
Last active January 9, 2024 20:31
Preact Signals `bind:value`
import { options } from "preact";
import { Signal } from "@preact/signals";
// Add `bind:value` to JSX types
declare global {
namespace preact.createElement.JSX {
interface HTMLAttributes {
"bind:value"?: Signal<string | string[] | number | undefined>;
}
}
@flickgradley
flickgradley / data_diff.rb
Created August 6, 2023 03:58
#diff method for Ruby Data classes
Measure = Data.define(:amount, :unit) do
def diff(other)
raise ArgumentError.new("other must be same class") unless other.is_a?(self.class)
members.inject({}) do |memo, key|
thisVal, otherVal = send(key), other.send(key)
memo[key] = [thisVal, otherVal] unless thisVal == otherVal
memo
end
@joeldrapper
joeldrapper / packer.rb
Last active July 12, 2023 12:58
Paquito Job Packer
# frozen_string_literal: true
module Packer
# A special packer class for globally identifiable objects. It takes a global ID and packs it as a String, then rehydrates it as a GlobalID lookup.
class GloballyIdentifiable
def self.from_pack(uri)
GlobalID::Locator.locate(uri)
end
def initialize(identifiable)
// Turn all HTML <a> elements into client side router links, no special framework-specific <Link> component necessary!
// Example using the Next.js App Router.
import { useRouter } from 'next/navigation';
import { useEffect } from 'react';
function useLinkHandler() {
let router = useRouter();
useEffect(() => {
let onClick = e => {
@kaspth
kaspth / object_proxy.rb
Created June 10, 2023 14:42
Making Ruby better at object proxying, so we don't need to add `user_name` etc. for Law of Demeter.
class Object::Proxy < SimpleDelegator
def initialize(object, **values)
super(object)
@values = values
end
def method_missing(name, ...)
@values.fetch(name) { super }
end
end
@okuramasafumi
okuramasafumi / main.rb
Created May 30, 2023 08:32
RSpec documentation without executing
require 'syntax_tree'
class RSpecEnvironment
def initialize
@contexts = []
@results = []
end
def klass(k)
@contexts << k
@skatkov
skatkov / test.rb
Last active January 3, 2024 18:20
speed up testsuit by using unlogged tables in PG
# config/environments/test.rb
ActiveSupport.on_load(:active_record_postgresqladapter) do
# For this optimization to work, you need to recreate your test database
self.create_unlogged_tables = true
end