Skip to content

Instantly share code, notes, and snippets.

Avatar

Steven Harman stevenharman

View GitHub Profile
@stevenharman
stevenharman / gemfresh
Last active Jun 4, 2021
gemfresh: A handy script for determining the freshness of dependencies in a Ruby code base. Leverages bundler and libyear-bundler.
View gemfresh
#!/usr/bin/env bash
# shellcheck disable=SC2059
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
NO_COLOR='\033[0m'
CLEAR_LINE='\r\033[K'
@stevenharman
stevenharman / erb_now.rb
Last active Dec 16, 2020
Render Ruby ERB templates, inline, with variables bound and available local variables in the template. This technique is useful for rendering test fixtures via ERB, for example.
View erb_now.rb
require "ostruct"
# A wrapper around a single ERB template that will bind the given Hash of values,
# making them available as local variables w/in the template.
class ErbNow < OpenStruct
# Render a given ERB +template+ string, binding the entries in the given +attrs+ Hash
# as local variables in the template, with the key as the variable name.
#
# @example
#
@stevenharman
stevenharman / restart-network-services
Created Jun 16, 2020
Restart Active Network Services on your Mac. You know, for the VPN!
View restart-network-services
#! /usr/bin/env bash
# shellcheck disable=SC2059
set -euo pipefail
# Toggle all currently 'Active' network servcies (e.g., Wi-Fi, Ethernet
# connections, etc...) to "restart" them. We'll ignore any already 'Disabled'
# services, and toggle all of the others to 'Disabled' and then back to
# 'Enabled'. This has been found helpful when your VPN won't re-connect after
# undocking and re-docking your MacBook, for example.
@stevenharman
stevenharman / format-ruby
Created Mar 19, 2020
Run `standardrb --fix` as a git pre-commit hook. NOTE: this assumes `gem "standard"` is in your Gemfile
View format-ruby
#!/bin/sh
# This file is `.git/hooks/format-ruby` and it has been `chmod +x`'d
# Assumption: https://github.com/testdouble/standard is in your Gemfile
set -e
rubyfiles=$(git diff --cached --name-only --diff-filter=ACM "*.rb" "*.rake" "Gemfile" "Rakefile" | tr '\n' ' ')
[ -z "$rubyfiles" ] && exit 0
# Standardize all ruby files
@stevenharman
stevenharman / 00_Rails-binstubs-vs-Bundler-binstubs.md
Last active Nov 19, 2019
Rails' binstubs vs. Bundler binstubs - incompatibility and breaking changes, maybe?
View 00_Rails-binstubs-vs-Bundler-binstubs.md

Rails' binstubs vs. Bundler binstubs

There's an incompatibility in the Rails vs Bundler-generated bin/bundle binstub. Futher, other Bundler-generated binstubs (e.g., bin/rspec) generated by older versions of Bundler don't play well with those generated by Bundler v1.16.

The Binstubs and versions.

bin/rspec

The older rspec-old.rb was generated by an older version of Bundler.

@stevenharman
stevenharman / 00_Heroku-Release-Phase-Review-Apps-Rails_README.md
Last active Aug 15, 2021
Heroku Release Phase script for managing Rails DB migrations, and playing nice with Review Apps and postdeploy scripts
View 00_Heroku-Release-Phase-Review-Apps-Rails_README.md

Heroku Release Phase + Review Apps + Rails

This is a simplified, but fairly thorough, set of scripts and configuration to enable Heroku Release Phase for Rails apps. Further, this particular set up plays nicely with Heroku Review Apps in that the release phase script will:

  1. Fail, loudly, if the DB does not yet exist.
  2. Load the DB schema if the current schema version (as determined by bin/rails db:version) is 0.
  3. Run DB migrations otherwise.

For a "normal" app that usually means it will run the DB migrations.

@stevenharman
stevenharman / i18n_helpers.rb
Last active Sep 26, 2017
Loading I18n translation files outside of Rails (for example, as part of an isolated/non-Rails spec)
View i18n_helpers.rb
# frozen_string_literal: true
module I18nHelpers
def self.included(_mod)
locale_dir = Pathname.new(__dir__) + '../../config/locales/'
[:en].each do |locale|
locale_file = locale_dir + "#{locale}.yml"
I18n.backend.store_translations(locale, YAML.load_file(locale_file.open)[String(locale)])
end
end
@stevenharman
stevenharman / heroku_deploy.sh
Last active Jun 8, 2017
A reasonable, and admittedly naïve, attempt at an automated Rails + CircleCI + Heroku deploy script. 🐉 THERE BE DRAGONS! 🐲
View heroku_deploy.sh
#!/usr/bin/env bash
#
# Usage: bin/heroku_deploy <heroku-app-name> [git-treeish : default=HEAD]
. ./shell_colors
set -euo pipefail
app_name=$1
app_remote=$app_name
@stevenharman
stevenharman / _view_model.rb
Last active May 24, 2017
A Lo-Fi View Model/Presenter Base Class for Rails Apps
View _view_model.rb
# app/model/view/view_model.rb
# A base class for other view models (a.k.a., presenters)
module View
class ViewModel < SimpleDelegator
BUILD_DEFAULT_CONTEXT = -> { View::DefaultViewContext.new }
def initialize(model, view_context: BUILD_DEFAULT_CONTEXT.call)
@view_context = view_context
super(model)
end
@stevenharman
stevenharman / pre-commit.sh
Last active Mar 19, 2021
A Git Pre-Commit hook to run the Rubocop static code analyzer. I'm not saying this is a Good Idea™. Use at your own risk. If it breaks, feel free to keep both pieces.
View pre-commit.sh
#!/usr/bin/env bash
set -eu
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NO_COLOR='\033[0m'
CLEAR_LINE='\r\033[K'