Skip to content

Instantly share code, notes, and snippets.

View tlowrimore's full-sized avatar

Tim Lowrimore tlowrimore

View GitHub Profile
@tlowrimore
tlowrimore / jankylog.js
Created September 9, 2020 18:32
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.
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 March 10, 2016 17:41
Extends the functionality set forth in union_scope.rb, to include EXCEPT and INTERSECT operations
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 June 22, 2024 18:39
Keeps your API lookin' good! No need for all that nested_attributes pollution in your request/response payloads
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 March 12, 2014 03:33
Detects whether a number is palindromic without converting the number to a String.
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 August 29, 2015 13:57
A Naive (Somewhat Readable) Vigenére Cipher
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)
module Sluggo::Controller
MATCHER = /(^.+)-(.*$)/
def self.included(base)
base.send :include, InstanceMethods
end
module InstanceMethods
private
@tlowrimore
tlowrimore / or_scope.rb
Created March 27, 2013 19:39
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
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 December 14, 2015 23:59
Apache config directives for serving Rails's precompiled .gz files, created by the asset pipeline during precompile.
<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 January 13, 2023 21:12
Unions multiple scopes on a model, and returns an instance of ActiveRecord::Relation.
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 October 5, 2012 22:15
troublesome Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
user = ENV['OPSCODE_USER'] || ENV['USER']
base_box = "precise64"
# ------------------------------------------------------------------------------
# Short-hand node defs.
# ------------------------------------------------------------------------------
nodes = {
:eh_app_node => {