simplifies parent/child preview code; fixes #1489
This commit is contained in:
@@ -22,13 +22,8 @@ class PostsController < ApplicationController
|
||||
@post = Post.find(params[:id])
|
||||
@post_flag = PostFlag.new(:post_id => @post.id)
|
||||
@post_appeal = PostAppeal.new(:post_id => @post.id)
|
||||
|
||||
@children_post_set = PostSets::Post.new("parent:#{@post.id} -id:#{@post.id}", 1, 200)
|
||||
@children_post_set.posts.reverse!
|
||||
@parent_post_set = PostSets::Post.new("id:#{@post.parent_id} status:any")
|
||||
@siblings_post_set = PostSets::Post.new("parent:#{@post.parent_id} -id:#{@post.parent_id}", 1, 200)
|
||||
@siblings_post_set.posts.reverse!
|
||||
|
||||
@parent_post_set = PostSets::PostRelationship.new(@post.parent_id, :include_deleted => @post.is_deleted?)
|
||||
@children_post_set = PostSets::PostRelationship.new(@post.id, :include_deleted => @post.is_deleted?)
|
||||
respond_with(@post)
|
||||
end
|
||||
|
||||
|
||||
@@ -41,16 +41,17 @@ module PostsHelper
|
||||
end
|
||||
end
|
||||
|
||||
def has_parent_message(post, parent_post_set, siblings_post_set)
|
||||
def has_parent_message(post, parent_post_set)
|
||||
html = ""
|
||||
|
||||
html << "This post belongs to a "
|
||||
html << link_to("parent", post_path(post.parent_id))
|
||||
html << " (deleted)" if parent_post_set.posts.first.is_deleted?
|
||||
html << " (deleted)" if parent_post_set.parent.first.is_deleted?
|
||||
|
||||
if siblings_post_set.posts.count > 1
|
||||
sibling_count = parent_post_set.children.count - 1
|
||||
if sibling_count > 0
|
||||
html << " and has "
|
||||
text = siblings_post_set.posts.count > 2 ? "#{siblings_post_set.posts.count - 1} siblings" : "a sibling"
|
||||
text = sibling_count == 1 ? "a sibling" : "#{sibling_count} siblings"
|
||||
html << link_to(text, posts_path(:tags => "parent:#{post.parent_id}"))
|
||||
end
|
||||
|
||||
@@ -65,7 +66,7 @@ module PostsHelper
|
||||
html = ""
|
||||
|
||||
html << "This post has "
|
||||
text = children_post_set.posts.count == 1 ? "a child" : "#{children_post_set.posts.count} children"
|
||||
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("learn more", wiki_pages_path(:title => "help:post_relationships"))}) "
|
||||
|
||||
24
app/logical/post_sets/post_relationship.rb
Normal file
24
app/logical/post_sets/post_relationship.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
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
|
||||
@@ -48,8 +48,8 @@
|
||||
|
||||
<% if post.parent_id %>
|
||||
<div class="ui-corner-all ui-state-highlight notice notice-child">
|
||||
<%= has_parent_message(post, @parent_post_set, @siblings_post_set) %>
|
||||
<div id="has-parent-relationship-preview"><%= @parent_post_set.presenter.post_previews_html(self) %><%= @siblings_post_set.presenter.post_previews_html(self) %></div>
|
||||
<%= has_parent_message(post, @parent_post_set) %>
|
||||
<div id="has-parent-relationship-preview"><%= @parent_post_set.presenter.post_previews_html(self) %></div>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user