Skip to content

Instantly share code, notes, and snippets.

View mattlong's full-sized avatar
🌊

Matt Long mattlong

🌊
  • Medplum
  • San Francisco, CA
View GitHub Profile
@mattlong
mattlong / kill-defunct.sh
Created April 23, 2019 18:41
Kill defunct proccesses
#!/bin/bash
parents_of_dead_kids=$(ps -ef | grep [d]efunct | awk '{print $3}' | sort | uniq | egrep -v '^1$'); echo "$parents_of_dead_kids" | xargs kill
@mattlong
mattlong / rails_5_dirty.md
Created November 18, 2019 19:36 — forked from crivotz/rails_dirty.md
Rails 5.2 dirty

Rails dirty

After modifying an object and after saving to the database, or within after_save:

Rails <= 5 Rails >= 5.1
attribute_changed? saved_change_to_attribute?
changed? saved_changes?
changes saved_changes
attribute_was attribute_before_last_save
@mattlong
mattlong / .bashrc
Created February 4, 2020 18:57
Fix GPG decrypting
# If you get "Inappropriate ioctl for device" when decrypting
# Thanks to https://github.com/keybase/keybase-issues/issues/2798
export GPG_TTY=$(tty)
@mattlong
mattlong / fix-sound.sh
Created February 7, 2020 18:44
Fix OSX sound
#!/bin/bash
sudo kextunload /System/Library/Extensions/AppleHDA.kext
sudo kextload /System/Library/Extensions/AppleHDA.kext
@mattlong
mattlong / csrf_tokens.rb
Created April 6, 2020 17:40
Rails CSRF token analyzer
def real_csrf_token(token)
Base64.strict_decode64(token)
end
def xor_byte_strings(s1, s2)
s2_bytes = s2.bytes
s1.each_byte.with_index { |c1, i| s2_bytes[i] ^= c1 }
s2_bytes.pack("C*")
end
@mattlong
mattlong / pihole-whitelist.txt
Created April 20, 2020 20:28
Pihole whitelist
# [amplitude.com]
amplitude.com
amplify.amplitude.com
analytics.amplitude.com
api.amplitude.com
cdn.amplitude.com
discourse.amplitude.com
events.amplitude.com
go.amplitude.com
info.amplitude.com
@mattlong
mattlong / Gemfile
Created July 13, 2020 21:03 — forked from dhh/Gemfile
HEY's Gemfile
ruby '2.7.1'
gem 'rails', github: 'rails/rails'
gem 'tzinfo-data', '>= 1.2016.7' # Don't rely on OSX/Linux timezone data
# Action Text
gem 'actiontext', github: 'basecamp/actiontext', ref: 'okra'
gem 'okra', github: 'basecamp/okra'
# Drivers
@mattlong
mattlong / left_join_arel_example.rb
Created June 16, 2021 22:47 — forked from mildmojo/left_join_arel_example.rb
LEFT JOIN in ARel for ActiveRecord in Ruby on Rails
# Here's a contrived example of a LEFT JOIN using ARel. This is an example of
# the mechanics, not a real-world use case.
# NOTE: In the gist comments, @ozydingo linked their general-purpose ActiveRecord
# extension that works for any named association. That's what I really wanted!
# Go use that! Go: https://gist.github.com/ozydingo/70de96ad57ab69003446
# == DEFINITIONS
# - A Taxi is a car for hire. A taxi has_many :passengers.
# - A Passenger records one person riding in one taxi one time. It belongs_to :taxi.
module ActiveRecordExtension
extend ActiveSupport::Concern
module ClassMethods
# Simple left join taking advantage of existing Rails & Arel code
def left_joins(*args)
inner_joins = self.joins(*args).arel.join_sources
left_joins = inner_joins.map do |join|
Arel::Nodes::OuterJoin.new(join.left, join.right)
end
See question on stack overflow: http://stackoverflow.com/questions/28595636/rails-4-how-to-give-alias-names-to-includes-and-joins-in-active-record-que
- Model Student and model Teacher are both STI models with super class model User
- Model Story is a STI model with super class model Task
- includes() and joins(), both fails
Rails alias naming convention (includes() and joins())
- One model as parameter
- is base model (includes(:users))