From 7b5ce805932fc51215fe714df1295dce3ff5f3a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 24 Aug 2018 09:03:59 +0200 Subject: [PATCH] Simplify MergedCommentTree#initialize By setting the comments lazily in a different method, we can share its code with the parent class. --- lib/comment_tree.rb | 7 +++++-- lib/merged_comment_tree.rb | 8 ++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/comment_tree.rb b/lib/comment_tree.rb index f43ab7e40..eedb64d96 100644 --- a/lib/comment_tree.rb +++ b/lib/comment_tree.rb @@ -2,14 +2,17 @@ class CommentTree ROOT_COMMENTS_PER_PAGE = 10 - attr_accessor :root_comments, :comments, :commentable, :page, :order + attr_reader :root_comments, :commentable, :page, :order def initialize(commentable, page, order = "confidence_score", valuations: false) @commentable = commentable @page = page @order = order @valuations = valuations - @comments = root_comments + root_descendants + end + + def comments + @comments ||= root_comments + root_descendants end def root_comments diff --git a/lib/merged_comment_tree.rb b/lib/merged_comment_tree.rb index 30bacbaf8..efaca5f2f 100644 --- a/lib/merged_comment_tree.rb +++ b/lib/merged_comment_tree.rb @@ -1,14 +1,10 @@ class MergedCommentTree < CommentTree - attr_accessor :commentables, :array_order + attr_reader :commentables, :array_order def initialize(commentables, page, order = "confidence_score") @commentables = commentables - @commentable = commentables.first - @page = page - @order = order + super(commentables.first, page, order) @array_order = set_array_order(order) - - @comments = root_comments + root_descendants end def root_comments