tag set presenter

This commit is contained in:
albert
2012-01-14 14:31:09 -05:00
parent bec405df95
commit 673ff02fcf
8 changed files with 37 additions and 40 deletions

View File

@@ -43,6 +43,10 @@ module PostSets
tag_array.size == 1
end
def is_empty_tag?
tag_array.size == 0
end
def current_page
[page.to_i, 1].max
end

View File

@@ -1,6 +1,6 @@
class RelatedTagCalculator
def self.find_tags(tag, limit)
Post.tag_match(tag).limit(limit).select("posts.tag_string").order("posts.md5").map(&:tag_string)
Post.tag_match(tag).limit(limit).select("posts.tag_string").reorder("posts.md5").map(&:tag_string)
end
def self.calculate_from_sample_to_array(tags, category_constraint = nil)

View File

@@ -661,7 +661,7 @@ class Post < ActiveRecord::Base
relation = add_range_relation(q[:height], "posts.image_height", relation)
relation = add_range_relation(q[:score], "posts.score", relation)
relation = add_range_relation(q[:filesize], "posts.file_size", relation)
relation = add_range_relation(q[:date], "posts.created_at::date", relation)
relation = add_range_relation(q[:date], "date(posts.created_at)", relation)
relation = add_range_relation(q[:general_tag_count], "posts.tag_count_general", relation)
relation = add_range_relation(q[:artist_tag_count], "posts.tag_count_artist", relation)
relation = add_range_relation(q[:copyright_tag_count], "posts.tag_count_copyright", relation)

View File

@@ -1,32 +0,0 @@
class PostSetPresenter < Presenter
attr_accessor :post_set, :tag_set_presenter
def initialize(post_set)
@post_set = post_set
@tag_set_presenter = TagSetPresenter.new(RelatedTagCalculator.calculate_from_sample_to_array(@post_set.tag_string).map {|x| x[0]})
end
def posts
post_set.posts
end
def tag_list_html(template)
tag_set_presenter.tag_list_html(template)
end
def post_previews_html(template)
html = "<div class='post-previews'>"
if posts.empty?
return template.render("post_sets/blank")
end
posts.each do |post|
html << PostPresenter.preview(post)
end
html << '</div>'
html.html_safe
end
end

View File

@@ -10,14 +10,38 @@ module PostSetPresenters
def related_tags
if post_set.is_single_tag?
tag = Tag.find_by_name(post_set.tag_string)
if tag
return tag.related_tag_array.map(&:first)
end
related_tags_for_single
elsif post_set.is_empty_tag?
popular_tags
else
related_tags_for_group
end
end
def popular_tags
n = 1
results = []
while results.empty? && n < 256
query = n.days.ago.strftime("date:>%Y-%m-%d")
results = RelatedTagCalculator.calculate_from_sample_to_array(query).map(&:first)
n *= 2
end
results
end
def related_tags_for_group
RelatedTagCalculator.calculate_from_sample_to_array(post_set.tag_string).map(&:first)
end
def related_tags_for_single
tag = Tag.find_by_name(post_set.tag_string)
if tag
return tag.related_tag_array.map(&:first)
end
end
def tag_list_html(template)
tag_set_presenter.tag_list_html(template)

View File

@@ -12,7 +12,7 @@ class TagSetPresenter < Presenter
def tag_list_html(template, options = {})
html = ""
html << "<ul>"
@tags.flatten.each do |tag|
@tags.each do |tag|
html << build_list_item(tag, template, options)
end
html << "</ul>"

View File

@@ -68,6 +68,7 @@ class CreatePosts < ActiveRecord::Migration
add_index :posts, :uploader_id
add_index :posts, :uploader_ip_addr
execute "create index index_posts_on_created_at_date on posts (date(created_at))"
execute "CREATE INDEX index_posts_on_mpixels ON posts (((image_width * image_height)::numeric / 1000000.0))"
execute "SET statement_timeout = 0"

View File

@@ -3049,7 +3049,7 @@ create index index_posts_on_uploader_ip_addr on posts (uploader_ip_addr);
drop function trg_posts_tags__delete();
drop function trg_posts_tags__insert();
update posts set uploader_id = 1 where uploader_id is null;
create index index_posts_on_created_at_date on posts (date(created_at));
alter table post_appeals rename column user_id to creator_id;
alter index index_post_appeals_on_user_id rename to index_post_appeals_on_creator_id;
alter table post_appeals rename column ip_addr to creator_ip_addr;