Skip to content

Commit afd86c8

Browse files
committed
Paginate messages by published year/month
1 parent 5e900c6 commit afd86c8

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

app/controllers/messages_controller.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ class MessagesController < ApplicationController
22
PER_PAGE = 50
33

44
# GET /ruby-dev or /q=searchterm
5-
def index(list_name: nil, q: nil, page: nil)
5+
def index(list_name: nil, yyyymm: nil, q: nil, page: nil)
66
if list_name
77
@list = List.find_by_name list_name
88

9-
messages = Message.with_recursive(parent_and_children: [Message.where(list_id: @list, parent_id: nil).order(:id).limit(100), Message.joins('inner join parent_and_children on messages.parent_id = parent_and_children.id')])
9+
@yyyymms = Message.where(list_id: @list).order('yyyymm').pluck(Arel.sql "distinct to_char(published_at, 'YYYYMM') as yyyymm")
10+
@yyyymm = yyyymm || @yyyymms.last
11+
12+
root_query = Message.where(list_id: @list, parent_id: nil).where("to_char(published_at, 'YYYYMM') = ?", @yyyymm).order(:id)
13+
messages = Message.with_recursive(parent_and_children: [root_query, Message.joins('inner join parent_and_children on messages.parent_id = parent_and_children.id')])
1014
.joins('inner join parent_and_children on parent_and_children.id = messages.id')
15+
1116
@messages = compose_tree(messages)
1217
elsif q
1318
search q, page

app/views/messages/index.html.erb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@
66
</div>
77
<% end %>
88

9+
<div class="mb-4 border border-gray-200 dark:border-gray-700 rounded-lg p-3 bg-white dark:bg-gray-800">
10+
<% @yyyymms.group_by {|yyyymm| yyyymm[0, 4] }.sort.each do |year, months| %>
11+
<div class="flex gap-1 items-center mb-1">
12+
<span class="text-sm font-medium text-gray-700 dark:text-gray-300 w-12"><%= year %>:</span>
13+
<div class="flex gap-1 flex-wrap">
14+
<% months.sort.each do |yyyymm| %>
15+
<%= link_to [@list, yyyymm: yyyymm], class: "px-2 py-1 text-xs font-medium rounded transition-colors #{@yyyymm == yyyymm ? 'bg-red-600 dark:bg-red-500 text-white border-2 border-red-700 dark:border-red-400' : 'bg-gray-100 dark:bg-gray-900 text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 border-2 border-transparent'}" do %>
16+
<%= yyyymm[4..] %>
17+
<% end %>
18+
<% end %>
19+
</div>
20+
</div>
21+
<% end %>
22+
</div>
23+
924
<div class="grid grid-cols-4 gap-6" style="height: calc(100vh - 16rem);" data-controller="message-list">
1025
<div class="col-span-1 overflow-y-auto space-y-6">
1126
<%= render partial: 'thread', collection: @messages, as: :message, locals: {list: @list, depth: 0} %>

0 commit comments

Comments
 (0)