more work

This commit is contained in:
albert
2010-10-19 19:34:31 -04:00
parent cb3d7e9e9b
commit acdce69f20
48 changed files with 2494 additions and 1585 deletions

View File

@@ -2,7 +2,7 @@ class CommentsController < ApplicationController
respond_to :html, :xml, :json
def index
@posts = Post.paginate :order => "last_commented_at DESC", :per_page => 8
@posts = Post.commented_before(params[:before_date] || Time.now).limit(8)
end
def update

View File

@@ -1,23 +1,27 @@
class RelatedTagCalculator
def find_tags(tag, limit)
def self.find_tags(tag, limit)
Post.find_by_tags(tag, :limit => limit, :select => "posts.tag_string", :order => "posts.md5").map(&:tag_string)
end
def calculate_from_sample(name, limit, category_constraint = nil)
def self.calculate_from_sample_to_array(tags, category_constraint = nil)
convert_hash_to_array(calculate_from_sample(tags, Danbooru.config.post_sample_size, category_constraint))
end
def self.calculate_from_sample(tags, limit, category_constraint = nil)
counts = Hash.new {|h, k| h[k] = 0}
case category_constraint
when Tag.categories.artist
limit *= 5
limit *= 4
when Tag.categories.copyright
limit *= 4
limit *= 3
when Tag.categories.character
limit *= 3
limit *= 2
end
find_tags(name, limit).each do |tags|
find_tags(tags, limit).each do |tags|
tag_array = Tag.scan_tags(tags)
if category_constraint
tag_array.each do |tag|
@@ -36,11 +40,11 @@ class RelatedTagCalculator
counts
end
def convert_hash_to_array(hash)
def self.convert_hash_to_array(hash)
hash.to_a.sort_by {|x| -x[1]}.slice(0, 25)
end
def convert_hash_to_string(hash)
def self.convert_hash_to_string(hash)
convert_hash_to_array(hash).flatten.join(" ")
end
end

View File

@@ -8,7 +8,7 @@ class Post < ActiveRecord::Base
before_save :create_tags
before_save :update_tag_post_counts
before_save :set_tag_counts
before_validation_on_create :initialize_uploader
before_validation :initialize_uploader, :on => :create
belongs_to :updater, :class_name => "User"
belongs_to :approver, :class_name => "User"
belongs_to :parent, :class_name => "Post"
@@ -25,6 +25,7 @@ class Post < ActiveRecord::Base
validate :validate_parent_does_not_have_a_parent
attr_accessible :source, :rating, :tag_string, :old_tag_string, :last_noted_at
scope :visible, lambda {|user| Danbooru.config.can_user_see_post_conditions(user)}
scope :commented_before, lambda {|date| where("last_commented_at < ?", date).order("last_commented_at DESC")}
module FileMethods
def delete_files

View File

@@ -312,9 +312,8 @@ class Tag < ActiveRecord::Base
module RelationMethods
def update_related
calculator = RelatedTagCalculator.new
counts = calculator.calculate_from_sample(Danbooru.config.post_sample_size, name)
self.related_tags = calculator.convert_hash_to_string(counts)
counts = RelatedTagCalculator.calculate_from_sample(Danbooru.config.post_sample_size, name)
self.related_tags = RelatedTagCalculator.convert_hash_to_string(counts)
end
def update_related_if_outdated

View File

@@ -7,8 +7,8 @@ class Upload < ActiveRecord::Base
attr_accessor :file, :image_width, :image_height, :file_ext, :md5, :file_size
belongs_to :uploader, :class_name => "User"
belongs_to :post
before_validation_on_create :initialize_uploader
before_validation_on_create :initialize_status
before_validation :initialize_uploader, :on => :create
before_validation :initialize_status, :on => :create
before_create :convert_cgi_file
validate :uploader_is_not_limited

View File

@@ -1,2 +0,0 @@
class PaginatorPresenter < Presenter
end

View File

