From cf523ca39dc2940e6460083107c941c57e460f49 Mon Sep 17 00:00:00 2001 From: Jörg Battermann Date: Thu, 25 Dec 2008 12:08:22 +0800 Subject: [PATCH] acts_as_followable.rb's followed_by? method is working (again?)... and simplified lookup there a lil bit.. no foreach iteration anymore --- lib/acts_as_followable.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/acts_as_followable.rb b/lib/acts_as_followable.rb index 6b6dce8..09a95c6 100644 --- a/lib/acts_as_followable.rb +++ b/lib/acts_as_followable.rb @@ -29,14 +29,18 @@ module ActiveRecord #:nodoc: end # Returns true if the current instance is followed by the passed record. - def followed_by?(follwer) - rtn = false - self.follows.each do |f| - rtn = true if follwer.id == f.follower_id && parent_class_name(follwer) == f.follower_type - end - rtn + def followed_by?(follower) + self.follows.find(:first, :conditions => ["follower_id = ? AND follower_type = ?", follower.id, parent_class_name(follower)]) ? true : false end + # Retrieves the parent class name if using STI. + def parent_class_name(obj) + if obj.class.superclass != ActiveRecord::Base + return obj.class.superclass.name + end + + return obj.class.name + end end end -- libgit2 0.21.0