Skip to content

Instantly share code, notes, and snippets.

View raphaelcm's full-sized avatar

Raphael Crawford-Marks raphaelcm

View GitHub Profile
@raphaelcm
raphaelcm / mergesort.rb
Created April 14, 2013 21:53
O(n*log n) sorting algorithms: Quicksort and Mergesort. Implemented in Ruby
module Mergesort
def self.sort(array)
return array if array.length <= 1
middle = array.length/2
left = sort(array[0..(middle-1)])
right = sort(array[middle..-1])
merge(left, right)
end
@raphaelcm
raphaelcm / Gemfile
Last active December 15, 2015 22:08
Integrating with LinkedIn using Rails, Mongoid, Devise, Omniauth, LinkedIn Gem
# Gemfile
gem "linkedin"
gem "omniauth"
gem "omniauth-linkedin"
@raphaelcm
raphaelcm / persistence_test.rb
Created October 3, 2012 21:22
persistence bug repro case
class PersistenceTest
include Mongoid::Document
include Mongoid::Timestamps
BRAINTREE_SYNC_STATUSES = %w(new synced changed)
#
# Top-level Fields
#
field :terms_accepted_at, :type => DateTime
@raphaelcm
raphaelcm / dist.rb
Created June 22, 2012 12:28
print out characters according to their relative frequencies
#!/usr/bin/env ruby
inputs = {
'a' => 18,
'b' => 13,
'c' => 9
}
total = inputs.values.inject{|sum,x| sum + x }
@raphaelcm
raphaelcm / slow_rails_queries
Created May 30, 2012 13:37
Search last N lines of Rails log for slow queries
tail -n 10000 log/development.log | egrep -n '(Load|SQL|AREL|Request) \([2-9][0-9]{2,}.[0-9]ms\)'
@raphaelcm
raphaelcm / migrate.rb
Created February 10, 2012 17:10
Migrate local Refinery images/resources to S3
#!/usr/bin/env ruby
# Migrate local Refinery images & resources to S3.
# Assumes you've set up an AWS::S3 connection
# elsewhere (I have an initializer that does it).
image_classes = [Image]
data_classes = [Resource]
def upload(path, file, bucket, retries = 3)
@raphaelcm
raphaelcm / School.rb
Created February 3, 2012 20:23
Display specific records first.
class School < ActiveRecord::Base
scope :show_first, lambda{|val| order("(case country when '#{val}' then 0 else 1 end)") }
end
@raphaelcm
raphaelcm / custom_url_exmaple.rb
Created October 11, 2011 17:08
Easy way to support custom (human-written) URLs (helpful with SEO) using friendly_id
class BlogPost < ActiveRecord::Base
# This Model has, in addition to title and other usual fields,
# two fields for creating custom urls:
#
# :use_custom_url, :boolean
# :custom_url, :string
validates :title, :presence => true, :uniqueness => true