@@ -0,0 +1,78 @@
module Paginators
class Base < Presenter
def sequential_pagination_html(template)
html = "<menu>"
prev_url = template.request.env["HTTP_REFERER"]
next_url = sequential_link(template)
html << %{<li><a href="#{prev_url}">&laquo; Previous</a></li>}
if post_set.posts.any?
html << %{<li><a href="#{next_url}">Next &raquo;</a></li>}
end
html << "</menu>"
html.html_safe
end
def numbered_pagination_html(template)
html = "<menu>"
window = 3
if total_pages <= (window * 2) + 5
1.upto(total_pages) do |page|
html << numbered_pagination_item(template, page, current_page)
end
elsif current_page <= window + 2
1.upto(current_page + window) do |page|
html << numbered_pagination_item(template, page, current_page)
end
html << numbered_pagination_item(template, "...", current_page)
html << numbered_pagination_item(template, total_pages, current_page)
elsif current_page >= total_pages - (window + 1)
html << numbered_pagination_item(template, 1, current_page)
html << numbered_pagination_item(template, "...", current_page)
(current_page - window).upto(total_pages) do |page|
html << numbered_pagination_item(template, page, current_page)
end
else
html << numbered_pagination_item(template, 1, current_page)
html << numbered_pagination_item(template, "...", current_page)
(current_page - window).upto(current_page + window) do |page|
html << numbered_pagination_item(template, page, current_page)
end
html << numbered_pagination_item(template, "...", current_page)
html << numbered_pagination_item(template, total_pages, current_page)
end
html << "</menu>"
html.html_safe
end
protected
def numbered_pagination_item(template, page, current_page)
html = "<li>"
if page == "..."
html << "..."
elsif page == current_page
html << page.to_s
else
html << link(template, page)
end
html << "</li>"
html.html_safe
end
def total_pages
raise NotImplementedError
end
def current_page
raise NotImplementedError
end
def sequential_link(template)
raise NotImplementedError
end
def paginated_link(template, page)
raise NotImplementedError
end
end
end

View File

@@ -0,0 +1,26 @@
module Paginators
class Post < Base
attr_accessor :post_set
def initialize(post_set)
@post_set = post_set
end
protected
def total_pages
(post_set.count.to_f / post_set.limit.to_f).ceil
end
def current_page
[1, post_set.page].max
end
def sequential_link(template)
template.posts_path(:tags => template.params[:tags], before_id => post_set.posts[-1].id, :page => nil)
end
def paginated_link(template, page)
template.link_to(page, template.posts_path(:tags => template.params[:tags], :page => page))
end
end
end

View File

@@ -1,18 +1,19 @@
require 'pp'
class PostSetPresenter < Presenter
attr_accessor :post_set
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.tags).map {|x| x[0]})
end
def posts
post_set.posts
end
def tag_list_html
""
def tag_list_html(template)
tag_set_presenter.tag_list_html(template)
end
def wiki_html(template)
@@ -39,72 +40,12 @@ class PostSetPresenter < Presenter
def pagination_html(template)
if post_set.use_sequential_paginator?
sequential_pagination_html(template)
Paginators::Post.new(post_set).sequential_pagination_html(template)
else
numbered_pagination_html(template)
Paginators::Post.new(post_set).numbered_pagination_html(template)
end
end
def sequential_pagination_html(template)
html = "<menu>"
prev_url = template.request.env["HTTP_REFERER"]
next_url = template.posts_path(:tags => template.params[:tags], before_id => post_set.posts[-1].id, :page => nil)
html << %{<li><a href="#{prev_url}">&laquo; Previous</a></li>}
if post_set.posts.any?
html << %{<li><a href="#{next_url}">Next &raquo;</a></li>}
end
html << "</menu>"
html.html_safe
end
def numbered_pagination_html(template)
total_pages = (post_set.count.to_f / post_set.limit.to_f).ceil
current_page = [1, post_set.page].max
html = "<menu>"
window = 3
if total_pages <= (window * 2) + 5
1.upto(total_pages) do |page|
html << numbered_pagination_item(template, page, current_page)
end
elsif current_page <= window + 2
1.upto(current_page + window) do |page|
html << numbered_pagination_item(template, page, current_page)
end
html << numbered_pagination_item(template, "...", current_page)
html << numbered_pagination_item(template, total_pages, current_page)
elsif current_page >= total_pages - (window + 1)
html << numbered_pagination_item(template, 1, current_page)
html << numbered_pagination_item(template, "...", current_page)
(current_page - window).upto(total_pages) do |page|
html << numbered_pagination_item(template, page, current_page)
end
else
html << numbered_pagination_item(template, 1, current_page)
html << numbered_pagination_item(template, "...", current_page)
(current_page - window).upto(current_page + window) do |page|
html << numbered_pagination_item(template, page, current_page)
end
html << numbered_pagination_item(template, "...", current_page)
html << numbered_pagination_item(template, total_pages, current_page)
end
html << "</menu>"
html.html_safe
end
def numbered_pagination_item(template, page, current_page)
html = "<li>"
if page == "..."
html << "..."
elsif page == current_page
html << page.to_s
else
html << template.link_to(page, template.__send__(:posts_path, :tags => template.params[:tags], :page => page))
end
html << "</li>"
html.html_safe
end
def post_previews_html
html = ""

