Skip to content

Instantly share code, notes, and snippets.

View hooopo's full-sized avatar
🍏
I may be slow to respond.

Hooopo hooopo

🍏
I may be slow to respond.
View GitHub Profile
@hooopo
hooopo / Instrument Anything in Rails 3.md
Created March 15, 2012 13:26 — forked from mnutt/Instrument Anything in Rails 3.md
How to use Rails 3.0's new notification system to inject custom log events

Instrument Anything in Rails 3

With Rails 3.0 released a few weeks ago I've migrated a few apps and I'm constantly finding useful new improvements. One such improvement is the ability to log anything in the same way that Rails internally logs ActiveRecord and ActionView. By default Rails 3 logs look slightly spiffier than those produced by Rails 2.3: (notice the second line has been cleaned up)

Started GET "/" for 127.0.0.1 at Mon Sep 06 01:07:11 -0400 2010
  Processing by HomeController#index as HTML
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1
  CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1

Rendered layouts/_nav.html.erb (363.4ms)

@hooopo
hooopo / connection_fix.rb
Created March 21, 2012 06:40 — forked from eric/connection_fix.rb
MySQL server has gone away fix
# If your workers are inactive for a long period of time, they'll lose
# their MySQL connection.
#
# This hack ensures we re-connect whenever a connection is
# lost. Because, really. why not?
#
# Stick this in RAILS_ROOT/config/initializers/connection_fix.rb (or somewhere similar)
#
# From:
# http://coderrr.wordpress.com/2009/01/08/activerecord-threading-issues-and-resolutions/
@hooopo
hooopo / trace.rb
Created March 22, 2012 17:28
trace active_record call
set_trace_func proc { |event, file, line, id, binding, classname|
if classname.to_s =~ /ActiveRecord/i
s = sprintf("%8s %s:%-2d %10s %8s\n", event, file.split("/active_record/").last, line, id, classname)
ActiveRecord::Base.logger.info s
end
}
有人用过cell么?
<span style="font-size: small;"><a target="_blank" href="http://rubyforge.org/frs/?group_id=2978&amp;release_id=9650">http://rubyforge.org/frs/?group_id=2978&amp;release_id=9650</a>
<br /></span>
<br /><span style="font-size: small;"> </span>
<br /><span style="font-size: small;">
<br />非常小,源代码一目了然。感觉虽然有点土,但比自己做个module的方式好一点点
<br />号称: 代替发挥component作用的controller, 这样就不需要邪恶的render_component了
<br /></span>
<br /><span style="font-size: small;">A cell acts as a lightweight controller in the sense that it will assign variables and render a view.</span>
@hooopo
hooopo / deploy.rake
Created May 24, 2012 07:49
Manage and rollback Heroku deployments Capistrano-style
task :deploy do
puts 'Deploying site to Heroku ...'
puts `git push heroku`
puts 'Running database migrations ...'
puts `heroku rake db:migrate`
release_name = "release-#{Time.now.utc.strftime("%Y%m%d%H%M%S")}"
puts "Tagging release as '#{release_name}'"
puts `git tag -a #{release_name} -m 'Tagged release'`
@hooopo
hooopo / OPML
Created May 27, 2012 14:00
opml.xml
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
<head>
<title>Google 阅读器中 Hooopo 的订阅</title>
</head>
<body>
<outline text="(未知标题)" title="(未知标题)" type="rss"
xmlUrl="http://thoughtrails.com/feeds.atom" htmlUrl="http://thoughtrails.com"/>
<outline text="(未知标题)" title="(未知标题)" type="rss"
xmlUrl="http://confreaks.net/feeds2.feedburner.com/confreaks-general" htmlUrl="http://www.google.com/reader/view/feed%2Fhttp%3A%2F%2Fconfreaks.net%2Ffeeds2.feedburner.com%2Fconfreaks-general"/>
File.open("num.txt", "w+") do |file|
50_000_000.times do
num = (1..11).map{ rand(10).to_s}.join
file.puts num
end
end
@hooopo
hooopo / each_object
Created July 13, 2012 09:36
each_object
[8] pry(main)> class A;;end
=> nil
[9] pry(main)> a1 = A.new
=> #<A:0xab50838>
[10] pry(main)> a2 = A.new
=> #<A:0xa8871b0>
[11] pry(main)> ObjectSpace.each_object(A){|object| p object}
#<A:0xa8871b0>
#<A:0xab50838>
@hooopo
hooopo / fetch_by_name.rb
Created August 3, 2012 06:12
fetch_by_name
# -*- encoding : utf-8 -*-
module FetchByName
def fetch_by_name(name, field_name = :name)
if id = Rails.cache.read("cached_#{self.name}_#{field_name}_#{name}")
self.find(id)
else
record = self.where(field_name => name).first || raise(ActiveRecord::RecordNotFound)
record.tap{|record| Rails.cache.write("cached_#{self.name}_#{field_name}_#{name}", record.id)}
end
end
@hooopo
hooopo / README.markdown
Created August 8, 2012 14:12 — forked from greypants/README.markdown
RAILS 3: nav_link helper for adding 'selected' class to navigation elements

#Behold, the nav_link:

The nav_link helper works just like the standard Rails link_to helper, but adds a 'selected' class to your link (or its wrapper) if certain criteria are met. By default, if the link's destination url is the same url as the url of the current page, a default class of 'selected' is added to the link.

For full usage details, see: http://viget.com/extend/rails-selected-nav-link-helper

Drop nav_link_helper.rb into app/helpers in your Rails 3.x app and enjoy.