Skip to content

Instantly share code, notes, and snippets.

View t27duck's full-sized avatar
🦆

Tony Drake t27duck

🦆
View GitHub Profile
import { Controller } from "@hotwired/stimulus"
import { DirectUpload } from "@rails/activestorage"
// This is a very rough hack to allow a file upload field that we'd like to be a direct upload
// but is loading into the dom in a way that the normal form submit event is clobbered by other
// processes (like hotwire).
//
// This is made based off of the ActiveStoreage guides for integrating direct uploads for other
// JS frameworks. The biggest difference here is when the user picks a file, the file is immedidatly
// uploaded to the sever. More things in this controller should be fleshed out in the future such
@t27duck
t27duck / clear_local_branches.rb
Created December 21, 2017 14:01
Deletes local branches that have been merged into master
branch_prefix = "pt_"
branches_to_keep = []
cmd = `git branch --merged master`
branches = cmd.split("\n").map(&:strip).select{ |b| b.start_with?(branch_prefix) && !branches_to_keep.include?(b) }
branches.each do |branch|
`git branch -D #{branch}`
end
@t27duck
t27duck / github_unicorn_killer.rb
Created July 3, 2017 18:06
OG unicorn killer script from github
#! /usr/bin/env ruby
unicorn_worker_memory_limit = 460_000
loop do
begin
# unicorn workers
#
# ps output line format:
# 31580 275444 unicorn_rails worker[15] -c /data/github/current/config/unicorn.rb -E production -D
class Post
has_many :cat_relations
has_many :categories, through: :cat_relations
end
class Category
has_many :cat_relations
has_many :posts, through: :cat_relations
end
# Your code you include somewhere
module MySpecialCodeBlock
def foo(x)
puts "Twice x is: #{x*2}"
super
end
end
# From a libary
class TheRealClass
@t27duck
t27duck / paper_trail_updated_by.rb
Last active August 29, 2015 14:07
Initializer to be able to set an updated_by on paper trailed models. Giving it an ActiveRecord object will store whodunnit as "ModelName::Id". Calling updated_by will return the ActiveRecord object. This is also applied to the Version model.
module PaperTrail
module Model
# Creates and updated_by attributes on all paper trailed models. If given
# an ActiveRecord instance, PaperTrail.whodunnit will be set as
# "ModelName::Id". Calling updated_by will return the ActiveRecord object.
module InstanceMethods
def updated_by=(updater)
PaperTrail.whodunnit = PaperTrail::UpdatedByParser.to_s(updater)
end