Skip to content

Instantly share code, notes, and snippets.

@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 / 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]);
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: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)
@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 / csv_ext.rb
Last active February 25, 2017 22:19
Monkey patch the CSV library to add i18n support
require 'csv'
module CoreExtensions
module CSV
module I18n
# Redefine the shift method to catch the exception and attempt to return
# a translated version of the error message using I18n.t
def shift
super
rescue ::CSV::MalformedCSVError => exception

Keybase proof

I hereby claim:

  • I am jhubert on github.
  • I am jhubert (https://keybase.io/jhubert) on keybase.
  • I have a public key whose fingerprint is CE02 7B8C 1EB8 1445 6A5D 3B93 55C9 EAAF 0186 36B4

To claim this, I am signing this object:

@jhubert
jhubert / benchmark-caches-from-heroku.markdown
Last active July 21, 2016 00:16
Benchmarking memcachier and redis-cloud from your heroku instance

I did some performance testing from heroku to memcachier and redis-cloud to check the iterations per second. Here is how I did it, based on the work that @nateberkopec did:

From heroku run bash -r production run the following:

gem install benchmark-ips --no-doc --no-ri
gem install redis-activesupport --no-doc --no-ri
# the other gems are already installed because they're included in our app's gemfile, but you may need:
# gem install dalli --no-doc --no-ri
# gem install rails --no-doc --no-ri