May 2007 Archives

Book of Duck Typing (fake)

| 1 Comment | No TrackBacks
fake ora book


Made with http://www.monkeyboy.is-a-geek.org/oracover.jsp , a funny O'REALLY book cover generator.

I like generators.

Labels: , ,

(嗯。可是,的確是非常顯眼啊。)

Jobs about this rails frameework.

| No Comments | No TrackBacks
Recenty used Rails for $work, and came up this function to make it more convenient to retrieve remote records in a many-to-many relationship with a join-table used to saved the relationship and it'll takes a .map-conversion away to get the actual records.

Here's what we came up:


module AsynapseSupport
def has_(prefix, item)
prefix = prefix.to_s
item = item.to_s

sy = "#{prefix}_#{item}"
mo = sy.camelcase.singularize

self.class_eval <
has_many :_#{sy}, :class_name => "#{mo}"
def #{sy}
_#{sy}.map { |f| f.#{item.singularize} }
end
EOE
end
end

If your mixin this module in your ActiveRecord::Base, then you can say this in your user.rb model class:

  has_ :favorite, :chatrooms

It'll make an favorite_chatrooms method to retrieve a list of "Chatroom" instead of retrieving a list of "FavoriteChatroom", which is the behaviour if you say:

  has_many :favorite_chatrooms

Our has_ method, although seems to be a controversial name, is pretty handy to deal with many-to-many relationship.

Project asynapse: http://code.google.com/p/asynapse/