Skip to content

Instantly share code, notes, and snippets.

@julianrubisch
julianrubisch / Gemfile
Last active September 13, 2019 16:39
Hooking up Dropzone.js with ActiveStorage DirectUpload via Stimulus.js
source 'https://rails-assets.org' do
gem 'rails-assets-dropzone'
end
@julianrubisch
julianrubisch / _inline_text_edit.html.slim
Last active September 8, 2018 08:58
DRY up an Inline-Text-Edit component with Partials, Capture, and a StimulusJS controller
- downcase_modelname = model.class.name.downcase
- url = Rails.application.routes.url_helpers.send("#{downcase_modelname}_path", model, format: :json)
div[class="#{wrapper_class}" data-controller="inline-edit" data-inline-edit-url="#{url}" data-inline-edit-model="#{downcase_modelname}" data-inline-edit-model-attribute="#{attribute}" data-inline-edit-model-id="#{model.id}"]
span[data-target="inline-edit.originalText" id="#{attribute}-original"]
span = capture(attribute, &block)
a.inline-edit-edit-button[data-action="inline-edit#handleShow"]
i.fas.fa-edit
= simple_fields_for model do |f|
.ui.form.mini.hidden[data-target="inline-edit.form" id="#{attribute}-form"]
@julianrubisch
julianrubisch / categories_controller.rb
Last active January 22, 2024 06:44
Dependent Select with StimulusJS
class CategoriesController < ApplicationController
def fields
render json: Field.where(category_id: params[:id]).order(:id)
end
end
@julianrubisch
julianrubisch / _active_storage_preview.html.slim
Last active January 1, 2019 13:20
ActiveStorage NullAttachment
- if file.blob.image?
= image_tag file.variant(resize: "512x512"), class: 'img-thumbnail w-25 float-left mr-3'
- elsif file.blob.content_type =~ /pdf/
= image_tag file.preview(resize: "512x512"), class: 'img-thumbnail w-25 float-left mr-3'
@julianrubisch
julianrubisch / active_storage_previewable.rb
Created January 13, 2019 18:09
Previewable, first try
module ActiveStoragePreviewable
extend ActiveSupport::Concern
included do
attachments = reflect_on_all_associations.select do |assoc|
assoc.klass == ActiveStorage::Attachment
end
attachments.each do |att|
prefix = (/(.*)_attachment\Z/.match att.name.to_s)[1]
# app/lib/easymon/action_cable_connection_statistics.rb
module Easymon
class ActionCableConnectionStatistics
def check
count = ActionCable.server.open_connections_statistics.count
status = count < 100
[status, "#{status ? "low" : "high"} - #{count}"]
rescue
@julianrubisch
julianrubisch / middleware.v1.js
Last active January 2, 2020 16:43
Express File Cache Middleware
// const cachedAssetPath = ...
try {
if (fs.existsSync(cachedAssetPath)) {
const response = await fetch(res.locals.fetchUrl, { method: "HEAD" });
res.locals.contentLength = response.headers.get("content-length");
res.locals.contentType = response.headers.get("content-type");
res.locals.buffer = fs.readFileSync(cachedAssetPath);
} else {
const blob = await (await fetch(res.locals.fetchUrl)).blob();
@julianrubisch
julianrubisch / benchmark_view.rb
Created December 16, 2019 08:53
benchmarking partials
require "benchmark-malloc"
require "slim"
require "action_view"
class BenchmarkView < ActionView::Base
def benchmark
bench_malloc = Benchmark::Malloc.new
stats = bench_malloc.run {
@julianrubisch
julianrubisch / webpack.config.js
Last active January 11, 2020 07:15
workbox webpack runtime cache
new WorkboxWebpackPlugin.GenerateSW({
clientsClaim: true,
exclude: [/\.map$/, /asset-manifest\.json$/],
importWorkboxFrom: "cdn",
runtimeCaching: [
{
urlPattern: new RegExp("https://images\.ctfassets\.net"),
handler: "cacheFirst"
},
{
@julianrubisch
julianrubisch / middleware.v1.js
Created January 4, 2020 06:52
middleware v1
// const cachedAssetPath = ...
try {
if (fs.existsSync(cachedAssetPath)) {
const response = await fetch(res.locals.fetchUrl, { method: "HEAD" });
res.locals.contentLength = response.headers.get("content-length");
res.locals.contentType = response.headers.get("content-type");
res.locals.buffer = fs.readFileSync(cachedAssetPath);
} else {
const blob = await (await fetch(res.locals.fetchUrl)).blob();