mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-10-08 15:50:08 +09:00
d6b3dfd64d
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>
26 lines
760 B
Ruby
26 lines
760 B
Ruby
class Membership < ApplicationRecord
|
|
include Connectable
|
|
|
|
belongs_to :room
|
|
belongs_to :user
|
|
|
|
after_destroy_commit { user.reset_remote_connections }
|
|
|
|
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_direct_rooms, -> { includes(:room).joins(:room).merge(Room.directs).order(rooms: { updated_at: :desc }) }
|
|
scope :without_direct_rooms, -> { joins(:room).merge(Room.without_directs) }
|
|
|
|
scope :visible, -> { where.not(involvement: :invisible) }
|
|
scope :unread, -> { where.not(unread_at: nil) }
|
|
|
|
def read
|
|
update!(unread_at: nil)
|
|
end
|
|
|
|
def unread?
|
|
unread_at.present?
|
|
end
|
|
end
|