Class: Components::Comment

Inherits:
Base
  • Object
show all
Includes:
Phlex::Rails::Helpers::LinkTo, Phlex::Rails::Helpers::TimeAgoInWords
Defined in:
app/components/comment.rb

Instance Method Summary collapse

Constructor Details

#initialize(comment:, show_system: false) ⇒ Comment

Returns a new instance of Comment.



9
10
11
12
# File 'app/components/comment.rb', line 9

def initialize(comment:, show_system: false)
  @comment = comment
  @show_system = show_system
end

Instance Method Details

#before_template ⇒ Object



18
19
20
# File 'app/components/comment.rb', line 18

def before_template
  @reported = @comment.reports.any?
end

#render? ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'app/components/comment.rb', line 14

def render?
  !@comment.system || @show_system
end

#view_template ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/components/comment.rb', line 22

def view_template
  div class: "comment-component #{"comment-reported" if @reported}", id: "comment-#{@comment.to_param}" do
    div class: "comment-header" do
      div do
        if @comment.system
          Icon icon: "robot", label: t("components.comment.system")
        else
          Icon icon: "chat", role: "presentation"
        end
        whitespace
        span do
          if @comment.commenter.is_a?(Federails::Actor)
            link_to @comment.commenter.name, @comment.commenter.profile_url, title: @comment.commenter.at_address
          else
            @comment.commenter.try(:name) || @comment.commenter.try(:username)
          end
        end
        whitespace
        span(class: "comment-time", title: @comment.created_at) { t("components.comment.posted", time: time_ago_in_words(@comment.created_at)) }
      end
      div class: "comment-header-links" do
        link_to polymorphic_path([@comment.commentable, @comment, :report], action: :new) do
          Icon(icon: "flag", label: t("components.comment.report"))
        end
        whitespace
        if policy(@comment).destroy?
          span(class: "ms-2") do
            link_to [@comment.commentable, @comment], method: :delete, class: "link-danger", data: {confirm: t("components.comment.confirm_delete")} do
              Icon(icon: "trash", label: t("components.comment.delete"))
            end
          end
        end
      end
    end
    div(class: "comment-body") { markdownify(@comment.comment) }
    if current_user&.is_moderator? && @comment.reports.any?
      div(class: "comment-footer comment-footer-reported") do
        span { t("components.comment.reported", time: @comment.reports.order(created_at: :desc).pick(:created_at)) }
      end
    end
  end
end