Skip to content

Instantly share code, notes, and snippets.

Avatar

Tim Lowrimore tlowrimore

View GitHub Profile
@tlowrimore
tlowrimore / jankylog.js
Created Sep 9, 2020
A janky-ass console logger for debugging web apps in iOS Safari, when you don't just have a Mac lying around for accessing a debug console.
View jankylog.js
function jankylog(msg) {
let konsole = document.getElementById('jankylog');
if(!konsole) {
konsole = document.createElement('pre');
konsole.id = 'jankylog';
konsole.style.backgroundColor = 'white';
konsole.style.padding = '1rem';
document.body.append(konsole);
@tlowrimore
tlowrimore / set_operations.rb
Created Mar 10, 2016
Extends the functionality set forth in union_scope.rb, to include EXCEPT and INTERSECT operations
View set_operations.rb
module ActiveRecord
module Scopes
module SetOperations
extend ActiveSupport::Concern
class_methods do
def union_scope(*scopes)
apply_operation 'UNION', scopes
end
@tlowrimore
tlowrimore / address_books_controller.rb
Last active Sep 22, 2021
Keeps your API lookin' good! No need for all that nested_attributes pollution in your request/response payloads
View address_books_controller.rb
class V1::AddressBooksController < V1::BaseController
def create
@address_book = AddressBook.new address_book_params
unless @address_book.save
errors = @address_book.errors.to_hash(true)
render status: 422, json: { errors: errors }
end
end
private
@tlowrimore
tlowrimore / palindrome.rb
Created Mar 12, 2014
Detects whether a number is palindromic without converting the number to a String.
View palindrome.rb
module Palindrome
def self.palindrome?(n)
n == reverse(n)
end
def self.reverse(n)
rs = 0
while n > 0
n, r = n.divmod 10
rs = rs * 10 + r
@tlowrimore
tlowrimore / vigenere.rb
Last active Aug 29, 2015
A Naive (Somewhat Readable) Vigenére Cipher
View vigenere.rb
module Vigenere
def self.encrypt(key, phrase)
exec key, phrase
end
def self.decrypt(key, phrase)
exec key, phrase, -1
end
def self.exec(key, phrase, direction=1)
View controller.rb
module Sluggo::Controller
MATCHER = /(^.+)-(.*$)/
def self.included(base)
base.send :include, InstanceMethods
end
module InstanceMethods
private
@tlowrimore
tlowrimore / or_scope.rb
Created Mar 27, 2013
OR's together provided scopes, returning an ActiveRecord::Relation. This implementation is smart enough to not only handle your _where_ clauses, but it also takes care of your joins! *Note* This is a work in progress, so it has room to grow
View or_scope.rb
module ActiveRecord::OrScope
def self.included(base)
base.send :extend, ClassMethods
end
module ClassMethods
def or_scope(*scopes)
conditions =
scopes
.map { |scope| "(#{scope.where_clauses.map{ |clause| "(#{clause})"}.join(" AND ")})" }
@tlowrimore
tlowrimore / assets_gz.conf
Last active Dec 14, 2015
Apache config directives for serving Rails's precompiled .gz files, created by the asset pipeline during precompile.
View assets_gz.conf
<LocationMatch "^/assets/.*$">
Header unset ETag
FileETag None
# RFC says only cache for 1 year
ExpiresActive On
ExpiresDefault "access plus 1 year"
RewriteEngine On
RewriteCond %{HTTP:Accept-Encoding} gzip
@tlowrimore
tlowrimore / union_scope.rb
Last active Nov 9, 2021
Unions multiple scopes on a model, and returns an instance of ActiveRecord::Relation.
View union_scope.rb
module ActiveRecord::UnionScope
def self.included(base)
base.send :extend, ClassMethods
end
module ClassMethods
def union_scope(*scopes)
id_column = "#{table_name}.#{primary_key}"
sub_query = scopes.map { |s| s.select(id_column).to_sql }.join(" UNION ")
where "#{id_column} IN (#{sub_query})"
@tlowrimore
tlowrimore / gist:3842767
Created Oct 5, 2012
troublesome Vagrantfile
View gist:3842767
# -*- mode: ruby -*-
# vi: set ft=ruby :
user = ENV['OPSCODE_USER'] || ENV['USER']
base_box = "precise64"
# ------------------------------------------------------------------------------
# Short-hand node defs.
# ------------------------------------------------------------------------------
nodes = {
:eh_app_node => {