Skip to content

Instantly share code, notes, and snippets.


Ken Mayer kmayer

View GitHub Profile
kmayer / application_record.rb
Last active Jun 15, 2017
How to get a fast, but approximate, row count in ActiveRecord + Postgresql
View application_record.rb
# frozen_string_literal: true
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
class << self
def approximate_row_count(table_name =
results = ActiveRecord::Base.connection_pool.with_connection { |c|
c.execute("SELECT reltuples::BIGINT AS approximate_row_count FROM pg_class WHERE relname = '#{table_name}'")
results.present? ? results.first['approximate_row_count'].to_i : nil
kmayer /
Created Mar 8, 2017
Instant web proxy server
ruby -r webrick/httpproxy -e 's = => 9999, :RequestCallback =>{|req,res| puts req.request_line, req.raw_header}); trap("INT"){s.shutdown}; s.start'
View logentries_alerts_controller.rb
class LogentriesAlertsController < ApplicationController
skip_before_action :verify_authenticity_token
def create
return head(:unprocessable_entity) unless
if what =~ /R1[45]/ &&
who =~ /web\.\d+/
kmayer / logentries_auth.rb
Last active Oct 26, 2016 HMAC Authenticator in Ruby, for Rails
View logentries_auth.rb
# Check authenticity of a webhook
# cf:
# cf:
require 'openssl'
require 'base64'
require 'digest/md5'
class LogentriesAuth
attr_reader :request
kmayer / sticky_heading_list.jsx
Created Apr 28, 2016
<StickyHeadingList> react component
View sticky_heading_list.jsx
var _css = require("./sticky_heading_list.scss");
// import from
// requires jQuery as a global
/* A little helper to calculate the sum of different
* CSS properties
* $('#my-div').cssSum('paddingLeft', 'paddingRight');
kmayer / puma.rb
Last active Apr 5, 2016
Puma plugin to stop via Redis on Heroku
View puma.rb
# === Plugins ===
require './lib/puma/plugin/redis_stop_puma'
plugin 'redis_stop_puma'
View Gemfile
source ''
gem 'octopress', '~> 3.0'
# gem 'drjekyll'
gem 'html2markdown'
group :jekyll_plugins do
gem 'octopress-codefence'
gem 'octopress-debugger'
View gist:f9e808488229e0576333
class BankAccount < ActiveRecord::Base
# :identifier is a column in "bank_accounts"
delegate :name, to: :balanced_api
def balanced_api
@balanced_api ||= begin
View gist:a899782e26da3548aa55
"status": 201,
"data": {
"bank_name": "SAN MATEO CREDIT UNION",
"_type": "bank_account",
"_uris": {
"debits_uri": {
"_type": "page",
"key": "debits"
kmayer / trustctime.rb
Created Nov 27, 2013
chef recipe to turn off ctime trust
View trustctime.rb
execute "mistrust ctime" do
command "git config --global core.trustctime #{node['git']['trustctime'].to_s }"
user node['current_user']