Skip to content

Instantly share code, notes, and snippets.

@codeodor
codeodor / bug_report.rb
Created October 2, 2014 20:04
Demo of bug described by rails/rails #17147
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'rack', github: 'rack/rack'
gem 'i18n', github: 'svenfuchs/i18n'
gem 'sqlite3'
GEMFILE
@codeodor
codeodor / reproduce_rails_issue_17119.rb
Last active August 29, 2015 14:07
A bug report script to reproduce issue #17119 in rails/rails
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'rack', github: 'rack/rack'
gem 'i18n', github: 'svenfuchs/i18n'
gem 'sqlite3'
GEMFILE
@codeodor
codeodor / tournament_seeding.rb
Created May 1, 2015 12:56
tournament seeding
teams = (1..16).to_a
# => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
games = (0..7).inject([]){|g, i| g << [teams[i], teams[-i-1]] }
# => [[1, 16], [2, 15], [3, 14], [4, 13], [5, 12], [6, 11], [7, 10], [8, 9]]
# so now, how do I transform that to:
# => [[1, 16], [8, 9], [5, 12], [4, 13], [6, 11], [3, 14], [7, 10], [2, 15]]
# In essence, I have the teams already sorted into the games of who plays who in this round.
# But I want to order the games so they appear next to who will play the winners of this round in the next round.
<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(){
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 )
@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.
@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 / 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 / .rspec
Created July 14, 2012 21:57 — forked from coreyhaines/.rspec
Loading just active record
--colour
-I app
@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