Skip to content

Instantly share code, notes, and snippets.

View gregoriokusowski's full-sized avatar

Gregório Chalinski Kusowski gregoriokusowski

View GitHub Profile
@gregoriokusowski
gregoriokusowski / runner.rb
Created February 21, 2011 14:23
a rails model used to create all "validates_numericality_of" validations based on the schema. The code sux a bit, and have a lot of smells, but is working by now...
class TheModel
def name
@model_name
end
def initialize(line)
@model_name = extract_name(line).to_s.singularize
end
@gregoriokusowski
gregoriokusowski / key_finder.rb
Created February 21, 2011 14:29
A helper that finds the informed word(when running in shell), and gives all the options, with the respectives I18n keys, like "activerecord.models.attributes.etc.name"
require 'yaml'
@sentence = ARGV.shift
def find(path, map)
map.each do |key, value|
if value.is_a? Hash
find("#{path}#{key}.", value)
else
puts "#{path}#{key} - #{value}" if value =~ /#{@sentence}/
@gregoriokusowski
gregoriokusowski / ubuntu.sh
Created March 2, 2011 13:45
with comments
#let the computer on, then closing the notebook lid
gconftool-2 --set '/apps/gnome-power-manager/buttons/lid_battery' --type string 'nothing'
gconftool-2 --set '/apps/gnome-power-manager/buttons/lid_ac' --type string 'nothing'
@gregoriokusowski
gregoriokusowski / download.rb
Created March 9, 2011 11:26
A simple ruby downloader.
require "open-uri"
#Ex.: Download.from("www.blablabla.com/my_download.txt").to("my_downloaded_file.txt")
class Download
def initialize(source)
@source = source
end
def self.from(source)
self.new(source)
end
@gregoriokusowski
gregoriokusowski / unzip.rb
Created March 9, 2011 11:34
A simple unzipper :)
#rubygem: rubyzip
require 'zip/zip'
#Ex.: Unzip.from("my_file.zip").to("my_folder")
class Unzip
def initialize(source)
@source = source
end
def self.from(source)
self.new(source)
@gregoriokusowski
gregoriokusowski / find_columns_by_type.rb
Created March 11, 2011 10:54
Method that searches an ActiveRecord::Base for it's columns by type.
#clazz must be the ActiveRecord::Base, and type must be a type symbol
def find_columns(clazz, type)
clazz.columns.select do |c|
c.type == type
end.collect do |c|
c.name
end
end
@gregoriokusowski
gregoriokusowski / spec_helper.rb
Created April 1, 2011 16:28
How to make Spork work with autotest and rspec. Using a sqlite3 in-memory db.
#how to set spork
require 'spork'
Spork.prefork do
#my old spec_helper.rb here ;)
end
Spork.each_run do
load_schema = lambda {
@gregoriokusowski
gregoriokusowski / gist:1174216
Created August 26, 2011 19:28
Spork config with sqlite3 test database and selenium specs.
# https://github.com/jnicklas/capybara/issues/450
# hack
ActiveRecord::ConnectionAdapters::ConnectionPool.class_eval do
def current_connection_id
# Thread.current.object_id
Thread.main.object_id
end
end
# https://github.com/timcharper/spork/wiki/troubleshooting
@gregoriokusowski
gregoriokusowski / chronno_trigger_ftw.rb
Created August 26, 2011 20:20
Avoids the AR default timestamp records.
# how to use(and abuse) it:
# chronno_trigger_ftw! { my_model_instance.update_attributes(:updated_at => my_custom_date) }
def chronno_trigger_ftw!(&block)
Customer.record_timestamps = false
yield
Customer.record_timestamps = true
end
# a threadsafe with ensure block:
@gregoriokusowski
gregoriokusowski / stupid_multi_tenancy_conversion.rb
Created September 14, 2011 19:40
A stupid script that may be used a single time to migrate an old data-base to one of those new rails multi-tenancy(scoped) apps. It's not pretty. Not even safe, so watchout =P
%x[ls -R app/models/].split("\n").each do |w|
begin
w[0...-3].camelize.constantize.unscoped.each{ |u| u.update_attribute(:company_id, Company.unscoped.first.id) }
rescue
end
end