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 / 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 / user.rb
Created November 23, 2016 20:53
Rolify: Remove all roles for a resource and enforce only one role per resource (singleton pattern)
class User < ApplicationRecord
rolify :strict => true, :before_add => :before_add_role
#Helper method to remove any existing role this user has for a resource
def remove_all_roles resource
# README: This syntax relies on changes on the following PR
# https://github.com/RolifyCommunity/rolify/pull/427
# Or include the source of this directly:
# gem 'rolify', :git => "git://github.com/Genkilabs/rolify.git"
remove_role nil, resource
@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