Index: lib/identity.rb =================================================================== --- lib/identity.rb (revision 8) +++ lib/identity.rb (working copy) @@ -8,11 +8,11 @@ if method_sym.to_s =~ Regexp.new("^favorite_(\\w+)") favorite_class = ($1).singularize.classify.constantize favorite_class.find(:all, :include => :favorites, - :conditions => ['user_id = ? AND favorable_type = ?', + :conditions => ['favorites.user_id = ? AND favorites.favorable_type = ?', id, favorite_class.to_s ] ) elsif method_sym.to_s =~ Regexp.new("^has_favorite_(\\w+)\\?") favorite_class = ($1).singularize.classify.constantize - Favorite.count( :include => :user, :conditions => [ 'user_id = ? AND favorable_type = ?', + Favorite.count( :include => :user, :conditions => [ 'favorites.user_id = ? AND favorites.favorable_type = ?', id, favorite_class.to_s ] ) != 0 else super Index: lib/acts_as_favorite.rb =================================================================== --- lib/acts_as_favorite.rb (revision 8) +++ lib/acts_as_favorite.rb (working copy) @@ -33,26 +33,38 @@ def has_favorite( favorite_obj ) favorite = get_favorite( favorite_obj ) if favorite.nil? - favorite = Favorite.create( :favorable_type => favorite_obj.class.to_s, + favorite = Favorite.create( :user_id => self.id, + :favorable_type => favorite_obj.class.to_s, :favorable_id => favorite_obj.id ) end - self.favorites << favorite if not self.favorites.include?( favorite ) favorite end # Removes an object from the users favorites def has_no_favorite( favorite_obj ) favorite = get_favorite ( favorite_obj ) - self.favorites.delete( favorite ) if favorite + + # ACA: The original plugin did not properly destroy the favorite. + # Instead, it just removed the element from the favorites list. If the + # favorites list was refetched from the DB, surprise! It came back! + + if favorite + self.favorites.delete( favorite ) + favorite_obj.favorites.delete( favorite ) + favorite.destroy + end end private # Returns a favorite def get_favorite( favorite_obj ) + # ACA: The original plugin did not incoorporate the user id. This + # meant that the has_many associations did not find favorites + # properly. Favorite.find( :first, - :conditions => [ 'favorable_type = ? AND favorable_id = ?', - favorite_obj.class.to_s, favorite_obj.id ] ) + :conditions => [ 'user_id = ? AND favorable_type = ? AND favorable_id = ?', + self.id, favorite_obj.class.to_s, favorite_obj.id ] ) end end end @@ -67,21 +79,11 @@ def acts_as_favorite has_many :favorites, :as => :favorable has_many :favorite_users, :through => :favorites, :source => :user + + # ACA: The original plugin included unnecessary repetitions of the + # instance method definitions as well as the below include. - def accepts_favorite?( user_obj ) - user_obj.has_favorite?( self ) - end - - def accepts_favorite( user_obj ) - user_obj.has_favorite( self ) - end - - def accepts_no_favorite( user_obj ) - user_obj.has_no_favorite( self ) - end - - include ActsAsFavorite::ModelExtensions::InstanceMethods - + include ActsAsFavorite::ModelExtensions::InstanceMethods end end