ruby-on-railsrubymailboxer

How can I have an "unread message" next to each message show in the inbox section of the mailbox?


Question: How can I have a 'Message not read' appear next to each message within my inbox?

I have created an internal mailing system for my app, using the mailboxer gem. My issue is when I go into the inbox section, and view all the messages that have been sent to my user account, I want to be able to see which messages I have not read.

My code:

<div class="media-body">
    <h4 class="media-heading">
    <b> Sender:  </b> <%=  conversation.originator.username %> <br>
    </h4>
<small><b>Subject: </b><%= conversation.subject %></small><br>
<small><b>Date: </b><%=  conversation.messages.last.created_at.strftime("%A, %b %d, %Y at %I:%M%p") %></small><br>
<b> Recent message: </b> <%= truncate conversation.messages.last.body, length: 145 %>
    <%= link_to "View", conversation_path(conversation)  %>
</div>

This code works out, of the messages in the inbox, which ones are unread, and tallies them so I know which messages are not read.

module MailboxHelper
    def unread_messages_count
    # how to get the number of unread messages for the current user
    # using mailboxer
        mailbox.inbox(:unread => true).count(:id, :distinct => true)
    end
end

The only thing I can identify in the schema, that was generated by t he mailboxer gem, is the boolean is_read:

create_table "mailboxer_receipts", force: :cascade do |t|
    t.integer  "receiver_id"
    t.string   "receiver_type"
    t.integer  "notification_id",                            null: false
    t.boolean  "is_read",                    default: false
    t.boolean  "trashed",                    default: false
    t.boolean  "deleted",                    default: false
    t.string   "mailbox_type",    limit: 25
    t.datetime "created_at",                                 null: false
    t.datetime "updated_at",                                 null: false
    t.boolean  "is_delivered",               default: false
    t.string   "delivery_method"
    t.string   "message_id"
  end

I guess, based on this information, I'm unsure how to tie everything together so when I'm in the inbox view, and see all my messages, I want a "Not read" or something appearing for each message in the inbox. How could I do this?

Also, what other information do you require? The mailboxer gem generated a fair bit of code already, but I wasn't sure what is relevant to this question.


Solution

  • actually mailboxer has method in conversation record is_unread?(current_user) you can use it for your task above

    below for example your conversations controller

    @mailbox ||= current_user.mailbox
    @conversations = @mailbox.inbox
    

    in your view files (index_inbox.html.erb)

    <%=  @conversations.each do |conversation| %>
      # this to show subject that link to conversation
      <%= link_to conversation.subject, conversation_path(conversation) %>
      <% if conversation.is_unread?(current_user) %>
        (unread message)
      <% end %>
    <% end %>