posts/show: refactor parent/child notices.
* Convert notices from helpers to partials. * Eliminate PostSets::PostRelationship class in favor of post_sets/posts template. * Eliminate COUNT(*) queries when calculating the number of child posts. * Eliminate redundant parent load and parent exists queries.
This commit is contained in:
@@ -27,8 +27,11 @@ class PostsController < ApplicationController
|
||||
@comments = @comments.visible(CurrentUser.user)
|
||||
|
||||
include_deleted = @post.is_deleted? || (@post.parent_id.present? && @post.parent.is_deleted?) || CurrentUser.user.show_deleted_children?
|
||||
@parent_post_set = PostSets::PostRelationship.new(@post.parent_id, :include_deleted => include_deleted)
|
||||
@children_post_set = PostSets::PostRelationship.new(@post.id, :include_deleted => include_deleted)
|
||||
@sibling_posts = @post.parent.present? ? @post.parent.children : Post.none
|
||||
@sibling_posts = @sibling_posts.undeleted unless include_deleted
|
||||
|
||||
@child_posts = @post.children
|
||||
@child_posts = @child_posts.undeleted unless include_deleted
|
||||
|
||||
respond_with(@post) do |format|
|
||||
format.html.tooltip { render layout: false }
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
module PostsHelper
|
||||
def post_previews_html(posts, **options)
|
||||
posts.map do |post|
|
||||
PostPresenter.preview(post, **options)
|
||||
end.join("").html_safe
|
||||
end
|
||||
|
||||
def post_search_counts_enabled?
|
||||
Danbooru.config.enable_post_search_counts && Danbooru.config.reportbooru_server.present? && Danbooru.config.reportbooru_key.present?
|
||||
end
|
||||
@@ -63,45 +69,6 @@ module PostsHelper
|
||||
post.favorited_users.reverse_each.map {|user| link_to_user(user)}.join(", ").html_safe
|
||||
end
|
||||
|
||||
def has_parent_message(post, parent_post_set)
|
||||
html = ""
|
||||
|
||||
html << "This post belongs to a "
|
||||
html << link_to("parent", posts_path(:tags => "parent:#{post.parent_id}"))
|
||||
html << " (deleted)" if parent_post_set.parent.first.is_deleted?
|
||||
|
||||
sibling_count = parent_post_set.children.count - 1
|
||||
if sibling_count > 0
|
||||
html << " and has "
|
||||
text = (sibling_count == 1) ? "a sibling" : "#{sibling_count} siblings"
|
||||
html << link_to(text, posts_path(:tags => "parent:#{post.parent_id}"))
|
||||
end
|
||||
|
||||
html << " (#{link_to_wiki "learn more", "help:post_relationships"}) "
|
||||
|
||||
html << link_to("« hide".html_safe, "#", :id => "has-parent-relationship-preview-link")
|
||||
|
||||
html.html_safe
|
||||
end
|
||||
|
||||
def has_children_message(post, children_post_set)
|
||||
html = ""
|
||||
|
||||
html << "This post has "
|
||||
text = (children_post_set.children.count == 1) ? "a child" : "#{children_post_set.children.count} children"
|
||||
html << link_to(text, posts_path(:tags => "parent:#{post.id}"))
|
||||
|
||||
html << " (#{link_to_wiki "learn more", "help:post_relationships"}) "
|
||||
|
||||
html << link_to("« hide".html_safe, "#", :id => "has-children-relationship-preview-link")
|
||||
|
||||
html.html_safe
|
||||
end
|
||||
|
||||
def pool_link(pool)
|
||||
render("posts/partials/show/pool_link", post: @post, pool: pool)
|
||||
end
|
||||
|
||||
def is_pool_selected?(pool)
|
||||
return false if params.key?(:q)
|
||||
return false if params.key?(:favgroup_id)
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
module PostSets
|
||||
class PostRelationship < PostSets::Post
|
||||
attr_reader :parent, :children
|
||||
|
||||
def initialize(parent_id, options = {})
|
||||
@parent = ::Post.where("id = ?", parent_id)
|
||||
@children = ::Post.where("parent_id = ?", parent_id).order("id ASC")
|
||||
if options[:include_deleted]
|
||||
super("parent:#{parent_id} status:any")
|
||||
else
|
||||
@children = @children.where("is_deleted = ?", false)
|
||||
super("parent:#{parent_id}")
|
||||
end
|
||||
end
|
||||
|
||||
def posts
|
||||
@parent + @children
|
||||
end
|
||||
|
||||
def presenter
|
||||
::PostSetPresenters::Post.new(self)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1239,10 +1239,6 @@ class Post < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def parent_exists?
|
||||
Post.exists?(parent_id)
|
||||
end
|
||||
|
||||
def has_visible_children?
|
||||
return true if has_active_children?
|
||||
return true if has_children? && CurrentUser.user.show_deleted_children?
|
||||
|
||||
8
app/views/posts/partials/show/_child_notice.html.erb
Normal file
8
app/views/posts/partials/show/_child_notice.html.erb
Normal file
@@ -0,0 +1,8 @@
|
||||
This post has <%= link_to pluralize(children.length, "child"), posts_path(tags: "parent:#{parent.id}") %>
|
||||
|
||||
(<%= link_to_wiki "learn more", "help:post_relationships" %>)
|
||||
<%= link_to("« hide", "#", id: "has-children-relationship-preview-link") %>
|
||||
|
||||
<div id="has-children-relationship-preview">
|
||||
<%= post_previews_html([parent, *children], tags: "parent:#{parent.id}", show_deleted: true) %>
|
||||
</div>
|
||||
@@ -55,17 +55,15 @@
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<% if post.parent_id && post.parent_exists? %>
|
||||
<% if post.parent.present? %>
|
||||
<div class="notice notice-small post-notice post-notice-child">
|
||||
<%= has_parent_message(post, @parent_post_set) %>
|
||||
<div id="has-parent-relationship-preview"><%= @parent_post_set.presenter.post_previews_html(self) %></div>
|
||||
<%= render "posts/partials/show/parent_notice", parent: post.parent, children: @sibling_posts.to_a %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<% if post.has_visible_children? %>
|
||||
<div class="notice notice-small post-notice post-notice-parent">
|
||||
<%= has_children_message(post, @children_post_set) %>
|
||||
<div id="has-children-relationship-preview"><%= @children_post_set.presenter.post_previews_html(self) %></div>
|
||||
<%= render "posts/partials/show/child_notice", parent: post, children: @child_posts.to_a %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
|
||||
14
app/views/posts/partials/show/_parent_notice.html.erb
Normal file
14
app/views/posts/partials/show/_parent_notice.html.erb
Normal file
@@ -0,0 +1,14 @@
|
||||
This post belongs to a <%= link_to "parent", posts_path(tags: "parent:#{parent.id}") %> <% "(deleted)" if parent.is_deleted? %>
|
||||
|
||||
<% children.length.tap do |children_count| %>
|
||||
<% if children_count > 1 %>
|
||||
and has <%= link_to pluralize(children_count - 1, "sibling"), posts_path(tags: "parent:#{parent.id}") %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
(<%= link_to_wiki "learn more", "help:post_relationships" %>)
|
||||
<%= link_to "« hide", "#", id: "has-parent-relationship-preview-link" %>
|
||||
|
||||
<div id="has-parent-relationship-preview">
|
||||
<%= post_previews_html([parent, *children], tags: "parent:#{parent.id}", show_deleted: true) %>
|
||||
</div>
|
||||
Reference in New Issue
Block a user