View File

@@ -31,12 +31,12 @@ private
def build_list_item(tag, template, options)
html = ""
html << %{<li data-tag-type="#{category_for(tag)}">}
html << %{<li data-tag-type="#{category_for(tag)}" data-tag-name="#{u(tag)}">}
if options[:show_extra_links]
html << %{<a href="/wiki_pages/#{u(tag)}">?</a> }
if CurrentUser.user.is_privileged?
html << %{<a href="/wiki_pages?title=#{u(tag)}">?</a> }
html << %{<a href="#" class="search-inc-tag">+</a> }
html << %{<a href="#" class="search-exl-tag">-</a> }
html << %{<a href="#" class="search-exl-tag">&ndash;</a> }
end
humanized_tag = tag.tr("_", " ")

View File

@@ -0,0 +1,2 @@
- @posts.each do |post|
= @post.id

View File

@@ -13,6 +13,7 @@
<% end %>
<%= auto_discovery_link_tag :atom, posts_path(:format => "atom", :tags => params[:tags]) %>
<%= stylesheet_link_tag "compiled/default" %>
<%= stylesheet_link_tag "smoothness/jquery-ui-1.8.5.custom.css" %>
<%= javascript_include_tag "compiled/default" %>
<%= Danbooru.config.custom_html_header_content %>
<%= yield :html_header %>
@@ -47,7 +48,7 @@
<div id="notice" style="display: none;"></div>
<% end %>
<div id="content">
<div id="page">
<%= yield :layout %>
</div>

View File

