Files
fizzy/app/models/bubble/thread.rb
T
2024-10-11 20:27:32 -05:00

40 lines
818 B
Ruby

class Bubble::Thread
attr_reader :bubble
def initialize(bubble)
@bubble = bubble
end
def entries
sorted_entries.chunk_while { |a, b| consecutive_events?(a, b) }.map.with_index { |entries, index| roll_up(entries, index) }
end
def to_partial_path
"bubbles/threads/thread"
end
def cache_key
ActiveSupport::Cache.expand_cache_key bubble, :thread
end
private
delegate :events, :comments, to: :bubble, private: true
def sorted_entries
(events + comments).sort_by(&:created_at)
end
def consecutive_events?(a, b)
[ a, b ] in [ Event, Event ]
end
def roll_up(entries, position)
case entries.first
when Comment
entries.sole
when Event
Rollup.new self, entries, first_position: position.zero?
end
end
end