Skip to content

Instantly share code, notes, and snippets.

Avatar

Matt Long mattlong

  • Y Combinator
  • San Francisco, CA
View GitHub Profile
@mattlong
mattlong / Gemfile
Created Jul 13, 2020 — forked from dhh/Gemfile
HEY's Gemfile
View 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
View pihole-whitelist.txt
# [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 / csrf_tokens.rb
Created Apr 6, 2020
Rails CSRF token analyzer
View csrf_tokens.rb
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
View fix-sound.sh
#!/bin/bash
sudo kextunload /System/Library/Extensions/AppleHDA.kext
sudo kextload /System/Library/Extensions/AppleHDA.kext
@mattlong
mattlong / .bashrc
Created Feb 4, 2020
Fix GPG decrypting
View .bashrc
# If you get "Inappropriate ioctl for device" when decrypting
# Thanks to https://github.com/keybase/keybase-issues/issues/2798
export GPG_TTY=$(tty)
View rails_5_dirty.md

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 / kill-defunct.sh
Created Apr 23, 2019
Kill defunct proccesses
View kill-defunct.sh
#!/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 / organization.rb
Last active Nov 15, 2018
Rails / ActiveRecord has_one and has_many to same model
View organization.rb
# From https://github.com/rails/rails/issues/20606#issuecomment-113323102
# Almost good, but I don't think it handles the case of promoting an
# existing location to being the primary location correctly in some cases
class Organization < ActiveRecord::Base
has_many :locations, dependent: :destroy, autosave: true # autosave necessary for the importer
has_one :primary_location, -> { where(locations: { primary: true }) }, class_name: "Location", autosave: true
# Override getter to fix issue with Rails not reloading the primary_location after resetting it to nil
def primary_location
@mattlong
mattlong / rails-jsonb-queries
Created Sep 24, 2018 — forked from mankind/rails-jsonb-queries
Rails-5 postgresql-9.6 jsonb queries
View rails-jsonb-queries
http://stackoverflow.com/questions/22667401/postgres-json-data-type-rails-query
http://stackoverflow.com/questions/40702813/query-on-postgres-json-array-field-in-rails
#payload: [{"kind"=>"person"}]
Segment.where("payload @> ?", [{kind: "person"}].to_json)
#data: {"interest"=>["music", "movies", "programming"]}
Segment.where("data @> ?", {"interest": ["music", "movies", "programming"]}.to_json)
Segment.where("data #>> '{interest, 1}' = 'movies' ")
Segment.where("jsonb_array_length(data->'interest') > 1")
@mattlong
mattlong / where_is.rb
Created Aug 27, 2018 — forked from wtaysom/where_is.rb
A little Ruby module for finding the source location where class and methods are defined.
View where_is.rb
module Where
class <<self
attr_accessor :editor
def is_proc(proc)
source_location(proc)
end
def is_method(klass, method_name)
source_location(klass.method(method_name))