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 / presence_refinements.rb
Last active August 15, 2024 20:01
Simple drop-in refinements module that replicates Rails's presence methods
# frozen_string_literal: true
module PresenceRefinements
refine Object do
def blank? = (respond_to?(:empty?) ? !!empty? : !self)
def present? = !blank?
def presence = (self if present?)
end
# This bit is cribbed from https://github.com/rpanachi/core_ext
@johncarney
johncarney / pre-push
Last active July 29, 2024 18:04
Git pre-push rubocop check
#!/bin/sh
# Passes all changed and added files on the current branch to RuboCop before pushing. Paste the below into your
# .bin/hooks/pre-push file, which should be made executable.
# Note that it assumes that your trunk branch is named "main." If this is not the case, make the appropriate adjustment.
TRUNK_BRANCH=main
CURRENT_BRANCH="$(git branch --show-current)"
if [ "$CURRENT_BRANCH" != "$TRUNK_BRANCH" ]; then
rubocop $(git diff --name-status "$TRUNK_BRANCH" "$CURRENT_BRANCH" | egrep '^[AM]' | cut -f 2 | egrep '(^Gemfile|.rb)$')
@johncarney
johncarney / rspec-flake-finder.rb
Last active July 15, 2024 22:32
RSpec Flake Finder
#!/usr/bin/env ruby
# frozen_string_literal: true
require "json"
require "open3"
require "pathname"
require "tempfile"
# Detects intermittent failures in an RSpec test suite and reports on varying
# coverage.
@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: