Skip to content

Instantly share code, notes, and snippets.

View johncarney's full-sized avatar

John Carney johncarney

  • Victor, Idaho
View GitHub Profile
@johncarney
johncarney / socials.md
Last active August 16, 2023 21:46
Socials

Music I like

A random list of tracks that comes up in the algorithmic feeds I listen to that I particularly like.

Track Artist Country
[Gagarin] [Public Service Broadcasting] UK
[As I Went Out One Morning][dps-01] [Dirty Projectors] US
[Make The Road By Walking][msb-01] [Menahan Street Band] US
[Home][vs-01] [Villagers] Ireland
@johncarney
johncarney / options.rb
Created July 12, 2020 23:42
Wrapper for OptionParser
# frozen_string_literal: true
require "active_support/all"
require "optparse"
module Options
extend ActiveSupport::Concern
included do
delegate :arguments, :options, to: :Options
@johncarney
johncarney / call_me.md
Last active July 12, 2020 22:37
Module for implementing the service object pattern that allows for custom 'verbs'

A module for implementing the Service Object pattern that allows for custom "verbs".

Usage

Including CallMe in your class adds a call class method that instantiates an object of the class using the provided arguments and invokes its call method. For example:

@johncarney
johncarney / pre-push.sh
Last active March 3, 2020 20:04
Git pre-push hook that runs Rubocop on files that have been added or modified on the current branch
#!/usr/bin/env sh
# If you don't already have a pre-push hook in your working copy, simply copy
# this into .git/hooks/pre-push in your working copy. If you already have a
# pre-push hook, you'll have to integrate it with your existing hook.
function current_branch {
git rev-parse --abbrev-ref HEAD
}
@johncarney
johncarney / associations-using-with-deleted.rb
Last active July 30, 2018 23:08
Scans all associations in a Rails app and lists those that have a scope that uses `with_deleted`. Also lists potential uses of those associations in joins. The list of joins will likely include false positives.
# ###########################################################################
#
# Probes all model associations looking for ones with a scope that call
# `with_deleted`. Lists all such associations, along with the location in
# source that `with_deleted` is called.
#
# Usage:
#
# $ rails runner script/associations-using-with-deleted.rb
#

The arel_extensions gem has a couple of bugs...

  1. DateSub doesn't work:
(table[:column] - 10.hours).to_sql
# => NoMethodError: undefined method `to_sql' for nil:NilClass
  1. For MySQL, DateAdd generates a double-minus when given a negative duration:
@johncarney
johncarney / yaml-with-lines.rb
Last active June 8, 2022 13:15
A Psych-based YAML parser that captures line numbers
#!/usr/bin/env ruby
# Custom Psych parser that captures line number information from a YAML file.
#
# For a project I'm working on I need to be able to determine which line(s) in a YAML
# file a particular value comes from. There are a few bits of advice on the internet
# about this, the best of them that I've found involves monkey-patching, which is a
# fairly low bar for "best" in my opinion. I found it on Stack Overflow:
#
# https://stackoverflow.com/questions/29462856/loading-yaml-with-line-number-for-each-key
@johncarney
johncarney / null_order_arel_extension.rb
Last active November 9, 2017 18:50
Possible null ordering solutions
# Examples
#
# MyModel.order(MyModel.arel_table[:name].asc.nulls_last)
# MyModel.order(MyModel.arel_table[:name].desc.nulls_first)
module Arel
module NullOrder
def nulls_last
Arel::Nodes::NullsLast.new self
end
@johncarney
johncarney / rails-locales-conflicts.rb
Last active January 3, 2017 02:58
Lists conflicting translations from your Rails locale files. Note that it will only detect conflicts between different locale files. Conflicts within a single locale file will not be detected.
#!/usr/bin/env ruby
# Usage: ruby rails-locales-conflicts.rb [<rails root>]
require "pathname"
require "yaml"
class Translations
def initialize
@translations = Hash.new do |h, k|