diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 191ae621f..87df1473c 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -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 diff --git a/app/helpers/posts_helper.rb b/app/helpers/posts_helper.rb index b759efbed..7f09d3af9 100644 --- a/app/helpers/posts_helper.rb +++ b/app/helpers/posts_helper.rb @@ -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"))}) " diff --git a/app/logical/post_sets/post_relationship.rb b/app/logical/post_sets/post_relationship.rb new file mode 100644 index 000000000..2cd085590 --- /dev/null +++ b/app/logical/post_sets/post_relationship.rb @@ -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 diff --git a/app/views/posts/partials/show/_notices.html.erb b/app/views/posts/partials/show/_notices.html.erb index 73e21be68..bdf81b3f5 100644 --- a/app/views/posts/partials/show/_notices.html.erb +++ b/app/views/posts/partials/show/_notices.html.erb @@ -48,8 +48,8 @@ <% if post.parent_id %>
- <%= has_parent_message(post, @parent_post_set, @siblings_post_set) %> -
<%= @parent_post_set.presenter.post_previews_html(self) %><%= @siblings_post_set.presenter.post_previews_html(self) %>
+ <%= has_parent_message(post, @parent_post_set) %> +
<%= @parent_post_set.presenter.post_previews_html(self) %>
<% end %>