Skip to content

Instantly share code, notes, and snippets.

Avatar

Joshua Flanagan joshuaflanagan

View GitHub Profile
@joshuaflanagan
joshuaflanagan / fish.rb
Last active Jan 13, 2016
Simplify working with UUID primary keys in Rails console
View fish.rb
class ActiveRecord::Base
# Find a record by UUID primary key prefix
#
# Example:
# > User.fish "1245"
# # User Load (0.3ms) SELECT "users".* FROM "users" WHERE ("users"."id" BETWEEN '12450000000000000000000000000000' AND '1245ffffffffffffffffffffffffffff') LIMIT $1 [["LIMIT", 2]]
# => #<User id: "12451304-761d-4e1d-8281-40aa6213b633", name: "josh">
def self.fish(partial_id)
return find(partial_id) unless partial_id.is_a?(String)
scrubbed = partial_id.gsub(/[^a-fA-F0-9]/, "")
@joshuaflanagan
joshuaflanagan / big_decimal_marshal.rb
Created Nov 15, 2015
BigDecimal does not Marshal round trip in ruby 2.1/2.2 (did in 1.9.3)
View big_decimal_marshal.rb
# Ruby 2.1.2
2.1.2 :173 > x = BigDecimal.new(1.0, 1)
=> #<BigDecimal:7ffcee09bb98,'0.1E1',9(27)>
2.1.2 :174 > y = Marshal.load Marshal.dump x
=> #<BigDecimal:7ffcec5d9ee0,'0.1E1',9(18)>
2.1.2 :175 > x == y
=> true
2.1.2 :176 > Marshal.dump(x) == Marshal.dump(y)
=> false
View no_surprises.rb
def no_surprises!
ActiveRecord::Base.class_eval do
include SkipsValidations
include SkipsCallbacks
end
end
module SkipsValidations
def perform_validations(*)
true
View pngcheck.rb
# http://www.w3.org/TR/PNG/#5DataRep
class PNGCheck
def initialize(path)
@path = path
end
def check_all!
expected_signature = [137, 80, 78, 71, 13, 10, 26, 10]
file.seek 0
signature_data = file.read(8)
@joshuaflanagan
joshuaflanagan / gist:523aeec5fceb3bc75691
Created Dec 17, 2014
Recover a branch when you know the SHA, but don't have it locally
View gist:523aeec5fceb3bc75691
http -v -a username:password POST https://api.github.com/repos/owner/repo/git/refs ref="refs/heads/recovered_branch" sha="290b2090a741d35edd67f15c74b2d43299df8995"
git fetch
git checkout recovered_branch
@joshuaflanagan
joshuaflanagan / gist:7495006
Created Nov 16, 2013
Postgres extensions on Amazon RDS
View gist:7495006
name | default_version | installed_version | comment
------------------------+-----------------+-------------------+---------------------------------------------------------------------
pg_buffercache | 1.0 | | examine the shared buffer cache
earthdistance | 1.0 | | calculate great-circle distances on the surface of the Earth
pg_freespacemap | 1.0 | | examine the free space map (FSM)
intagg | 1.0 | | integer aggregator and enumerator (obsolete)
plperl | 1.0 | | PL/Perl procedural language
sslinfo | 1.0 | | information about SSL certificates
btree_gist | 1.0 | | support for indexing common datatypes in GiST
fuzzystrmatch | 1.0
@joshuaflanagan
joshuaflanagan / pull_request_labels.user.js
Created Feb 2, 2013
UserScript to show Labels on Pull Requests
View pull_request_labels.user.js
// ==UserScript==
// @name Pull Request Labels
// @namespace http://joshuaflanagan.com
// @include https://github.com/*/*/pulls
// @version 1
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// ==/UserScript==
$(function(){
var $pull_requests = $(".pulls-list .list-browser-item h3");
$pull_requests.each(function(){
@joshuaflanagan
joshuaflanagan / watch_git.rb
Created Jun 27, 2012
See what's going on in your .git folder when you perform git commands
View watch_git.rb
# I used this to monitor changes to the .git folder during
# my Austin on Rails lightning talk on Understanding Git.
#
# I ignore any changes to the .git/logs folder, because they are
# noisy and don't add a lot to understanding.
# If you want "the whole truth", remove the :ignore parameter below.
#
# ruby listen.rb <path to .git folder>
#
# gem install listen # its the foundation of Guard
@joshuaflanagan
joshuaflanagan / delegate_eql.rb
Created May 5, 2012
DelegateClass instance doesn't eql? itself
View delegate_eql.rb
require 'test/unit'
require 'delegate'
# These tests demonstrate some unexpected (to me)
# behavior of a class created by DelegateClass.
# An instance of a DelegateClass created class does not eql? itself.
#
# The WidgetTests all pass.
# The comparison and eql WidgetWrapperTests fail.
# ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]
View example_config_initializer_smart_urls.rb
SmartUrlHelper.configure do |url|
url.for ->model{ model.is_a?(Comment) } do |helpers, model|
helpers.post_comment_path(model.post, model)
end
# TODO: Add a convenience method for matching on type:
# url.for(Comment) do |helpers, model|
# helpers.post_comment_path(model.post, model)
# end
end
You can’t perform that action at this time.