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 / 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
@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 / 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 / any_controller.rb
Last active August 31, 2020 14:45
Rails 4 Ransacker to build subquery for searching across multiple fields in multiple polymorphic classes.
#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
@profile_instance.search( { :find_term_in => "has fleas", :other_ransack_term_eq => "foobar" } ).results()
@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 / application_controller.rb
Created July 11, 2018 17:52
How to dynamically select rails views for specific model instances (ie. a specific user)
#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
Genkilabs / inflections.rb
Created June 29, 2017 17:40
How to use Devise inside of a Versionist scope without the version-name in the model's path prefix
#/config/initializers/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'
end
@Genkilabs
Genkilabs / autocomplete.js.coffee
Created February 20, 2012 19:39
Pass additional parameters into jQueryUI autocomplete widget in rails 3.1 coffeescript
#turn on our autocompletes if there are any
jQuery ->
if $('#user_autocomplete').length
index = 1
$('#user_autocomplete').autocomplete
source: (request, response) ->
#add our custom index parameter to the call
request["index"] = index++
#make our request and handle the response as normal
$.ajax(
@Genkilabs
Genkilabs / application.js
Last active November 18, 2017 11:16
Bootstrap 3 modals in Rails 4 confirms using bootstrap3-dialog JS plugin
//= require jquery
//= require jquery_ujs
//= require_tree .
// ^ I am assuming that bootstrap3-dialog is in the tree
//GLOBAL JQuery Functaionality
$(function(){
/*** CONFIRM MODAL OVERRIDE ***/
//override the use of js alert on confirms
@Genkilabs
Genkilabs / autosavable.js
Last active January 3, 2016 03:49
Debounced Autosave for EmberJS using 1.2.0-beta.4 and EmberData 1.0.0-beta.4+canary.3993001d with hooks for pacifier from http://ricostacruz.com/nprogress
// Debounced autosave for Ember.js
// Original code by Mitch Lloyd http://gaslight.co/blog/?author=mitchlloyd
// updated from http://gaslight.co/blog/an-autosave-pattern-for-ember-and-ember-data
// repo at https://github.com/gaslight/ember-autosaving
// Changed to work with latest Ember Data as of 2013-12-16 by Genkilabs
// Includes hooks for pacifier from http://ricostacruz.com/nprogress
// NOTE: This requires a 2 part install in which the controller and any models it loads have the corresponding mixin
// This is how long we will wait on a form before saving. I like to put this in App.AUTOSAVE_DELAY
var AUTOSAVE_DELAY = 1500