Module: ActsAsFavorite::UserExtensions::InstanceMethods

Defined in:
lib/acts_as_favorite.rb

Instance Method Summary collapse

Instance Method Details

#all_favoritesObject

Returns a polymorphic array of all user favorites



22
23
24
# File 'lib/acts_as_favorite.rb', line 22

def all_favorites
  self.favorites.map{|f| f.favorable }
end

#has_favorite(favorite_obj) ⇒ Object

Sets the object as a favorite of the users



33
34
35
36
37
38
39
40
41
# File 'lib/acts_as_favorite.rb', line 33

def has_favorite( favorite_obj )
  favorite = get_favorite( favorite_obj )
  if favorite.nil?
    favorite = Favorite.create( :user_id => self.id,
                                :favorable_type => favorite_obj.class.to_s,
                                :favorable_id   => favorite_obj.id )
  end
  favorite
end

#has_favorite?(favorite_obj) ⇒ Boolean

Returns trur/false if the provided object is a favorite of the users

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/acts_as_favorite.rb', line 27

def has_favorite?( favorite_obj )
  favorite = get_favorite( favorite_obj )
  favorite ? self.favorites.exists?( favorite.id ) : false
end

#has_no_favorite(favorite_obj) ⇒ Object

Removes an object from the users favorites



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/acts_as_favorite.rb', line 44

def has_no_favorite( favorite_obj )
  favorite = get_favorite(favorite_obj)

  # 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