fixed user tests
This commit is contained in:
20
app/models/favorite.rb
Normal file
20
app/models/favorite.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
class Favorite < ActiveRecord::Base
|
||||
TABLE_COUNT = 100
|
||||
validates_uniqueness_of :post_id, :scope => :user_id
|
||||
|
||||
def self.model_for(user_id)
|
||||
mod = user_id % 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
|
||||
@@ -373,13 +373,17 @@ class Post < ActiveRecord::Base
|
||||
|
||||
module FavoriteMethods
|
||||
def delete_favorites
|
||||
Favorite.destroy_for_post(self)
|
||||
Favorite.delete_post(id)
|
||||
end
|
||||
|
||||
def favorited_by?(user_id)
|
||||
fav_string =~ /(?:\A| )fav:#{user_id}(?:\Z| )/
|
||||
end
|
||||
|
||||
def append_user_to_fav_string(user_id)
|
||||
update_attribute(:fav_string, (fav_string + " fav:#{user_id}").strip)
|
||||
end
|
||||
|
||||
def add_favorite(user_id)
|
||||
if user_id.is_a?(ActiveRecord::Base)
|
||||
user_id = user_id.id
|
||||
@@ -389,10 +393,13 @@ class Post < ActiveRecord::Base
|
||||
return false
|
||||
end
|
||||
|
||||
self.fav_string += " fav:#{user_id}"
|
||||
self.fav_string.strip!
|
||||
update_attribute(:fav_string, fav_string)
|
||||
Favorite.create(:user_id => user_id, :post_id => id)
|
||||
append_user_to_fav_string(user_id)
|
||||
|
||||
Favorite.model_for(user_id).create(:user_id => user_id, :post_id => id)
|
||||
end
|
||||
|
||||
def delete_user_from_fav_string(user_id)
|
||||
update_attribute(:fav_string, fav_string.gsub(/(?:\A| )fav:#{user_id}(?:\Z| )/, " ").strip)
|
||||
end
|
||||
|
||||
def remove_favorite(user_id)
|
||||
@@ -400,10 +407,9 @@ class Post < ActiveRecord::Base
|
||||
user_id = user_id.id
|
||||
end
|
||||
|
||||
self.fav_string.gsub!(/(?:\A| )fav:#{user_id}(?:\Z| )/, " ")
|
||||
self.fav_string.strip!
|
||||
update_attribute(:fav_string, fav_string)
|
||||
Favorite.destroy(:user_id => user_id, :post_id => id)
|
||||
delete_user_from_fav_string(user_id)
|
||||
|
||||
Favorite.model_for(user_id).destroy_all(:user_id => user_id, :post_id => id)
|
||||
end
|
||||
|
||||
def favorited_user_ids
|
||||
@@ -780,10 +786,6 @@ class Post < ActiveRecord::Base
|
||||
remove_favorite(user_id)
|
||||
end
|
||||
end
|
||||
|
||||
def delete_favorites
|
||||
Favorite.destroy_for_post(self)
|
||||
end
|
||||
end
|
||||
|
||||
module DeletionMethods
|
||||
|
||||
@@ -118,12 +118,7 @@ class User < ActiveRecord::Base
|
||||
|
||||
module FavoriteMethods
|
||||
def favorites(options = {})
|
||||
post_ids = Favorite.find_post_ids(id, options)
|
||||
if post_ids.any?
|
||||
Post.where("id in (?)", post_ids).order(Favorite.sql_order_clause(post_ids))
|
||||
else
|
||||
Post.where("false")
|
||||
end
|
||||
Favorite.model_for(id).where("user_id = ?", id)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -212,7 +207,7 @@ class User < ActiveRecord::Base
|
||||
elsif created_at > 1.week.ago
|
||||
false
|
||||
else
|
||||
Comment.where("creator_id = ? and created_at > ?", id, 1.hour.ago).count <= Danbooru.config.member_comment_limit
|
||||
Comment.where("creator_id = ? and created_at > ?", id, 1.hour.ago).count < Danbooru.config.member_comment_limit
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user