Class: Favorite

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/favorite.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_user_or_ip_address(favoritable = nil, user = nil, remote_ip = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'app/models/favorite.rb', line 36

def self.find_by_user_or_ip_address(favoritable = nil, user = nil, remote_ip = nil)
  return false unless favoritable && (user || remote_ip)
  
  if user
    favorite = self.find(:first, :conditions => ["user_id = ? AND favoritable_type = ? AND favoritable_id = ?", user.id, favoritable.class.to_s, favoritable.id])
  else
    favorite = self.find(:first, :conditions => ["ip_address = ? AND favoritable_type = ? AND favoritable_id = ?", remote_ip, favoritable.class.to_s, favoritable.id])      
  end
  return favorite
end

.find_favorites_by_user(user) ⇒ Object



29
30
31
32
33
34
# File 'app/models/favorite.rb', line 29

def self.find_favorites_by_user(user)
  find(:all,
    :conditions => ["user_id = ?", user.id],
    :order => "created_at DESC"
  )
end

Instance Method Details

#update_counter_on_favoritableObject



23
24
25
26
27
# File 'app/models/favorite.rb', line 23

def update_counter_on_favoritable
  if favoritable && favoritable.respond_to?(:favorited_count)
    favoritable.update_attribute(:favorited_count, favoritable.favorites.count.to_s )
  end
end