* 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:
@@ -1,7 +1,7 @@
|
||||
module PostSets
|
||||
module Favorite
|
||||
def user
|
||||
@user ||= User.find(params[:id])
|
||||
@user ||= ::User.find(params[:id])
|
||||
end
|
||||
|
||||
def tags
|
||||
@@ -19,11 +19,15 @@ module PostSets
|
||||
end
|
||||
|
||||
def count
|
||||
@count ||= Favorite.count(user.id)
|
||||
@count ||= relation.count
|
||||
end
|
||||
|
||||
def posts
|
||||
@posts ||= user.favorites(pagination_options)
|
||||
@posts ||= slice(relation).map(&:post)
|
||||
end
|
||||
|
||||
def relation
|
||||
::Favorite.model_for(user.id).where("user_id = ?", user.id).order("id desc")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,12 +1,5 @@
|
||||
module PostSets
|
||||
module Numbered
|
||||
attr_reader :page
|
||||
|
||||
def initialize(params)
|
||||
super
|
||||
@page = options[:page] ? options[:page].to_i : 1
|
||||
end
|
||||
|
||||
def total_pages
|
||||
@total_pages ||= (count / limit.to_f).ceil.to_i
|
||||
end
|
||||
@@ -17,7 +10,7 @@ module PostSets
|
||||
end
|
||||
|
||||
def slice(relation)
|
||||
relation.offset(offset).all
|
||||
relation.offset(offset).limit(limit).all
|
||||
end
|
||||
|
||||
def pagination_options
|
||||
@@ -28,6 +21,10 @@ module PostSets
|
||||
offset == 0
|
||||
end
|
||||
|
||||
def page
|
||||
@page ||= params[:page] ? params[:page].to_i : 1
|
||||
end
|
||||
|
||||
def offset
|
||||
((page < 1) ? 0 : (page - 1)) * limit
|
||||
end
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
module PostSets
|
||||
module Pool
|
||||
def pool
|
||||
@pool ||= Pool.find(params[:id])
|
||||
@pool ||= ::Pool.find(params[:id])
|
||||
end
|
||||
|
||||
def tags
|
||||
@@ -19,7 +19,7 @@ module PostSets
|
||||
end
|
||||
|
||||
def posts
|
||||
@posts ||= pool.posts(pagination_options)
|
||||
@posts ||= pool.posts(pagination_options).limit(limit).all
|
||||
end
|
||||
|
||||
def reload
|
||||
|
||||
@@ -5,7 +5,7 @@ module PostSets
|
||||
attr_accessor :tags, :count, :wiki_page, :artist, :suggestions
|
||||
|
||||
def tags
|
||||
@tags ||= Tag.normalize(params[:tags])
|
||||
@tags ||= ::Tag.normalize(params[:tags])
|
||||
end
|
||||
|
||||
def count
|
||||
@@ -13,7 +13,7 @@ module PostSets
|
||||
end
|
||||
|
||||
def posts
|
||||
@posts ||= slice(::Post.tag_match(tags).limit(limit))
|
||||
@posts ||= slice(::Post.tag_match(tags))
|
||||
end
|
||||
|
||||
def reload
|
||||
@@ -42,7 +42,7 @@ module PostSets
|
||||
end
|
||||
|
||||
def tag_array
|
||||
@tag_array ||= Tag.scan_query(tags)
|
||||
@tag_array ||= ::Tag.scan_query(tags)
|
||||
end
|
||||
|
||||
def validate
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
module PostSets
|
||||
module Sequential
|
||||
attr_reader :before_id, :after_id
|
||||
def before_id
|
||||
params[:before_id]
|
||||
end
|
||||
|
||||
def initialize(params)
|
||||
super
|
||||
@before_id = params[:before_id]
|
||||
@after_id = params[:after_id]
|
||||
def after_id
|
||||
params[:after_id]
|
||||
end
|
||||
|
||||
def slice(relation)
|
||||
if before_id
|
||||
relation.where("id < ?", before_id).all
|
||||
relation.where("id < ?", before_id).limit(limit).all
|
||||
elsif after_id
|
||||
relation.where("id > ?", after_id).order("id asc").all.reverse
|
||||
relation.where("id > ?", after_id).order("id asc").limit(limit).all.reverse
|
||||
else
|
||||
relation.all
|
||||
relation.limit(limit).all
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ module PostSets
|
||||
end
|
||||
|
||||
def tags
|
||||
@tags ||= Tag.normalize(wiki_page.title)
|
||||
@tags ||= ::Tag.normalize(wiki_page.title)
|
||||
end
|
||||
|
||||
def posts
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user