mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-10-08 15:50:08 +09:00
Compose sidebar lists from existing room scopes
Directs merge Room.directs and order by recency. Shared rooms reuse with_ordered_room and without_direct_rooms, with the STI filter coming from Room.without_directs so the join alias stays rooms. Co-authored-by: Thomas Klemm <github@tklemm.eu>
This commit is contained in:
@@ -4,7 +4,7 @@ class Users::SidebarsController < ApplicationController
|
|||||||
def show
|
def show
|
||||||
visible_memberships = Current.user.memberships.visible
|
visible_memberships = Current.user.memberships.visible
|
||||||
@direct_memberships = visible_memberships.with_direct_rooms
|
@direct_memberships = visible_memberships.with_direct_rooms
|
||||||
@other_memberships = visible_memberships.with_ordered_shared_rooms
|
@other_memberships = visible_memberships.with_ordered_room.without_direct_rooms
|
||||||
|
|
||||||
@direct_placeholder_users = find_direct_placeholder_users
|
@direct_placeholder_users = find_direct_placeholder_users
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -9,9 +9,8 @@ class Membership < ApplicationRecord
|
|||||||
enum :involvement, %w[ invisible nothing mentions everything ].index_by(&:itself), prefix: :involved_in
|
enum :involvement, %w[ invisible nothing mentions everything ].index_by(&:itself), prefix: :involved_in
|
||||||
|
|
||||||
scope :with_ordered_room, -> { includes(:room).joins(:room).order("LOWER(rooms.name)") }
|
scope :with_ordered_room, -> { includes(:room).joins(:room).order("LOWER(rooms.name)") }
|
||||||
scope :with_direct_rooms, -> { includes(:room).joins(:room).where(rooms: { type: "Rooms::Direct" }).order(rooms: { updated_at: :desc }) }
|
scope :with_direct_rooms, -> { includes(:room).joins(:room).merge(Room.directs).order(rooms: { updated_at: :desc }) }
|
||||||
scope :with_ordered_shared_rooms, -> { includes(:room).joins(:room).where.not(rooms: { type: "Rooms::Direct" }).order("LOWER(rooms.name)") }
|
scope :without_direct_rooms, -> { joins(:room).merge(Room.without_directs) }
|
||||||
scope :without_direct_rooms, -> { joins(:room).where.not(room: { type: "Rooms::Direct" }) }
|
|
||||||
|
|
||||||
scope :visible, -> { where.not(involvement: :invisible) }
|
scope :visible, -> { where.not(involvement: :invisible) }
|
||||||
scope :unread, -> { where.not(unread_at: nil) }
|
scope :unread, -> { where.not(unread_at: nil) }
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ class Users::SidebarsControllerTest < ActionDispatch::IntegrationTest
|
|||||||
assert_select ".unread", count: users(:david).memberships.reject { |m| m.room.direct? || !m.unread? }.count
|
assert_select ".unread", count: users(:david).memberships.reject { |m| m.room.direct? || !m.unread? }.count
|
||||||
end
|
end
|
||||||
|
|
||||||
test "directs are queried separately and ordered by room recency, not name" do
|
test "directs are ordered by room recency, not name" do
|
||||||
older = rooms(:david_and_jason)
|
older = rooms(:david_and_jason)
|
||||||
newer = rooms(:david_and_kevin)
|
newer = rooms(:david_and_kevin)
|
||||||
older.update_column :updated_at, 2.days.ago
|
older.update_column :updated_at, 2.days.ago
|
||||||
|
|||||||
Reference in New Issue
Block a user