This commit is contained in:
r888888888
2014-04-14 14:32:01 -07:00
parent 7a61048d69
commit fad0ab7c93
105 changed files with 610 additions and 485 deletions

View File

@@ -1,6 +1,7 @@
class Favorite < ActiveRecord::Base
belongs_to :post
scope :for_user, lambda {|user_id| where("user_id % 100 = #{user_id.to_i % 100} and user_id = #{user_id.to_i}")}
attr_accessible :user_id, :post_id
# this is necessary because there's no trigger for deleting favorites
def self.destroy_all(hash)
@@ -14,11 +15,11 @@ class Favorite < ActiveRecord::Base
def self.add(post, user)
Favorite.transaction do
return if Favorite.for_user(user.id).where(:user_id => user.id, :post_id => post.id).exists?
Favorite.create(:user_id => user.id, :post_id => post.id)
Post.update_all("fav_count = fav_count + 1", {:id => post.id})
Post.update_all("score = score + 1", {:id => post.id}) if user.is_gold?
Favorite.create!(:user_id => user.id, :post_id => post.id)
Post.where(:id => post.id).update_all("fav_count = fav_count + 1")
Post.where(:id => post.id).update_all("score = score + 1") if user.is_gold?
post.append_user_to_fav_string(user.id)
User.update_all("favorite_count = favorite_count + 1", {:id => user.id})
User.where(:id => user.id).update_all("favorite_count = favorite_count + 1")
user.favorite_count += 1
# post.fav_count += 1 # this is handled in Post#clean_fav_string!
post.score += 1 if user.is_gold?
@@ -29,10 +30,10 @@ class Favorite < ActiveRecord::Base
Favorite.transaction do
return unless Favorite.for_user(user.id).where(:user_id => user.id, :post_id => post.id).exists?
Favorite.destroy_all(:user_id => user.id, :post_id => post.id)
Post.update_all("fav_count = fav_count - 1", {:id => post.id})
Post.update_all("score = score - 1", {:id => post.id}) if user.is_gold?
Post.where(:id => post.id).update_all("fav_count = fav_count - 1")
Post.where(:id => post.id).update_all("Score = score - 1") if user.is_gold?
post.delete_user_from_fav_string(user.id)
User.update_all("favorite_count = favorite_count - 1", {:id => user.id})
User.where(:id => user.id).update_all("favorite_count = favorite_count - 1")
user.favorite_count -= 1
post.fav_count -= 1
post.score -= 1 if user.is_gold?