docbrown/app/helpers/comments_helper.rb
dependabot[bot] 76b199e438
Bump rubocop-performance from 1.9.2 to 1.10.1 (#12860)
* Bump rubocop-performance from 1.9.2 to 1.10.1

Bumps [rubocop-performance](https://github.com/rubocop/rubocop-performance) from 1.9.2 to 1.10.1.
- [Release notes](https://github.com/rubocop/rubocop-performance/releases)
- [Changelog](https://github.com/rubocop/rubocop-performance/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rubocop/rubocop-performance/compare/v1.9.2...v1.10.1)

Signed-off-by: dependabot[bot] <support@github.com>

* Enable new cops

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: rhymes <rhymes@hey.com>
2021-03-02 10:37:13 +01:00

75 lines
1.9 KiB
Ruby

module CommentsHelper
MAX_COMMENTS_TO_RENDER = 250
MIN_COMMENTS_TO_RENDER = 8
def comment_class(comment, is_view_root: false)
if comment.root? || is_view_root
"root"
else
"child"
end
end
def comment_user_id_unless_deleted(comment)
comment.deleted ? 0 : comment.user_id
end
def commentable_author_is_op?(commentable, comment)
commentable &&
[
commentable.user_id,
commentable.co_author_ids,
].flatten.any?(comment.user_id)
end
def get_ama_or_op_banner(commentable)
commentable.decorate.cached_tag_list_array.include?("ama") ? "Ask Me Anything" : "Author"
end
def tree_for(comment, sub_comments, commentable)
nested_comments(tree: { comment => sub_comments }, commentable: commentable, is_view_root: true)
end
def should_be_hidden?(comment, root_comment)
comment.hidden_by_commentable_user && comment != root_comment
end
def high_number_of_comments?(comments_number)
comments_number > MAX_COMMENTS_TO_RENDER
end
def view_all_comments?(comments_number)
comments_number > MIN_COMMENTS_TO_RENDER
end
def number_of_comments_to_render
MAX_COMMENTS_TO_RENDER
end
def comment_count(view)
view == "comments" ? MAX_COMMENTS_TO_RENDER : MIN_COMMENTS_TO_RENDER
end
def like_button_text(comment)
case comment.public_reactions_count
when 0
"Like"
when 1
"&nbsp;like"
else
"&nbsp;likes"
end
end
private
def nested_comments(tree:, commentable:, is_view_root: false)
comments = tree.map do |comment, sub_comments|
render("comments/comment", comment: comment, commentable: commentable,
is_view_root: is_view_root, is_childless: sub_comments.empty?,
subtree_html: nested_comments(tree: sub_comments, commentable: commentable))
end
safe_join(comments)
end
end