Skip to content

Instantly share code, notes, and snippets.

Writing software...

Alex Genkilabs

Writing software...
  • Adept Mobile
  • Colorado
View GitHub Profile
Genkilabs / any_es6_module.js
Created Feb 18, 2021
Workaround for running pdf.js on Rails 6.1 Ruby 3.0 with webpacker
View 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 / application.js
Created Jun 15, 2020
JS Pattern for Organizing Selectors and Function Reuse After Load
View application.js
// 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 / application_controller.rb
Created Jul 11, 2018
How to dynamically select rails views for specific model instances (ie. a specific user)
View application_controller.rb
#An overly complex example of dynamically micro-managing which templates are shown..
class ApplicationController < ActionController::Base
# In this example we assume a using Devise so user_signed_in? and current_user are provided.
# Note: the value returned here could very well be a user's uuid if you needed to provide
# one specific different view for a single or small collection of specific users.
# @returns String || nil
def get_custom_view_name
#This doesn't need to be lazy, but it can be.
@custom_view_name ||= begin
Genkilabs / inflections.rb
Created Jun 29, 2017
How to use Devise inside of a Versionist scope without the version-name in the model's path prefix
View inflections.rb
# This is a nice-to-have so API doesn't keep showing up as Api
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'API'
Genkilabs / user.rb
Created Nov 23, 2016
Rolify: Remove all roles for a resource and enforce only one role per resource (singleton pattern)
View user.rb
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
# Or include the source of this directly:
# gem 'rolify', :git => "git://"
remove_role nil, resource
Genkilabs / to_bool.rb
Created Nov 23, 2015
Add a 'to_bool' method to every Ruby / Rails class with initializer
View to_bool.rb
#Alias the default boolean? operator of ruby for everything so all objects responds_to? :to_bool
#...then further refine it for specific classes later
class Object
def to_bool
return !!self
#Lets give String a custom to_bool that is in line with the meaning for our specific program
class String
Genkilabs /
Last active Aug 29, 2015
Localhost developing in Coalesce for Ember CLI based apps
cd ../../coalesce;
npm run-script build;
cd ../coalesce-ember;
# Hack to use the latest version of Coalesce
cp ../coalesce/dist/* bower_components/coalesce/
npm run-script build;
cd ../ember-cli-coalesce-todos/client;
mkdir -p vendor/coalesce-ember
Genkilabs / simple-auth-config.js
Created Aug 25, 2014
ember-cli-simple-auth-devise initializer for setting endpoint. Lets you run on Rails localhost without proxy.
View simple-auth-config.js
// app/initializers/simple-auth-config.js
export default {
name: 'simple-auth-config',
before: 'simple-auth',
initialize: function() {
var tokenEndpoint = '/users/sign_in';
MyAppnameENV['simple-auth'] = {
authorizer: 'simple-auth-authorizer:devise',
Genkilabs / any_controller.rb
Last active Aug 31, 2020
Rails 4 Ransacker to build subquery for searching across multiple fields in multiple polymorphic classes.
View any_controller.rb
#This is how it might be used. Create your instance which could be polymorphic or just a class.
#Search using the predicate 'in' because the subquery find_term will return an ID of every record that matches.
@profile_instance = either a PetProfile or an OwnerProfile { :find_term_in => "has fleas", :other_ransack_term_eq => "foobar" } ).results()
Genkilabs / application.js
Last active Nov 18, 2017
Bootstrap 3 modals in Rails 4 confirms using bootstrap3-dialog JS plugin
View application.js
//= require jquery
//= require jquery_ujs
//= require_tree .
// ^ I am assuming that bootstrap3-dialog is in the tree
//GLOBAL JQuery Functaionality
//override the use of js alert on confirms