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



31
32
33
34
35
36
37
38
39
40
# File 'app/models/favorite.rb', line 31

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.where("user_id = ? AND favoritable_type = ? AND favoritable_id = ?", user.id, favoritable.class.to_s, favoritable.id).first
  else
    favorite = self.where("ip_address = ? AND favoritable_type = ? AND favoritable_id = ?", remote_ip, favoritable.class.to_s, favoritable.id).first
  end
  return favorite
end

.find_favorites_by_user(user) ⇒ Object



27
28
29
# File 'app/models/favorite.rb', line 27

def self.find_favorites_by_user(user)
  where("user_id = ?", user.id).order("created_at DESC")
end

Instance Method Details

#update_counter_on_favoritableObject



21
22
23
24
25
# File 'app/models/favorite.rb', line 21

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