Browse Source

Add index action to Conversation controller

Frans Bergman 7 years ago
parent
commit
826797c14f

+ 6 - 0
app/controllers/conversations_controller.rb

@@ -3,6 +3,12 @@ class ConversationsController < ApplicationController
   before_action :set_conversation, only: [:show]
   before_action :check_permission, only: [:show]
 
+  def index
+    @conversations = current_user.conversations.sort_by{
+      |c| c.messages.last.created_at
+    }
+  end
+
   def show
   end
 

+ 15 - 0
app/views/conversations/_conversation.html.erb

@@ -0,0 +1,15 @@
+<tr>
+  <td>
+    <%= link_to conversation.name, conversation %>
+  </td>
+  <td>
+    <% conversation.users.each do |user| %>
+      <%= link_to user_url(user), data_toggle: "tooltip", title: user.name do %>
+        <%= image_tag user.picture.tiny.url %>
+      <% end %>
+    <% end %>
+  </td>
+  <td>
+    <b><%= conversation.messages.last.user.name %>:</b> <%= truncate(conversation.messages.last.content) %>
+  </td>
+</tr>

+ 20 - 0
app/views/conversations/index.html.erb

@@ -0,0 +1,20 @@
+<% provide(:title, "Conversations") %>
+<h1>Conversations</h1>
+<table width="100%" class="table table-striped table-bordered table-hover">
+  <thead>
+    <tr>
+      <th>
+        Conversation
+      </th>
+      <th>
+        Participants
+      </th>
+      <th>
+        Last message
+      </th>
+    </tr>
+  </thead>
+  <tbody>
+    <%= render @conversations %>
+  </tbody>
+</table>

+ 3 - 0
app/views/layouts/_navigation.html.erb

@@ -45,6 +45,9 @@
                 <li>
                     <%= link_to fa_icon("home fw", text: "Home"), home_path %>
                 </li>
+                <li>
+                    <%= link_to fa_icon("envelope fw", text: "Conversations"), conversations_path %>
+                </li>
             </ul>
         </div>
     </div>

+ 8 - 0
test/controllers/conversations_controller_test.rb

@@ -11,4 +11,12 @@ class ConversationsControllerTest < ActionDispatch::IntegrationTest
     get conversation_path conversations(:two)
     assert_redirected_to root_path
   end
+
+  test "last message should be shown in index" do
+    message = @user.messages.build(content: "Hello, world!",
+                                   conversation: conversations(:one))
+    message.save
+    get conversations_path
+    assert_match message.content, response.body
+  end
 end