Skip to content

Instantly share code, notes, and snippets.

View metalelf0's full-sized avatar

Andrea Schiavini metalelf0

View GitHub Profile
@metalelf0
metalelf0 / gist:702423
Created November 16, 2010 20:15
MysqlQueryResultsFormatter
module MysqlQueryResultsFormatter
require 'terminal-table/import'
def print_results_of_query query
result = ActiveRecord::Base.connection.execute(query)
return nil if result.nil?
results_table = table do |t|
results = result.all_hashes
t.headings = results.first.keys
results.each do |each_row|
@metalelf0
metalelf0 / patterns.txt
Created November 16, 2010 20:17
TextMate patterns example
{ scopeName = 'todo';
patterns = (
{ name = 'todo.completed';
match = '^\[X+\]';
},
{ name = 'todo.in_progress';
match = '^\[X*\.*\]';
},
{ name = 'todo.new';
match = '^\[.+]';
@metalelf0
metalelf0 / mixin_vs_monkey_patch.rb
Created November 16, 2010 20:20
Mixin VS Monkey patching
######### MIXIN
module SplittableArray
def split_by_half
middle = (self.size.to_f / 2).floor
return [self[0..middle], self[middle+1..self.size]]
end
end
some_array = [1, 2, 3, 4, 5]
@metalelf0
metalelf0 / habtm_scope.rb
Created January 28, 2011 13:39
How to define a named scope in a habtm relation
# First model: tag.rb
# note that pomodori is a custom plural for pomodoro
class Tag < ActiveRecord::Base
has_and_belongs_to_many :pomodori
end
# Second model: pomodoro.rb
# here is how to define a Rails 3 scope through the join table:
ENV["WATCHR"] = "1"
system 'clear'
def growl(message)
growlnotify = `which growlnotify`.chomp
title = "Watchr Test Results"
puts message
image = message.match(/\s0\s(errors|failures)/) ? "~/.watchr_images/passed.png" : "~/.watchr_images/failed.png"
options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{message}' '#{title}'"
system %(#{growlnotify} #{options} &)
@metalelf0
metalelf0 / gist:840314
Created February 23, 2011 11:28
Autotest notifier
module Autotest::Notify
def self.notify title, msg, img, pri='low', time=3000
`notify-send -i #{img} -u #{pri} -t #{time} '#{msg}'`
end
Autotest.add_hook :initialize do |at|
%w{.svn .hg .git vendor}.each {|exception| at.add_exception(exception)}
end
Autotest.add_hook :ran_command do |autotest|
require 'rubygems'
require 'stringex'
strings = ["Bolesława", "Poznań", "Stańczyk", "Šaković" ]
strings.each do |string|
puts "#{string} => #{string.to_ascii}"
end
# Bolesława => Boleslawa
# Poznań => Poznan
@metalelf0
metalelf0 / rollback.rb
Created March 10, 2011 13:54
Snippet to rollback DB state after a ruby script
ActiveRecord::Base.connection.increment_open_transactions
ActiveRecord::Base.connection.begin_db_transaction
at_exit do
ActiveRecord::Base.connection.rollback_db_transaction
ActiveRecord::Base.connection.decrement_open_transactions
end
# person.rb
validates_presence_of :address
# person_factory.rb
Factory.define :person do |p|
p.name 'John'
p.surname 'Doe'
p.association :address
AREL (0.5ms) INSERT INTO "addresses" ("country", "planet", "created_at",
"updated_at") VALUES ('Italy', 'Earth', '2011-06-18 16:45:00.268423',
'2011-06-18 16:45:00.268423')