simplifies parent/child preview code; fixes #1489

This commit is contained in:
Toks
2013-04-30 14:54:40 -04:00
parent a2cf238f58
commit 6c3aabf2b6
4 changed files with 34 additions and 14 deletions

View File

@@ -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

View File

@@ -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"))}) "

View 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

View File

@@ -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 %>