Skip to content

Instantly share code, notes, and snippets.

View joelmoss's full-sized avatar

Joel Moss joelmoss

View GitHub Profile
@DavidWells
DavidWells / run-tests-for-file.bash
Created February 20, 2025 18:41
Bash for VSCode* to run tests in current file. E.g. Inside file.js will run file.test.js, inside file.test.js it runs itself
#!/bin/bash
if [[ "${relativeFile}" == *.test.js ]]; then
FILE="${relativeFile}"
elif [ -f "${fileDirname}/${fileBasenameNoExtension}.test.js" ]; then
FILE="${fileDirname}/${fileBasenameNoExtension}.test.js"
elif [ -f "${fileDirname}/$(basename "${fileDirname}").test.js" ]; then
FILE="${fileDirname}/$(basename "${fileDirname}").test.js"
else
FILE="${relativeFile}"
# frozen_string_literal: true
# For now, run like this: `ruby --rjit --rjit-disable fjit.rb`
#
# Once RJIT is removed, the extra flags will not be necessary
require "fiddle"
require "ffi"
require "jit_buffer"
require "hacks"
# config/initializers/phlex_template_handler.rb
require "action_view"
require "phlex"
# Intercept unknown "capitalized" method calls (e.g., PageView(...)) in templates,
# look up a Phlex component class, instantiate it, and render it.
# Crucially, we re-bind the user’s block so that `self` is the component, not the ActionView context.
module PhlexDynamicMethodCalls
def method_missing(name, *args, **kwargs, &block)
# Only intercept method calls starting with an uppercase letter (e.g. "PageView", "MyComponent", etc.)
@coolprobn
coolprobn / date_in_preferred_time_zone.rb
Created June 19, 2024 15:18
Convert any DateTime to the timezone you prefer while still keeping the same date and time - Ruby on Rails
def date_in_danish_time(date_time)
date_array =
date_time.strftime("%Y %m %d").split.map { |string| Integer(string, 0) }
danish_date = Date.new(*date_array).in_time_zone("Copenhagen")
# extracting offset from the date will help us in also taking care of Daylight Saving
danish_date_offset = danish_date.formatted_offset
date_time_array =
date_time
.strftime("%Y %m %d %H %M %S")
@kaspth
kaspth / concern.rb
Created June 4, 2024 20:37
Writing Concerns without the `extend`/`included`/`class_methods` boilerplate.
# Maybe there's a way to support ActiveSupport::Concern's dependencies too?
# Although this doesn't use the module hierarchy, so `prepend` can't work.
class Concern < Module
def initialize(&block) = @block = block
def included(klass) = klass.class_eval(&@block)
end
module Kernel
def Concern(...) = Concern.new(...)
end
@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
@palkan
palkan / README.md
Last active January 3, 2025 14:22
ActionCable over SSE

Action Cable over SSE

This is a demo of leveraging the proposed Action Cable architecture to implement an SSE transport for Action Cable (without changing the user-space code, e.g., Connection and Channel classes).

Start the server by running the following command:

 ruby main.rb

Now, you can connect to Action Cable over SSE via cURL as follows:

@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 December 16, 2024 14:38
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>;
}
}