Class: MessageSerializer

Inherits:
ActiveModel::Serializer
  • Object
show all
Defined in:
app/serializers/message_serializer.rb

Instance Method Summary collapse

Instance Method Details

#attachmentsObject



41
42
43
# File 'app/serializers/message_serializer.rb', line 41

def attachments
  object.attachments.attached? ? object.attachments_urls : []
end

#bodyObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/serializers/message_serializer.rb', line 4

def body
  if object.body.blank? && object.attachments.any?
    attachment = object.attachments.last
    content_type = attachment.blob.content_type

    if content_type.start_with?("image/")
      attachment.blob.filename.to_s
    elsif content_type.start_with?("video/")
      attachment.blob.filename.to_s
    else
      attachment.blob.filename.to_s
    end
  else
    object.body
  end
end

#receiverObject



31
32
33
34
35
36
37
38
39
# File 'app/serializers/message_serializer.rb', line 31

def receiver
  if object.receiver_id.present?
    {
      id: object.receiver_id,
      name: object.receiver.first_name,
      image: object.receiver.profile_image.present? ? object.receiver.profile_image.url : nil
    }
  end
end

#senderObject



21
22
23
24
25
26
27
28
29
# File 'app/serializers/message_serializer.rb', line 21

def sender
  if object.sender_id.present?
    {
      id: object.sender_id,
      name: object.sender.first_name,
      image: object.sender.profile_image.present? ? object.sender.profile_image.url : nil
    }
  end
end