refactored favorites
This commit is contained in:
@@ -6,4 +6,26 @@ class Favorite < ActiveRecord::Base
|
|||||||
def self.destroy_all(hash)
|
def self.destroy_all(hash)
|
||||||
connection.execute("delete from favorites_#{hash[:user_id] % 100} where user_id = #{hash[:user_id]} and post_id = #{hash[:post_id]}")
|
connection.execute("delete from favorites_#{hash[:user_id] % 100} where user_id = #{hash[:user_id]} and post_id = #{hash[:post_id]}")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.add(post, user)
|
||||||
|
return if Favorite.for_user(user.id).exists?(:user_id => user.id, :post_id => post.id)
|
||||||
|
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_privileged?
|
||||||
|
post.append_user_to_fav_string(user.id)
|
||||||
|
user.add_favorite!(post)
|
||||||
|
user.increment!(:favorite_count)
|
||||||
|
post.add_favorite!(user)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.remove(post, user)
|
||||||
|
return unless Favorite.for_user(user.id).exists?(:user_id => user.id, :post_id => post.id)
|
||||||
|
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_privileged?
|
||||||
|
post.delete_user_from_fav_string(user.id)
|
||||||
|
user.remove_favorite!(post)
|
||||||
|
user.decrement!(:favorite_count)
|
||||||
|
post.remove_favorite!(user)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -525,11 +525,7 @@ class Post < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def add_favorite!(user)
|
def add_favorite!(user)
|
||||||
return if favorited_by?(user.id)
|
Favorite.add(self, user)
|
||||||
append_user_to_fav_string(user.id)
|
|
||||||
Post.connection.execute_sql("update posts set fav_count = fav_count + 1 where id = #{id}")
|
|
||||||
Post.connection.execute_sql("update posts set score = score + 1 where id = #{id}") if CurrentUser.is_privileged?
|
|
||||||
user.add_favorite!(self)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_user_from_fav_string(user_id)
|
def delete_user_from_fav_string(user_id)
|
||||||
@@ -537,11 +533,7 @@ class Post < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def remove_favorite!(user)
|
def remove_favorite!(user)
|
||||||
return unless favorited_by?(user.id)
|
Favorite.remove(self, user)
|
||||||
Post.connection.execute_sql("update posts set fav_count = fav_count - 1 where id = #{id}")
|
|
||||||
Post.connection.execute_sql("update posts set score = score - 1 where id = #{id}") if CurrentUser.is_privileged?
|
|
||||||
delete_user_from_fav_string(user.id)
|
|
||||||
user.remove_favorite!(self)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def favorited_user_ids
|
def favorited_user_ids
|
||||||
|
|||||||
@@ -204,17 +204,11 @@ class User < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def add_favorite!(post)
|
def add_favorite!(post)
|
||||||
return if Favorite.for_user(id).exists?(:user_id => id, :post_id => post.id)
|
Favorite.add(post, self)
|
||||||
Favorite.create(:user_id => id, :post_id => post.id)
|
|
||||||
increment!(:favorite_count)
|
|
||||||
post.add_favorite!(self)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_favorite!(post)
|
def remove_favorite!(post)
|
||||||
return unless Favorite.for_user(id).exists?(:user_id => id, :post_id => post.id)
|
Favorite.remove(post, self)
|
||||||
Favorite.destroy_all(:user_id => id, :post_id => post.id)
|
|
||||||
decrement!(:favorite_count)
|
|
||||||
post.remove_favorite!(self)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
9
script/fixes/013.rb
Normal file
9
script/fixes/013.rb
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment'))
|
||||||
|
|
||||||
|
ActiveRecord::Base.connection.execute("set statement_timeout = 0")
|
||||||
|
|
||||||
|
Post.select("id, score, up_score, down_score, fav_count").find_each do |post|
|
||||||
|
post.update_column(:score, post.up_score - post.down_score + post.fav_count)
|
||||||
|
end
|
||||||
@@ -713,7 +713,7 @@ class PostTest < ActiveSupport::TestCase
|
|||||||
should "not decrement the post's score for basic users" do
|
should "not decrement the post's score for basic users" do
|
||||||
@member = FactoryGirl.create(:user)
|
@member = FactoryGirl.create(:user)
|
||||||
CurrentUser.scoped(@member, "127.0.0.1") do
|
CurrentUser.scoped(@member, "127.0.0.1") do
|
||||||
@post.remove_favorite!(@user)
|
@post.remove_favorite!(@member)
|
||||||
end
|
end
|
||||||
@post.reload
|
@post.reload
|
||||||
assert_equal(1, @post.score)
|
assert_equal(1, @post.score)
|
||||||
@@ -757,7 +757,7 @@ class PostTest < ActiveSupport::TestCase
|
|||||||
should "not increment the post's score for basic users" do
|
should "not increment the post's score for basic users" do
|
||||||
@member = FactoryGirl.create(:user)
|
@member = FactoryGirl.create(:user)
|
||||||
CurrentUser.scoped(@member, "127.0.0.1") do
|
CurrentUser.scoped(@member, "127.0.0.1") do
|
||||||
@post.add_favorite!(@user)
|
@post.add_favorite!(@member)
|
||||||
end
|
end
|
||||||
@post.reload
|
@post.reload
|
||||||
assert_equal(0, @post.score)
|
assert_equal(0, @post.score)
|
||||||
|
|||||||
@@ -187,6 +187,7 @@ class UploadTest < ActiveSupport::TestCase
|
|||||||
@upload = FactoryGirl.create(:source_upload)
|
@upload = FactoryGirl.create(:source_upload)
|
||||||
assert_difference("CurrentUser.post_upload_count", 1) do
|
assert_difference("CurrentUser.post_upload_count", 1) do
|
||||||
@upload.process!
|
@upload.process!
|
||||||
|
puts @upload.errors.full_messages
|
||||||
CurrentUser.reload
|
CurrentUser.reload
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user