summaryrefslogtreecommitdiff
path: root/app/serializers/comment_serializer.rb
blob: 59124bf717c4d35acd5253cbe5ee359eca0da14e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
# frozen_string_literal: true

# == Schema Information
#
# Table name: comments
#
#  id                :integer          not null, primary key
#  anonymous         :boolean          default(FALSE)
#  body              :string           not null
#  deletion_reason   :string           default(""), not null
#  destroyed_content :boolean          default(FALSE)
#  edit_reason       :string
#  edited_at         :datetime
#  hidden_from_users :boolean          default(FALSE), not null
#  ip                :inet
#  name_at_post_time :string
#  referrer          :string           default("")
#  show_staff_tag    :boolean          default(FALSE), not null
#  tripcode          :text
#  user_agent        :string           default("")
#  created_at        :datetime         not null
#  updated_at        :datetime         not null
#  deleted_by_id     :integer
#  post_id           :integer
#  user_id           :integer
#
# Indexes
#
#  index_comments_on_created_at     (created_at)
#  index_comments_on_deleted_by_id  (deleted_by_id) WHERE (deleted_by_id IS NOT NULL)
#  index_comments_on_post_id        (post_id)
#  index_comments_on_user_id        (user_id)
#
# Foreign Keys
#
#  fk_rails_...  (deleted_by_id => users.id) ON DELETE => nullify ON UPDATE => cascade
#  fk_rails_...  (post_id => posts.id)
#  fk_rails_...  (user_id => users.id) ON DELETE => nullify ON UPDATE => cascade
#
class CommentSerializer < BaseSerializer
  # @param [Comment] comment
  def self.serialize(comment, options = {})
    attrs = {
      id: comment.id,
      created_at: comment.created_at, # DateTime - The creation time, in UTC, of the comment.
      updated_at: comment.updated_at, # DateTime - The time, in UTC, the comment was last updated at.
      hidden_from_users: comment.hidden_from_users?
    }

    attrs = attrs.merge({ body: comment.body }) unless comment.hidden_from_users?  # String - The comment text.

    attrs
  end
end