fixed tests, implemented sql based partitioning for favorites
This commit is contained in:
@@ -1,22 +1,4 @@
|
||||
class Favorite < ActiveRecord::Base
|
||||
TABLE_COUNT = 100
|
||||
belongs_to :post
|
||||
|
||||
scope :for_user, lambda {|user_id| where("user_id = ?", user_id)}
|
||||
|
||||
def self.model_for(user_id)
|
||||
mod = user_id.to_i % TABLE_COUNT
|
||||
Object.const_get("Favorite#{mod}")
|
||||
end
|
||||
|
||||
def self.delete_post(post_id)
|
||||
0.upto(TABLE_COUNT - 1) do |i|
|
||||
model_for(i).destroy_all(:post_id => post_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
0.upto(Favorite::TABLE_COUNT - 1) do |i|
|
||||
Object.const_set("Favorite#{i}", Class.new(Favorite))
|
||||
Object.const_get("Favorite#{i}").set_table_name("favorites_#{i}")
|
||||
end
|
||||
|
||||
@@ -389,7 +389,7 @@ class Post < ActiveRecord::Base
|
||||
|
||||
module FavoriteMethods
|
||||
def delete_favorites
|
||||
Favorite.delete_post(id)
|
||||
Favorite.delete_all(:post_id => id)
|
||||
end
|
||||
|
||||
def favorited_by?(user_id)
|
||||
|
||||
@@ -5,12 +5,12 @@ class User < ActiveRecord::Base
|
||||
class PrivilegeError < Exception ; end
|
||||
|
||||
module Levels
|
||||
MEMBER = 0
|
||||
PRIVILEGED = 100
|
||||
CONTRIBUTOR = 200
|
||||
JANITOR = 300
|
||||
MODERATOR = 400
|
||||
ADMIN = 500
|
||||
MEMBER = 20
|
||||
PRIVILEGED = 30
|
||||
CONTRIBUTOR = 33
|
||||
JANITOR = 35
|
||||
MODERATOR = 40
|
||||
ADMIN = 50
|
||||
end
|
||||
|
||||
attr_accessor :password, :old_password
|
||||
@@ -128,18 +128,18 @@ class User < ActiveRecord::Base
|
||||
|
||||
module FavoriteMethods
|
||||
def favorites
|
||||
Favorite.model_for(id).where("user_id = ?", id).order("id desc")
|
||||
Favorite.where("user_id = ?", id).order("id desc")
|
||||
end
|
||||
|
||||
def add_favorite!(post)
|
||||
return if Favorite.model_for(id).exists?(:user_id => id, :post_id => post.id)
|
||||
Favorite.model_for(id).create(:user_id => id, :post_id => post.id)
|
||||
return if Favorite.exists?(:user_id => id, :post_id => post.id)
|
||||
Favorite.create(:user_id => id, :post_id => post.id)
|
||||
post.add_favorite!(self)
|
||||
end
|
||||
|
||||
def remove_favorite!(post)
|
||||
return unless Favorite.model_for(id).exists?(:user_id => id, :post_id => post.id)
|
||||
Favorite.model_for(id).destroy_all(:user_id => id, :post_id => post.id)
|
||||
return unless Favorite.exists?(:user_id => id, :post_id => post.id)
|
||||
Favorite.destroy_all(:user_id => id, :post_id => post.id)
|
||||
post.remove_favorite!(self)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user