Skip to content

Instantly share code, notes, and snippets.

@jhubert
jhubert / results.txt
Created October 15, 2014 17:31
Results from the Cool Climate API based on just passing in location, vehicle miles and vehicle mpg
result_takeaction_carfuel_reduction: 0
result_takeaction_carmfg_reduction: 0
result_takeaction_pubtrans_reduction: 0
result_takeaction_airtravel_reduction: 0
result_takeaction_electricity_reduction: 0
result_takeaction_natgas_reduction: 0
result_takeaction_otherfuels_reduction: 0
result_takeaction_watersewage_reduction: 0
result_takeaction_construction_reduction: 0
result_takeaction_meat_reduction: 0
@jhubert
jhubert / cookie2form.js
Last active November 5, 2018 04:19
Capture the UTM values from a querystring and store them in a long lived document cookie.
// Append UTM values to any form on the page
(function () {
function getUTMCookies() {
var cookies = {}, pairs = document.cookie.split(";"), i, pair, key;
for (i = 0; i < pairs.length; i++) {
pair = pairs[i].split("=");
key = (pair[0] + '').trim();
if (key.indexOf('utm') > -1) {
cookies[key] = unescape(pair[1]);
@jhubert
jhubert / namespaced_controller_routes.rb
Last active October 9, 2018 17:25
Investigating a strange issue with named routes and namespaced controllers
begin
require 'bundler/inline'
require 'pry'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
@jhubert
jhubert / delete-hipchat-from-sender.js
Created February 18, 2014 23:39
Delete all the messages from the Hipchat message log that were sent from the expected sender
$('form').each(function(i, el){
var sender = $(this).parent().siblings('p').text().trim();
if (sender == 'Errbit') {
var data = 'action=delete';
data += '&message_id='+$(el).find('input[name="message_id"]').val();
data += '&xsrf_token='+$(el).find('input[name="xsrf_token"]').val();
data += '&message_index='+$(el).find('input[name="message_index"]').val();
$.ajax({
type: $(el).attr('method'),
url: $(el).attr('action'),
(function () {
function deleteAll() {
var list = $('ul#list'),
list_id = parseInt($(list[0]).attr('rel'), 10),
list_name = $('#list' + list_id + ' b').html();
if (list_id > 0) {
if (confirm('Are you sure you want to delete all the items in ' + list_name)) {
list.find('li').each(function () {
There is a small period of time when migrations aren't recognized by rails during the deploy process and this causes errors.
- old code is running
- new code hits heroku
- heroku restart -> loads up the new code. takes 3ish minutes
- migration happens
- heroku restart -> loads up the new code again. takes 3ish minutes
With Preboot:
[old code ----------------------------------------------------]
require 'csv'
require 'csv-i18n'
require 'i18n'
I18n.backend.store_translations(:en, csv: { exception: { unclosed_quoted_field: "custom error on line %{line_number}" } })
CSV.parse('"')
# lib/ruby/2.3.0/csv.rb:1898:in `block in shift': custom error on line 1 (CSV::MalformedCSVError)
# from lib/ruby/2.3.0/csv.rb:1805:in `loop'
# from lib/ruby/2.3.0/csv.rb:1805:in `shift'
@jhubert
jhubert / disable_inactive_users_job.rb
Last active February 26, 2017 22:16
The before code for my article on refactoring in the real world
# Disable users who have had no activity in the last 14 days
class DisableInactiveUsersJob
include Sidekiq::Worker
def perform
now = Time.zone.now
target_users = users_with_no_activity_since(now - 14.days)
# Load the ids before we update the users or the pluck query will return nothing
target_user_ids = target_users.pluck(:id)
@jhubert
jhubert / disable_inactive_users_job.rb
Last active February 26, 2017 22:15
The after code for my article on Real World Refactoring
# Disable users who have had no activity in the last 14 days
class DisableInactiveUsersJob
include Sidekiq::Worker
def perform
users_with_no_activity_since(14.days.ago).pluck(:id).each do |user_id|
DisableUser.call(user_id)
end
end
@jhubert
jhubert / disable_rejected_emails_job.rb
Created February 26, 2017 21:16
Middle point for my article on refactoring in the real world
# Disable users who have email addresses that are bouncing
class DisableRejectedEmailsJob
include Sidekiq::Worker
def perform
now = Time.zone.now
target_users = User.enabled.where(email: rejected_emails)
# Load the ids before we update the users or the pluck query will return nothing
target_user_ids = target_users.pluck(:id)