@@ -1,81 +1,90 @@
<% if @post_set.suggestions.any? %>
<div class="notice">
Maybe you meant: <%= @post_set.suggestions.map {|x| link_to(x, posts_path(:tags => x), :class => "tag-type-#{Tag.type_name(x)}" )}.to_sentence(:last_word_connector => ", or ", :two_words_connector => " or ") %>
</div>
<% end %>
<div class="posts">
<div class="index">
<% if @post_set.suggestions.any? %>
<div class="notice">
Maybe you meant: <%= @post_set.suggestions.map {|x| link_to(x, posts_path(:tags => x), :class => "tag-type-#{Tag.type_name(x)}" )}.to_sentence(:last_word_connector => ", or ", :two_words_connector => " or ") %>
</div>
<% end %>
<aside class="sidebar">
<section id="search-box">
<h1>Search</h1>
<% form_tag(posts_path, :method => "get") do %>
<%= text_field_tag("tags", params[:tags], :size => 20) %>
<%= submit_tag "Go" %>
<% end %>
</section>
<% if CurrentUser.user.is_privileged? %>
<section id="mode-box">
<h1>Mode</h1>
<form action="/">
<select name="mode">
<option value="view">View</option>
<option value="edit">Edit</option>
<option value="add-fav">Favorite</option>
<option value="remove-fav">Unfavorite</option>
<option value="rating-s">Rate safe</option>
<option value="rating-q">Rate questionable</option>
<option value="rating-e">Rate explicit</option>
<option value="vote-up">Vote up</option>
<option value="vote-down">Vote down</option>
<option value="lock-rating">Lock rating</option>
<option value="lock-note">Lock notes</option>
<option value="edit-tag-script">Edit tag script</option>
<option value="apply-tag-script">Apply tag script</option>
<% if CurrentUser.user.is_janitor? %>
<option value="approve">Approve</option>
<aside class="sidebar">
<section id="search-box">
<h1>Search</h1>
<% form_tag(posts_path, :method => "get") do %>
<%= text_field_tag("tags", params[:tags], :size => 20) %>
<%= submit_tag "Go" %>
<% end %>
</section>
<% if CurrentUser.user.is_privileged? %>
<section id="mode-box">
<h1>Mode</h1>
<form action="/">
<select name="mode">
<option value="view">View</option>
<option value="edit">Edit</option>
<option value="add-fav">Favorite</option>
<option value="remove-fav">Unfavorite</option>
<option value="rating-s">Rate safe</option>
<option value="rating-q">Rate questionable</option>
<option value="rating-e">Rate explicit</option>
<option value="vote-up">Vote up</option>
<option value="vote-down">Vote down</option>
<option value="lock-rating">Lock rating</option>
<option value="lock-note">Lock notes</option>
<option value="edit-tag-script">Edit tag script</option>
<option value="apply-tag-script">Apply tag script</option>
<% if CurrentUser.user.is_janitor? %>
<option value="approve">Approve</option>
<% end %>
<option value="unapprove">Unapprove</option>
</select>
</form>
</section>
<% end %>
<section id="blacklist-box">
<h1>Blacklisted</h1>
<%= link_to "Hidden", "#" %>
<ul>
</ul>
</section>
<section id="tag-and-wiki-box">
<menu>
<li><h1><a href="#tag-box">Tags</a></h1></li>
<% unless @post_set.tags.blank? %>
<li><h1><a href="#wiki-box">Wiki</a></h1></li>
<% end %>
<option value="unapprove">Unapprove</option>
</select>
</form>
</section>
<% end %>
</menu>
<section id="blacklist-box">
<h1>Blacklisted</h1>
<%= link_to "Hidden", "#" %>
<ul>
</ul>
</section>
<div id="tag-and-wiki-box">
<menu>
<li><h1>Tags</h1></li>
<li><h1>Wiki</h1></li>
</menu>
<div id="tag-box">
<h2>Tags</h2>
<%= @post_set.presenter.tag_list_html(self) %>
</div>
<div id="wiki-box">
<h2>Wiki</h2>
<%= @post_set.presenter.wiki_html(self) %>
</div>
</section>
</aside>
<section class="content">
<h1>Posts</h1>
<%= @post_set.presenter.post_previews_html %>
<div class="clearfix"></div>
<section id="tag-box">
<h2>Tags</h2>
<%= @post_set.presenter.tag_list_html %>
<div class="paginator">
<%= @post_set.presenter.pagination_html(self) %>
</div>
</section>
<section id="wiki-box">
<h2>Wiki</h2>
<%= @post_set.presenter.wiki_html(self) %>
</section>
<% content_for(:page_title) do %>
/ <%= @post_set.tags %>
<% end %>
<%= render :partial => "posts/partials/common/secondary_links" %>
</div>
</aside>
</div>
<section>
<h1>Posts</h1>
<%= @post_set.presenter.post_previews_html %>
</section>
<footer>
<%= @post_set.presenter.pagination_html(self) %>
</footer>
<% content_for(:page_title) do %>
/ <%= @post_set.tags %>
<% end %>
<%= render :partial => "posts/partials/common/secondary_links" %>

View File

