Skip to content

Instantly share code, notes, and snippets.

View stevenharman's full-sized avatar

Steven Harman stevenharman

View GitHub Profile
@stevenharman
stevenharman / format-ruby
Created March 19, 2020 15:05
Run `standardrb --fix` as a git pre-commit hook. NOTE: this assumes `gem "standard"` is in your Gemfile
#!/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 November 19, 2019 14:26
Rails' binstubs vs. Bundler binstubs - incompatibility and breaking changes, maybe?

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 March 11, 2024 04:11
Heroku Release Phase script for managing Rails DB migrations, and playing nice with Review Apps and postdeploy scripts

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 September 26, 2017 01:27
Loading I18n translation files outside of Rails (for example, as part of an isolated/non-Rails spec)
# 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 June 8, 2017 01:15
A reasonable, and admittedly naïve, attempt at an automated Rails + CircleCI + Heroku deploy script. 🐉 THERE BE DRAGONS! 🐲
#!/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 17:17
A Lo-Fi View Model/Presenter Base Class for Rails Apps
# 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 February 22, 2023 12:49
A Git Pre-Commit hook to run the Rubocop static code analyzer. I'm not saying this is a Good Idea™. In fact, it's a Bad Idea™; integrate linting/formatting with your editor/IDE instead. Use at your own risk. If it breaks, feel free to keep both pieces.
#!/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'
@stevenharman
stevenharman / rerun-sidekiq.sh
Created May 11, 2016 20:49
Use rerun to restart the Sidekiq server, based on your Profile, when a file changes.
rerun --background --dir app,config,db,lib --pattern '{**/*.rb}' -- $(grep 'worker: ' Procfile | sed 's/worker: //')
@stevenharman
stevenharman / has_one_of_a_has_many.rb
Created January 13, 2016 19:52
Trying to avoid an N+1 query when we only want the first record of an ordered ActiveRecord association.
class Organization < ActiveRecord::Base
has_many :people, dependent: :destroy, inverse_of: :organization
has_many :locations, dependent: :destroy
has_many :activity_items, -> { order(last_activity_at: :desc) }, dependent: :destroy
has_one :last_activity, -> { order(last_activity_at: :desc) }, class_name: 'ActivityItem'
end
class ActivityItem < ActiveRecord::Base
belongs_to :organization
belongs_to :user
@stevenharman
stevenharman / bin-import.sh
Last active January 11, 2018 18:48
Via the magic of Bash and Heroku CLI you can pull down your Heroku App's database, migrate it, sanitize it (an exercise for the reader), and get on with your life. `bin/import -h` for help.
#!/usr/bin/env bash
set -euo pipefail
. ./bin/colors
heroku_app=your-app
capture=0
migrate=0
restore=0