Skip to content

Instantly share code, notes, and snippets.

@codeodor
codeodor / index_stats.rb
Created April 14, 2013 12:50
Inspect migrations to get an idea of how we define and use indexes in a Rails project.
apps = [
"/path/to/rails/project/root",
"/path/to/another/rails/project/root"
]
def number_of_migrations_since_first_reference(attribute, directory, indexed_in)
grep_command = "grep -H \"#{attribute.gsub(/[\[\]\s]/,'')}\" #{directory}/*.rb"
all_refs = `#{grep_command}`
return -1 unless all_refs.length > 0 # probably defined with t.references :other_table
@codeodor
codeodor / epic_yak_shave.sh
Created February 11, 2013 23:42
Setting up statsd to run with the graphite backend.
git clone git://github.com/etsy/statsd.git
cd statsd
# yak shaving:
# install nodejs if you don't have it
brew install node # assuming you have/use homebrew
# install python if you don't have it
# I already had python due to it being packaged with MacOS, but if you don't have it, you'll need it
@codeodor
codeodor / friend.rb
Created November 14, 2012 02:33
Friend functions in Ruby
# See http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-set_trace_func for docs on this function
require 'ostruct'
module Friendly
def self.included(base)
base.extend(ClassMethods)
end
def method_missing(name, *args, &block)
@codeodor
codeodor / routes.rb
Created October 24, 2012 17:55
How do I keep all of these routes, but also make them available under a scope?
# I have read http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Resources.html#method-i-resources
# and remain clueless.
MyApp::Application.routes.draw do
resources :blogs do
resources :comments
end
# then imagine tons of routing below this
end
@codeodor
codeodor / .rspec
Created July 14, 2012 21:57 — forked from coreyhaines/.rspec
Loading just active record
--colour
-I app
@codeodor
codeodor / gist:1752635
Created February 6, 2012 15:19
Thread Dump from JRuby when Active Record MS SQL Server Tests are stuck
2012-02-06 09:16:21
Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.4-b02-402 mixed mode):
"Attach Listener" daemon prio=9 tid=101ab8000 nid=0x10e607000 waiting on condition [00000000]
java.lang.Thread.State: RUNNABLE
"Thread-14" daemon prio=5 tid=10c89c800 nid=0x10e404000 in Object.wait() [10e403000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at org.jruby.util.ShellLauncher$StreamPumper.run(ShellLauncher.java:1323)
@codeodor
codeodor / filter_query_param.rb
Created June 3, 2011 21:28
Beginnings of filter_query_param hook for modifying where clauses before the query in ActiveRecord
ActiveRecord::QueryMethods.class_eval do
# this is code from the original file, with the .map do |x| ... added to build_where
# ideally, we would alias original_where where and work with the relation.where_values, but
# I haven't yet been successful in that attempt.
def where(opts, *rest)
return self if opts.blank?
relation = clone
relation.where_values +=
@codeodor
codeodor / remote_function_with_container_and_id_from_javascript.rb
Created April 13, 2011 18:55
What's a better name for this function, and how do you better accomplish the same task?
# Pass the container name, ID variable name, and options with those names
# surrounded by ## on either side, and this function will replace those,
# closing the string and referring to a javascript variable in each place
#
# An example of when it's useful: Suppose you have a set of lists on screen
# with some items. You periodically want to refresh the items for one of
# the lists, and you don't want to generate the same function for each list
# in the page. Instead, you'd rather just have one function and pass the ID
# of the list to it, and figure out the DOM ID and route based on the ID
# passed to the javascript function.
SELECT DISTINCT 'script/generate scaffold ' + t.name + ' ' + column_names
FROM sys.tables t
CROSS APPLY (
SELECT c.name +
case when max_length > 255 then ':text' else ':string' end + ' '
FROM sys.columns c
WHERE c.object_id = t.object_id
ORDER BY c.column_id
FOR XML PATH('') ) dummy_identifier ( column_names )
<script type="text/javascript">
timeOfLastKeyPress = new Date().getTime() * 2; // make it in the future
maxMillisecondsBetweenKeystrokes = 1000;
millisecondsBetweenCheckingForStoppage = 250;
function logKeyPress(){
timeOfLastKeyPress = new Date().getTime();
}
function userStoppedTyping(){