@@ -5,46 +5,54 @@
</div>
<% end %>
<% form_for(post) do |f| %>
<% form_for(post, :html => {:class => "simple_form"}) do |f| %>
<%= f.hidden_field :old_tags, :value => post.tag_string %>
<p>
<div class="input">
<% if post.is_rating_locked? %>
This post is rating locked.
<% else %>
<%= f.label :rating_explicit, "Explicit" %>
<%= f.radio_button :rating, :e %>
<%= f.label :rating_questionable, "Questionable" %>
<%= f.radio_button :rating, :q %>
<%= f.label :blank, "Rating" %>
<fieldset class="ratings">
<%= f.radio_button :rating, :e %>
<%= f.label :rating_e, "Explicit" %>
<%= f.label :rating_safe, "Safe" %>
<%= f.radio_button :rating, :s %>
<%= f.radio_button :rating, :q %>
<%= f.label :rating_q, "Questionable" %>
<%= f.radio_button :rating, :s %>
<%= f.label :rating_s, "Safe" %>
</fieldset>
<% end %>
</p>
</div>
<% if CurrentUser.user.is_privileged? %>
<p>
<%= f.label :is_note_locked, "Lock notes" %>
<%= f.check_box :is_note_locked %>
</p>
<p>
<%= f.label :is_rating_locked, "Lock rating" %>
<%= f.check_box :is_rating_locked %>
</p>
<div class="input">
<%= f.label :blank, "Lock" %>
<fieldset class="locks">
<%= f.check_box :is_note_locked %>
<%= f.label :is_note_locked, "Notes" %>
<%= f.check_box :is_rating_locked %>
<%= f.label :is_rating_locked, "Rating" %>
</fieldset>
</div>
<% end %>
<p>
<div class="input">
<%= f.label :source %>
<%= f.text_field :source %>
</p>
</div>
<p>
<div class="input">
<%= f.label :tag_string, "Tags" %>
<%= f.text_area :tag_string , :size => "50x3" %>
</p>
</div>
<%= submit_tag "Submit" %>
<div class="input">
<%= submit_tag "Submit" %>
</div>
<% end %>
</div>

View File

@@ -1,5 +1,4 @@
<ul>
<li>ID: <%= post.id %></li>
<li>Uploader: <%= link_to_unless(post.uploader.nil?, post.uploader_name, user_path(post.uploader)) %></li>
<li>Uploaded: <%= time_ago_in_words(post.created_at).gsub(/about/, "") %> ago</li>
<% if post.approver %>

View File

@@ -1,48 +1,58 @@
<aside class="sidebar">
<section>
<h1>Search</h1>
<%= render :partial => "posts/partials/common/search" %>
</section>
<section>
<h1>Tags</h1>
<%= @post.presenter.tag_list_html(self) %>
</section>
<section>
<h1>Information</h1>
<%= render :partial => "posts/partials/show/information", :locals => {:post => @post} %>
</section>
<div class="posts">
<div class="show">
<aside id="sidebar">
<section>
<h1>Search</h1>
<%= render :partial => "posts/partials/common/search" %>
</section>
<section>
<h1>Options</h1>
<%= render :partial => "posts/partials/show/options", :locals => {:post => @post} %>
</section>
</aside>
<section>
<h1>Tags</h1>
<%= @post.presenter.tag_list_html(self) %>
</section>
<section>
<h1>Post</h1>
<section>
<h2>Image</h2>
<%= @post.presenter.image_html(self) %>
</section>
<section>
<h2>Notes</h2>
<%= render :partial => "notes/note", :collection => @post.notes.active %>
</section>
<section>
<h2>Comments</h2>
<%= render :partial => "comments/partials/index/list", :locals => {:comments => @post.comments, :post => @post} %>
</section>
<section>
<h2>Edit</h2>
<%= render :partial => "posts/partials/show/edit", :locals => {:post => @post} %>
</section>
</section>
<section>
<h1>Information</h1>
<%= render :partial => "posts/partials/show/information", :locals => {:post => @post} %>
</section>
<section>
<h1>Options</h1>
<%= render :partial => "posts/partials/show/options", :locals => {:post => @post} %>
</section>
</aside>
<section id="content">
<h1>Post</h1>
<section id="image">
<h2>Image</h2>
<%= @post.presenter.image_html(self) %>
</section>
<menu id="post-sections">
<li><a href="#comments">Comments</a></li>
<li><a href="#notes">Notes</a></li>
<li><a href="#edit">Edit</a></li>
</menu>
<section id="comments">
<h2>Comments</h2>
<%= render :partial => "comments/partials/index/list", :locals => {:comments => @post.comments, :post => @post} %>
</section>
<section id="notes">
<h2>Notes</h2>
<%= render :partial => "notes/note", :collection => @post.notes.active %>
</section>
<section id="edit">
<h2>Edit</h2>
<%= render :partial => "posts/partials/show/edit", :locals => {:post => @post} %>
</section>
</section>
</div>
</div>
<% content_for(:page_title) do %>
/ <%= @post.tag_string %>

