Skip to content

Commit b3ca34f

Browse files
committed
added sorting to full user inbox
1 parent b9f4276 commit b3ca34f

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

app/controllers/notifications_controller.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ class NotificationsController < ApplicationController
33
include CommentsHelper
44

55
before_action :authenticate_user!, only: [:index]
6+
before_action :set_sorting, only: [:index]
67

78
def index
89
@notifications = Notification.unscoped
910
.where(user: current_user)
11+
.order(:is_read => :asc, @sort_type => @sort_order)
1012
.paginate(page: params[:page], per_page: 100)
1113
.order(Arel.sql('is_read ASC, created_at DESC'))
1214

@@ -78,4 +80,17 @@ def read_all
7880
end
7981
end
8082
end
83+
84+
private
85+
86+
def set_sorting
87+
sort_orders = { asc: :asc, desc: :desc }
88+
sort_types = { age: :created_at }
89+
90+
@default_sort_type = :age
91+
@default_sort_order = :desc
92+
93+
@sort_order = sort_orders[params[:order]&.to_sym] || sort_orders[@default_sort_order]
94+
@sort_type = sort_types[params[:sort]&.to_sym] || sort_types[@default_sort_type]
95+
end
8196
end

app/views/notifications/index.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
<%= link_to 'Unread', query_url(status: 'unread'),
1717
class: "button is-muted is-outlined #{'is-active' if status == 'unread'}" %>
1818
</div>
19+
20+
<%= render 'shared/sorting', default_order: @default_sort_order,
21+
default_type: @default_sort_type,
22+
types: %w[age] %>
1923
</div>
2024

2125
<% @notifications.each do |notif| %>

0 commit comments

Comments
 (0)