Skip to content

Instantly share code, notes, and snippets.

@julianrubisch
julianrubisch / middleware.v2.js
Created January 4, 2020 06:54
middleware v2
function encodeAssetCacheName(contentType, contentLength) {
return Buffer.from(`${contentType}:${contentLength}`).toString("base64");
}
function decodeAssetCacheName(encodedString) {
const decodedFileName = Buffer.from(encodedString, "base64").toString(
"ascii"
);
return decodedFileName.split(":");
}
@julianrubisch
julianrubisch / makeAssetCachePath.v1.js
Created January 4, 2020 07:19
makeAssetCachePath v1
/* from https://github.com/segment-boneyard/hash-mod/blob/master/lib/index.js */
function integerHash(string) {
return (string + "").split("").reduce((memo, item) => {
return (memo * 31 * item.charCodeAt(0)) % 982451653;
}, 7);
}
function makeAssetCachePath(cacheDir, cacheKey) {
const hash = crypto
.createHash("sha256")
@julianrubisch
julianrubisch / assets.js
Created January 6, 2020 17:41
express js asset proxy route
router.get(
"/:asset_type/:space_id/:asset_id/:token/:name",
async (req, res, next) => {
const format = req.query.fm;
const width = req.query.w;
const { asset_type, space_id, asset_id, token, name } = req.params;
res.locals.fetchUrl = `https://${asset_type}.ctfassets.net/${space_id}/${asset_id}/${token}/${name}?fm=${format}${
width ? `&w=${width}` : ""
@julianrubisch
julianrubisch / middleware.v3.js
Last active January 9, 2020 15:15
express js middleware LRU
function evictLeastRecentlyUsed(cacheDir, maxSize, logger) {
getFolderSize(cacheDir, (err, size) => {
if (err) {
throw err;
}
if (size >= maxSize) {
try {
// find least recently used file
const leastRecentlyUsed = findLeastRecentlyUsed(cacheDir);
require 'fileutils'
namespace :solargraph do
task update_model_definitions: :environment do
if Rails.env.development?
Rails.application.eager_load! unless Rails.application.config.cache_classes
Zeitwerk::Loader.eager_load_all if defined?(Zeitwerk)
ApplicationRecord.descendants.each do |model|
def_file = Rails.root.join('config', 'model_definitions', "#{model.name.underscore}.rb")
@julianrubisch
julianrubisch / README.md
Last active July 5, 2022 09:18
Futurism

There's one thing that I felt was missing from the CableReady ecosystem and should be doable: lazy loading

I introduce:

Futurism

with a helper in your template

<%= futurize @posts %>
@julianrubisch
julianrubisch / .gitlab-ci.yml
Last active April 6, 2023 20:13
Gitlab CI Config for Minitest/system tests
image: "ruby:2.7"
services:
- postgres
- redis:latest
variables:
RAILS_ENV: test
POSTGRES_DB: my_app_test
POSTGRES_USER: postgres
POSTGRES_PASSWORD: "password"
@julianrubisch
julianrubisch / articles_controller.rb
Last active October 4, 2020 02:06
StimulusReflex Concern-Like Architecture
class ArticlesController < ApplicationController
def index
@articles = Article.where(session[:articles][:param] => session[:articles][:value])
end
end
@julianrubisch
julianrubisch / _article.html.erb
Last active November 9, 2020 14:34
StimulusReflex Selector Morph
<%= article.title # ... etc %>
@julianrubisch
julianrubisch / form.html.erb
Last active July 5, 2022 09:18
StimulusReflex Form Reset Controller
<%= form_with(model: model, data: {controller: "reflex-form", reflex_form_reflex: "ExampleReflex#submit"}) do |form| %>
<%= form.button data: {action: "click->reflex-form#submit"} %>
<% end %>