* Refactored PostSet, splitting it into PostSets::Post and PostSets::Favorite
* Additional functional tests
This commit is contained in:
@@ -35,26 +35,26 @@ class Favorite
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def self.destroy_for_post_and_user(post_id, user_id)
|
||||
execute_sql("DELETE FROM #{table_name_for(user_id)} WHERE post_id = #{post_id} AND user_id = #{user_id}")
|
||||
end
|
||||
|
||||
def self.destroy_for_post(post)
|
||||
0.upto(9) do |i|
|
||||
execute_sql("DELETE FROM favorites_#{i} WHERE post_id = #{post.id}")
|
||||
private
|
||||
def self.destroy_for_post_and_user(post_id, user_id)
|
||||
execute_sql("DELETE FROM #{table_name_for(user_id)} WHERE post_id = #{post_id} AND user_id = #{user_id}")
|
||||
end
|
||||
end
|
||||
|
||||
def self.destroy_for_user(user)
|
||||
execute_sql("DELETE FROM #{table_name_for(user)} WHERE user_id = #{user.id}")
|
||||
end
|
||||
def self.destroy_for_post(post)
|
||||
0.upto(9) do |i|
|
||||
execute_sql("DELETE FROM favorites_#{i} WHERE post_id = #{post.id}")
|
||||
end
|
||||
end
|
||||
|
||||
def self.select_value_sql(sql, *params)
|
||||
ActiveRecord::Base.select_value_sql(sql, *params)
|
||||
end
|
||||
def self.destroy_for_user(user)
|
||||
execute_sql("DELETE FROM #{table_name_for(user)} WHERE user_id = #{user.id}")
|
||||
end
|
||||
|
||||
def self.execute_sql(sql, *params)
|
||||
ActiveRecord::Base.execute_sql(sql, *params)
|
||||
end
|
||||
def self.select_value_sql(sql, *params)
|
||||
ActiveRecord::Base.select_value_sql(sql, *params)
|
||||
end
|
||||
|
||||
def self.execute_sql(sql, *params)
|
||||
ActiveRecord::Base.execute_sql(sql, *params)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,108 +0,0 @@
|
||||
class PostSet
|
||||
class Error < Exception ; end
|
||||
|
||||
attr_accessor :tags, :page, :before_id, :errors, :count
|
||||
attr_accessor :wiki_page, :artist, :posts, :suggestions
|
||||
|
||||
def initialize(tags, page, before_id = nil)
|
||||
@tags = Tag.normalize(tags)
|
||||
@page = page.to_i
|
||||
@before_id = before_id
|
||||
@errors = []
|
||||
load_associations
|
||||
load_posts
|
||||
load_suggestions
|
||||
validate
|
||||
end
|
||||
|
||||
def use_sequential_paginator?
|
||||
!use_numbered_paginator?
|
||||
end
|
||||
|
||||
def use_numbered_paginator?
|
||||
before_id.nil?
|
||||
end
|
||||
|
||||
def has_errors?
|
||||
errors.any?
|
||||
end
|
||||
|
||||
def offset
|
||||
x = (page - 1) * limit
|
||||
if x < 0
|
||||
x = 0
|
||||
end
|
||||
x
|
||||
end
|
||||
|
||||
def limit
|
||||
Danbooru.config.posts_per_page
|
||||
end
|
||||
|
||||
def is_single_tag?
|
||||
tag_array.size == 1
|
||||
end
|
||||
|
||||
def date_tag
|
||||
tag_array.grep(/date:/).first
|
||||
end
|
||||
|
||||
def load_associations
|
||||
if is_single_tag?
|
||||
@wiki_page = WikiPage.find_by_title(tags)
|
||||
@artist = Artist.find_by_name(tags)
|
||||
end
|
||||
end
|
||||
|
||||
def load_posts
|
||||
@count = Post.fast_count(tags)
|
||||
@posts = Post.find_by_tags(tags, :before_id => before_id).all(:order => "posts.id desc", :limit => limit, :offset => offset)
|
||||
end
|
||||
|
||||
def load_suggestions
|
||||
if count < limit && is_single_tag?
|
||||
@suggestions = Tag.find_suggestions(tags)
|
||||
else
|
||||
@suggestions = []
|
||||
end
|
||||
end
|
||||
|
||||
def tag_array
|
||||
@tag_array ||= Tag.scan_query(tags)
|
||||
end
|
||||
|
||||
def validate
|
||||
validate_page
|
||||
validate_query_count
|
||||
rescue Error => x
|
||||
@errors << x.to_s
|
||||
end
|
||||
|
||||
def validate_page
|
||||
if page > 1_000
|
||||
raise Error.new("You cannot explicitly specify the page after page 1000")
|
||||
end
|
||||
end
|
||||
|
||||
def validate_query_count
|
||||
if !CurrentUser.user.is_privileged? && tag_array.size > 2
|
||||
raise Error.new("You can only search up to two tags at once with a basic account")
|
||||
end
|
||||
|
||||
if tag_array.size > 6
|
||||
raise Error.new("You can only search up to six tags at once")
|
||||
end
|
||||
end
|
||||
|
||||
def to_xml
|
||||
posts.to_xml
|
||||
end
|
||||
|
||||
def to_json
|
||||
posts.to_json
|
||||
end
|
||||
|
||||
def presenter
|
||||
@presnter ||= PostSetPresenter.new(self)
|
||||
end
|
||||
end
|
||||
39
app/logical/post_sets/base.rb
Normal file
39
app/logical/post_sets/base.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
module PostSets
|
||||
class Base
|
||||
attr_accessor :page, :before_id, :count
|
||||
|
||||
def initialize(options = {})
|
||||
@page = options[:page].to_i
|
||||
@before_id = options[:before_id]
|
||||
load_posts
|
||||
end
|
||||
|
||||
def has_wiki?
|
||||
false
|
||||
end
|
||||
|
||||
def use_sequential_paginator?
|
||||
!use_numbered_paginator?
|
||||
end
|
||||
|
||||
def use_numbered_paginator?
|
||||
before_id.nil?
|
||||
end
|
||||
|
||||
def load_posts
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def to_xml
|
||||
posts.to_xml
|
||||
end
|
||||
|
||||
def to_json
|
||||
posts.to_json
|
||||
end
|
||||
|
||||
def presenter
|
||||
@presnter ||= PostSetPresenter.new(self)
|
||||
end
|
||||
end
|
||||
end
|
||||
18
app/logical/post_sets/favorite.rb
Normal file
18
app/logical/post_sets/favorite.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
module PostSets
|
||||
class FavoriteSet < Base
|
||||
attr_accessor :user
|
||||
|
||||
def initialize(options = {})
|
||||
super(options)
|
||||
@user = user
|
||||
end
|
||||
|
||||
def tags
|
||||
"fav:#{user.name}"
|
||||
end
|
||||
|
||||
def load_posts
|
||||
user.favorite_posts(:before_id => before_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
92
app/logical/post_sets/post.rb
Normal file
92
app/logical/post_sets/post.rb
Normal file
@@ -0,0 +1,92 @@
|
||||
module PostSets
|
||||
class Post < Base
|
||||
class Error < Exception ; end
|
||||
|
||||
attr_accessor :tags, :errors, :count
|
||||
attr_accessor :wiki_page, :artist, :suggestions
|
||||
|
||||
def initialize(tags, options = {})
|
||||
super(options)
|
||||
@tags = Tag.normalize(tags)
|
||||
@errors = []
|
||||
load_associations
|
||||
load_suggestions
|
||||
validate
|
||||
end
|
||||
|
||||
def has_wiki?
|
||||
is_single_tag?
|
||||
end
|
||||
|
||||
def has_errors?
|
||||
errors.any?
|
||||
end
|
||||
|
||||
def offset
|
||||
x = (page - 1) * limit
|
||||
if x < 0
|
||||
x = 0
|
||||
end
|
||||
x
|
||||
end
|
||||
|
||||
def limit
|
||||
Danbooru.config.posts_per_page
|
||||
end
|
||||
|
||||
def is_single_tag?
|
||||
tag_array.size == 1
|
||||
end
|
||||
|
||||
def date_tag
|
||||
tag_array.grep(/date:/).first
|
||||
end
|
||||
|
||||
def load_associations
|
||||
if is_single_tag?
|
||||
@wiki_page = WikiPage.find_by_title(tags)
|
||||
@artist = Artist.find_by_name(tags)
|
||||
end
|
||||
end
|
||||
|
||||
def load_posts
|
||||
@count = Post.fast_count(tags)
|
||||
@posts = Post.find_by_tags(tags, :before_id => before_id).all(:order => "posts.id desc", :limit => limit, :offset => offset)
|
||||
end
|
||||
|
||||
def load_suggestions
|
||||
if count < limit && is_single_tag?
|
||||
@suggestions = Tag.find_suggestions(tags)
|
||||
else
|
||||
@suggestions = []
|
||||
end
|
||||
end
|
||||
|
||||
def tag_array
|
||||
@tag_array ||= Tag.scan_query(tags)
|
||||
end
|
||||
|
||||
def validate
|
||||
validate_page
|
||||
validate_query_count
|
||||
rescue Error => x
|
||||
@errors << x.to_s
|
||||
end
|
||||
|
||||
def validate_page
|
||||
if page > 1_000
|
||||
raise Error.new("You cannot explicitly specify the page after page 1000")
|
||||
end
|
||||
end
|
||||
|
||||
def validate_query_count
|
||||
if !CurrentUser.is_privileged? && tag_array.size > 2
|
||||
raise Error.new("You can only search up to two tags at once with a basic account")
|
||||
end
|
||||
|
||||
if tag_array.size > 6
|
||||
raise Error.new("You can only search up to six tags at once")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user