Skip to content

Instantly share code, notes, and snippets.

View awesome's full-sized avatar

So Awesome Man awesome

View GitHub Profile
@nesquena
nesquena / jbuilder.rb
Created February 14, 2012 01:15
JBuilder and RABL Syntax Comparison
Jbuilder.encode do |json|
json.content format_content(@message.content)
json.(@message, :created_at, :updated_at)
json.author do |json|
json.name @message.creator.name.familiar
json.email_address @message.creator.email_address_with_name
json.url url_for(@message.creator, format: :json)
end
@tjh
tjh / character_set_and_collation.rb
Created January 31, 2012 16:07
Convert all Rails table column collation and character set
#!/usr/bin/env ruby
# Put this file in the root of your Rails project,
# then run it to output the SQL needed to change all
# your tables and columns to the same character set
# and collation.
#
# > ruby character_set_and_collation.rb
DATABASE = ''
@milligramme
milligramme / bash_completion
Created January 20, 2012 08:13
brew install bash-completion
$ brew install bash-completion
==> Downloading http://bash-completion.alioth.debian.org/files/bash-completion-1.3.tar.bz2
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/bash-completion/1.3
==> make install
==> Caveats
Add the following lines to your ~/.bash_profile file:
if [ -f `brew --prefix`/etc/bash_completion ]; then
. `brew --prefix`/etc/bash_completion
fi
@juliepagano
juliepagano / jasmine_setup.rake
Created January 3, 2012 21:38
Jasmine & Rails 3.1 asset pipeline don't play well together; a workaround
task :copy_jquery_files do
# Jasmine doesn't work with the asset pipeline; therefore we copy the jquery
# files manually.
spec = Gem::Specification.find_by_name("jquery-rails")
jquery_dir = Pathname.new(Gem.dir) +
"gems" +
"#{spec.name}-#{spec.version}" +
"vendor" +
"assets" +
"javascripts"
@ihower
ihower / gist:1513416
Created December 23, 2011 06:51
rails/ruby/jruby/passenger/Trinidad benchmark
= httperf --server localhost --port 3000 --uri /render_text --num-conns 500 --num-calls 20
== Ruby 1.9.2/WEBrick
Request rate: 329.5 req/s (3.0 ms/req)
Request rate: 334.6 req/s (3.0 ms/req)
== Ruby 1.9.2/Passenger
Request rate: 539.5 req/s (1.9 ms/req)
Request rate: 543.1 req/s (1.8 ms/req)
@afternoon
afternoon / git-slim.py
Created December 5, 2011 14:42
Remove large objects from a git repository
#!/usr/bin/python
#
# git-slim
#
# Remove big files from git repo history.
#
# Requires GitPython (https://github.com/gitpython-developers/GitPython)
#
# References:
# - http://help.github.com/remove-sensitive-data/
@jpmckinney
jpmckinney / email_validator.rb
Created November 12, 2011 17:36
Email validation in Rails 3 or Active Model without regexp
# blog post: http://blog.slashpoundbang.com/post/12694519460/email-validation-in-rails-3-or-active-model-without
# http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/
class EmailValidator < ActiveModel::EachValidator
# Domain must be present and have two or more parts.
def validate_each(record, attribute, value)
address = Mail::Address.new value
record.errors[attribute] << (options[:message] || 'is invalid') unless (address.address == value && address.domain && address.__send__(:tree).domain.dot_atom_text.elements.size > 1 rescue false)
end
end
@bradland
bradland / datebench.rb
Created November 9, 2011 20:57
Date bench
#!/usr/bin/env ruby
require 'benchmark'
require 'date'
Benchmark.bm do|b|
ITERATE = 100000
b.report("strptime") do
time = "2011-05-25 00:00:03 -0400"
fmt = "%Y-%m-%d %H:%M:%S %z"
@tonywok
tonywok / results.md
Created September 15, 2011 01:33
Rails 3.1 Callbacks
s = Test.new(:name => "foo")

# ActiveRecord::Test after_initialize
# ActiveRecord::Test after_initialize, :on => :update
# ActiveRecord::Test after_initialize, :on => :create
# ActiveRecord::TestObserver after_initialize

# ===> #<Test id: nil, name: "foo", created_at: nil, updated_at: nil>
@ferblape
ferblape / gist:1189798
Created September 2, 2011 20:22
Fakeweb POST request tests
def test_register_uri_for_post_method_with_parameters
FakeWeb.register_uri(:post, "http://example.com/users", :body => "User list 1", :parameters => {:p1 => 'v1'})
FakeWeb.register_uri(:post, "http://example.com/users", :body => "User list 2", :parameters => :any)
assert_equal 2, FakeWeb::Registry.instance.uri_map.values.first[:post].size
assert FakeWeb.registered_uri?(:post, "http://example.com/users", :parameters => {:p1 => 'v1'})
assert FakeWeb.registered_uri?(:post, "http://example.com/users", :parameters => :any)
end