Skip to content

Instantly share code, notes, and snippets.

@timrwilliams
timrwilliams / routes.rb
Created August 26, 2013 10:59
How to secure access to Resque admin page in Rails 3.2 by only allowing users with a specified role to access the /resque route. Non authorized users will receive 404 error.
# Assumes you have a role attribue on user model
authenticate :user, lambda {|u| u.role == 'super_admin' } do
mount Resque::Server.new, :at => "/resque"
end
@timrwilliams
timrwilliams / geocoder.rake
Created November 20, 2013 10:24
A rake task to geocode all objects of a particular class using the Ruby Geocoder gem, retrying 3 times if unsuccessful. This is primarily targetted at the Google Maps API which limits the number of requests per second. By building in an increasing back off before each retry we can ensure we recover from these temporary bans.
namespace :geocoder do
desc "Geocode all objects without coordinates."
task :all_with_retry => :environment do
class_name = ENV['CLASS'] || ENV['class']
sleep_timer = ENV['SLEEP'] || ENV['sleep']
raise "Please specify a CLASS (model)" unless class_name
klass = class_from_string(class_name)
klass.not_geocoded.each do |obj|
geocode_with_retry obj
@timrwilliams
timrwilliams / data.json
Last active November 27, 2019 09:21
Sample setup for refreshing a Rickshaw graph from remote JSON source
[{"color":"#DDDF0D","name":"maps.googleapis.com","data":[{"x":1397779200,"y":13},{"x":1397782800,"y":11},{"x":1397786400,"y":10},{"x":1397790000,"y":12},{"x":1397793600,"y":14},{"x":1397797200,"y":13},{"x":1397800800,"y":6},{"x":1397804400,"y":0},{"x":1397808000,"y":0},{"x":1397811600,"y":2},{"x":1397815200,"y":33},{"x":1397818800,"y":17},{"x":1397822400,"y":7},{"x":1397826000,"y":16},{"x":1397829600,"y":29},{"x":1397833200,"y":25},{"x":1397836800,"y":22},{"x":1397840400,"y":30},{"x":1397844000,"y":27},{"x":1397847600,"y":27},{"x":1397851200,"y":18},{"x":1397854800,"y":23},{"x":1397858400,"y":19},{"x":1397862000,"y":7},{"x":1397865600,"y":9},{"x":1397869200,"y":8},{"x":1397872800,"y":21},{"x":1397876400,"y":9},{"x":1397880000,"y":5},{"x":1397883600,"y":6},{"x":1397887200,"y":2},{"x":1397890800,"y":0},{"x":1397894400,"y":0},{"x":1397898000,"y":0},{"x":1397901600,"y":23},{"x":1397905200,"y":0},{"x":1397908800,"y":5},{"x":1397912400,"y":2},{"x":1397916000,"y":5},{"x":1397919600,"y":14},{"x":1397923200,"y":21},{"
@timrwilliams
timrwilliams / unicorn.rb
Created October 8, 2014 09:21
Sample Unicorn.rb
#config/unicorn.rb
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 15
preload_app true
before_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
Process.kill 'QUIT', Process.pid
end
@timrwilliams
timrwilliams / qgoverride
Last active December 21, 2015 21:23
Override QuotaGuard Static Socksify env variable setup with your own socks.conf file
#!/bin/bash
# Script inspired by and derived from Proximo (https://github.com/pirateradio/proximo-stacklet)
# Usage ./qgoverride rails s
if [ "${QUOTAGUARDSTATIC_URL}" == "" ]; then
echo "No QUOTAGUARDSTATIC_URL env variable found. Have you installed QuotaGuard Static?"
echo "If using Heroku:"
echo " heroku addons:add QuotaGuardstatic:test"
echo "If using another platform sign-up at quotaguard.com and follow documentation to manually set variable"
exit 2
@timrwilliams
timrwilliams / designer.html
Last active August 29, 2015 14:08
designer
<link rel="import" href="../core-scaffold/core-scaffold.html">
<link rel="import" href="../core-header-panel/core-header-panel.html">
<link rel="import" href="../core-menu/core-menu.html">
<link rel="import" href="../core-item/core-item.html">
<link rel="import" href="../core-icon-button/core-icon-button.html">
<link rel="import" href="../core-toolbar/core-toolbar.html">
<link rel="import" href="../core-menu/core-submenu.html">
<link rel="import" href="../core-icons/core-icons.html">
<link rel="import" href="../paper-item/paper-item.html">
module RailsAppName
class Application < Rails::Application
# .... your settings
require "#{Rails.root}/lib/cloud_flare_middleware"
config.middleware.insert_before(0, Rack::CloudFlareMiddleware)
# ... your settings
end
end
@timrwilliams
timrwilliams / .irbrc
Created October 4, 2017 14:24
Choose an Apartment Tenant on start of IRB/Rails console (works on Heroku)
puts "What tenant do you require? "
tenant_names = Apartment.tenant_names
tenant_names.each_with_index do | tenant, index |
puts "#{index}: #{tenant}"
end
tenant_index = gets.chomp.to_i
tenant = tenant_names[tenant_index]
tenant = "public" if tenant.blank?
Apartment::Tenant.switch!(tenant)
puts "Current tenant: #{Apartment::Tenant.current}"
@timrwilliams
timrwilliams / i18ncount.awk
Last active January 3, 2018 12:25
Count number of unique keys and words in a Rails translation file
awk '{ if( $1 != "#" ){ print $0 } }' en.yml | awk -F':' '{print $2}' | awk 'NF'
@timrwilliams
timrwilliams / clone_schema.sql
Created August 7, 2018 16:06
Clone Postgres schema
-- Function: clone_schema(text, text)
-- DROP FUNCTION clone_schema(text, text);
CREATE OR REPLACE FUNCTION clone_schema(
source_schema text,
dest_schema text,
include_recs boolean)
RETURNS void AS
$BODY$