Skip to content

Instantly share code, notes, and snippets.

View trak3r's full-sized avatar

Thomas "Teflon Ted" Davis trak3r

View GitHub Profile
def has_role?(role)    
# if list isn't already assigned to something, create an array of the names of my roles
list ||= self.roles.map(&:name)    
# if list includes the string representation of the passed-in role [assumed to be the name] or if the list includes the string 'admin' [assumed to be the administrative role] return true
list.include?(role.to_s) || list.include?('admin')  
end
# from: http://pivotallabs.com/users/alex/blog/articles/865-monkey-patch-du-jour
module ActiveRecord
module ConnectionAdapters
class AbstractAdapter
def log_info(sql, name, ms)
if @logger && @logger.debug?
@logger.debug("#{sql}, #{name}, #{ms}")
c = caller.detect{|line| line !~ /(activerecord|active_support|__DELEGATION__|\/lib\/|\/vendor\/plugins|\/vendor\/gems)/i}
c.gsub!("#{File.expand_path(File.dirname(RAILS_ROOT))}/", '') if defined?(RAILS_ROOT)
name = '%s (%.1fms) %s' % [name || 'SQL', ms, c]
def self.included(receiver)
receiver.class_eval <<-RUBY
named_scope :latest_before, lambda {|time| {:conditions => ['created_at < ?', time]}}
RUBY
end
@trak3r
trak3r / gist:134143
Created June 22, 2009 19:29
assert_close_enough
def assert_close_enough(target, actual, message = nil)
lower = (0.9 * target.to_f).floor
upper = (1.1 * target.to_f).ceil
full_message = build_message(message,
"<?-?> expected but was\n<?>.\n", lower, upper, actual)
assert_block(full_message) { actual >= lower and actual <= upper }
end
@trak3r
trak3r / gist:140552
Created July 4, 2009 12:17
all my gems
sudo gem install ZenTest actionmailer actionpack activerecord activeresource activesupport autotest-fsevent autotest-rails beanstalk-client capistrano capistrano-ext columnize ctran-annotate curb engineyard-eycap fastercsv fastthread highline hoe httpclient libxml-ruby linecache mysql net-scp net-sftp net-ssh net-ssh-gateway nokogiri passenger plist rack rails rake redgreen rspec rspec-rails ruby-debug ruby-debug-base rubyforge rubyist-aasm sevenwire-forgery soap4r sys-uname taf2-curb taf2-rmem thoughtbot-factory_girl
@trak3r
trak3r / gist:143349
Created July 9, 2009 01:24
where ruby hides on os x
lrwxr-xr-x 1 root wheel 76 May 21 05:31 /usr/bin/ruby -> ../../System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby
ted@ceylon /usr/bin »ls -al | grep -i ruby
lrwxr-xr-x 1 root wheel 75 May 21 05:31 erb -> ../../System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/erb
lrwxr-xr-x 1 root wheel 71 May 21 05:31 gem -> ../../System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/gem
lrwxr-xr-x 1 root wheel 75 May 21 05:31 irb -> ../../System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/irb
-rwxr-xr-x 1 root wheel 435 Jul 4 08:14 multiruby
-rwxr-xr-x 1 root wheel 441 Jul 4 08:14 multiruby_setup
lrwxr-xr-x 1 root wheel 76 May 21 05:31 rdoc -> ../../System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/rdoc
lrwxr-xr-x 1 root wheel 74 May 21 05:31 ri -> ../../System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ri
lrwxr-xr-x 1 root wheel 71 Jul 8 22:13 ruby -> /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby
-rwxr-xr-x 1 root w
@trak3r
trak3r / gist:143618
Created July 9, 2009 12:34
all my gems with versions
*** LOCAL GEMS ***
actionmailer (2.3.2, 1.3.6)
actionpack (2.3.2, 1.13.6)
actionwebservice (1.2.6)
activerecord (2.3.2, 1.15.6)
activeresource (2.3.2)
activesupport (2.3.2, 1.4.4)
acts_as_ferret (0.4.4, 0.4.1)
autotest-fsevent (0.1.1)
@trak3r
trak3r / gist:144114
Created July 10, 2009 00:05
thread bug in capistrano
# threads = Array(servers).map { |server| establish_connection_to(server, failed_servers) }
# threads.each { |t| t.join }
Array(servers).each { |server| safely_establish_connection_to(server, Thread.current, failed_servers) }
@trak3r
trak3r / gist:147115
Created July 14, 2009 19:08
remove minutes/seconds from time
class Time
def no_minutes
return self - self.min.minutes - self.sec.seconds
end
end