def self.isFavorite(userId,objId,objClass)
if objClass == "Yellowpage"
yp = self.find(:first, :conditions => ["user_id =? and yellowpage_id =?", userId,objId])
elsif objClass == "Podcast"
yp = self.find(:first, :conditions => ["user_id =? and podcast_id =?", userId,objId])
elsif objClass == "Howtoguide"
yp = self.find(:first, :conditions => ["user_id =? and howtoguide_id =?", userId,objId])
elsif objClass == "Resource"
yp = self.find(:first, :conditions => ["user_id =? and resource_id =?", userId,objId])
else
yp = nil
end
end
def self.isFavorite(userId,objId,objClass)
if ["Yellowpage","Podcast","Howtoguide","Resource"].include?(objClass)
class_id = objClass.downcase << "_id"
yp = self.find(:first, :conditions => ["user_id =? and #{class_id} =?", userId,objId])
else
yp = nil
end
end
Refactorings
No refactoring yet !
Adam
August 19, 2008, August 19, 2008 15:43, permalink
Use script/performance/benchmarker to call each version. However, I would suggest that you only worry about the quality of the code here. The performance differences will be negligible.
Also, I present to you an additional refactoring:
def self.favorite?(object)
if reflection = object.class.reflect_on_association(:favorites)
send("find_by_#{reflection.primary_key_name}", object)
end
end
# Example usage:
# script/performance/benchmarker 50 "User.first.favorites.favorite?(Yellowpage.first)"
How I should benchmark following methods.