* Some major bug fixes related to post sets. Tests for pools and favorites were added.

* Refactored the favorites code a bit. Adding a favorite from either an user or a post now works and will update all necessary records.
This commit is contained in:
albert
2011-06-07 19:06:39 -04:00
parent 49b3d43ddd
commit f67374da83
14 changed files with 215 additions and 70 deletions

View File

@@ -1,6 +1,6 @@
class Favorite < ActiveRecord::Base
TABLE_COUNT = 100
validates_uniqueness_of :post_id, :scope => :user_id
belongs_to :post
def self.model_for(user_id)
mod = user_id.to_i % TABLE_COUNT

View File

@@ -78,8 +78,9 @@ class Pool < ActiveRecord::Base
end
def posts(options = {})
limit = options[:limit] || Danbooru.config.posts_per_page
if options[:offset]
limit = options[:limit] || Danbooru.config.posts_per_page
slice = post_id_array.slice(options[:offset], limit)
if slice && slice.any?
Post.where("id in (?)", slice).order(arbitrary_sql_order_clause(slice, "posts"))

View File

@@ -384,32 +384,20 @@ class Post < ActiveRecord::Base
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
end
if favorited_by?(user_id)
return false
end
append_user_to_fav_string(user_id)
Favorite.model_for(user_id).create(:user_id => user_id, :post_id => id)
def add_favorite!(user)
return if favorited_by?(user.id)
append_user_to_fav_string(user.id)
user.add_favorite!(self)
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)
if user_id.is_a?(ActiveRecord::Base)
user_id = user_id.id
end
delete_user_from_fav_string(user_id)
Favorite.model_for(user_id).destroy_all(:user_id => user_id, :post_id => id)
def remove_favorite!(user)
return unless favorited_by?(user.id)
delete_user_from_fav_string(user.id)
user.remove_favorite!(self)
end
def favorited_user_ids
@@ -785,8 +773,8 @@ class Post < ActiveRecord::Base
return if parent.nil?
favorited_user_ids.each do |user_id|
parent.add_favorite(user_id)
remove_favorite(user_id)
parent.add_favorite!(User.find(user_id))
remove_favorite!(User.find(user_id))
end
end
end

View File

@@ -117,8 +117,20 @@ class User < ActiveRecord::Base
end
module FavoriteMethods
def favorites(options = {})
Favorite.model_for(id).where("user_id = ?", id)
def favorites
Favorite.model_for(id).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)
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)
post.remove_favorite!(self)
end
end