Skip to content

Instantly share code, notes, and snippets.

View Genkilabs's full-sized avatar
😸
Writing software...

Alex Genkilabs

😸
Writing software...
  • Adept Mobile
  • Colorado
View GitHub Profile
@Genkilabs
Genkilabs / application.js
Created June 15, 2020 17:48
JS Pattern for Organizing Selectors and Function Reuse After Load
// Your main application js. We will include jQuery first, then everything else
//= require jquery
//
//= require_tree .
// You can choose to DRY common selectors perhaps used system wide.
const SELECTORS = {
RESULTS: () => $("#results-box")
}
@Genkilabs
Genkilabs / any_es6_module.js
Created February 18, 2021 22:39
Workaround for running pdf.js on Rails 6.1 Ruby 3.0 with webpacker
//app/assets/js/any_es6_module.js
//Use globally in any es6 module you are requiring through application.js
var render_pdf_thumbnail = function(pdfData, $canvas) {
//This is the sauce. We instantiate a new Worker from our globally scoped worker-loader f()
pdfjsLib.GlobalWorkerOptions.workerPort = new pdfjsWorker();
// In my case, using DocumentInitParameters object to load binary data.
var loadingTask = pdfjsLib.getDocument({data: pdfData});
loadingTask.promise.then(function(doc) {
@Genkilabs
Genkilabs / custom_utils.rb
Created October 5, 2023 16:00
Interpolate Sting Using Hash or Rails Model
module CustomUtils
# This should look for hash keys, or ActiveModel::Base attributes within the string
# while also safeguarding from typing errors.
# Interpolation symbols not provided in the hash should be left unchanged.
# This does NOT support "deep interpolation" eg. "my %{%{nested_key}} string"
#@param String with %{key} interpolation fields
#@param Hash with symbolic keys OR Object with 'attributes' method eg. ActiveModel
#@returns String original with any keys replaced if they are in the hash, others ignored
def self.interpolate str, obj
#For the hash, we need to re-key the hash to be sure, and add default to skip missing interpolations