View File

@@ -21,4 +21,4 @@
</tbody>
</table>
<%= render :partial => "posts/common_secondary_nav_links" %>
<%= render :partial => "posts/partials/common/secondary_links" %>

View File

@@ -1,56 +1,60 @@
<div id="upload-guide-notice">
<p>Before uploading, please read the <%= link_to "how to upload guide", wiki_page_path("howto:upload") %>. It explains how to tag and what ratings are.</p>
</div>
<% form_for(@upload, :html => {:multipart => true}) do |f| %>
<% if params[:url] %>
<div id="image-preview">
<%= image_tag(params[:url], :title => "Preview") %>
<p id="scale"></p>
<div class="uploads">
<div class="new">
<div id="upload-guide-notice">
<p>Before uploading, please read the <%= link_to "how to upload guide", wiki_pages_path(:title => "howto:upload") %>.</p>
</div>
<% if @post %>
<p>This post was probably already uploaded (<%= link_to "post ##{@post.id}", posts_path(@post), :target => "_blank" %>).</p>
<% form_for(@upload, :html => {:multipart => true, :class => "simple_form"}) do |f| %>
<% if params[:url] %>
<div id="image-preview">
<%= image_tag(params[:url], :title => "Preview") %>
<p id="scale"></p>
</div>
<% if @post %>
<p>This post was probably already uploaded (<%= link_to "post ##{@post.id}", posts_path(@post), :target => "_blank" %>).</p>
<% end %>
<% end %>
<div class="input">
<%= f.label :file %>
<%= f.file_field :file, :size => 50 %>
</div>
<div class="input">
<%= f.label :source, nil, :title => "You can enter a URL to have #{Danbooru.config.app_name} automatically download and process it" %>
<%= f.text_field :source, :size => 50, :value => params[:url] %>
</div>
<div class="input">
<%= f.label :rating_q, "Rating" %>
<fieldset class="ratings">
<%= f.radio_button :rating, :e %>
<%= f.label :rating_e, "Explicit", :title => "Hardcore porn, visible genitals, gore" %>
<%= f.radio_button :rating, :q %>
<%= f.label :rating_q, "Questionable", :title => "Nudity, anything erotic" %>
<%= f.radio_button :rating, :s %>
<%= f.label :rating_s, "Safe", :title => "Everything else" %>
</fieldset>
</div>
<div class="input">
<%= f.label :tag_string, "Tags" %>
<%= f.text_area :tag_string, :size => "60x4" %>
</div>
<div class="input">
<%= submit_tag "Submit" %>
</div>
<% end %>
<% end %>
<fieldset>
<legend>Upload</legend>
<p>
<%= f.label :file %>
<%= f.file_field :file, :size => 50 %>
</p>
<p>
<%= f.label :source, nil, :title => "You can enter a URL to have #{Danbooru.config.app_name} automatically download and process it" %>
<%= f.text_field :source, :size => 50, :value => params[:url] %>
</p>
<p>
<%= f.label :tag_string, "Tags" %>
<%= f.text_area :tag_string, :size => "60x4" %>
</p>
<p>
<%= f.label :rating_e, "Explicit", :title => "Hardcore porn, visible genitals, gore" %>
<%= f.radio_button :rating, :e %>
<%= f.label :rating_q, "Questionable", :title => "Nudity, anything erotic" %>
<%= f.radio_button :rating, :q %>
<%= f.label :rating_s, "Safe", :title => "Everything else" %>
<%= f.radio_button :rating, :s %>
</p>
<p>
<%= submit_tag "Submit" %>
</p>
</fieldset>
<% end %>
</div>
</div>
<% content_for(:page_title) do %>
/ Upload
<% end %>
<%= render :partial => "posts/common_secondary_nav_links" %>
<%= render :partial => "posts/partials/common/secondary_links" %>

View File

@@ -21,4 +21,4 @@
<% end %>
</p>
<%= render :partial => "posts/common_secondary_nav_links" %>
<%= render :partial => "posts/partials/common/secondary_links" %>