Class: Course::Discussion::Post::OrderingConcern::PostSort

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
app/models/concerns/course/discussion/post/ordering_concern.rb

Overview

Sorts all posts in a collection in topological order.

By convention, each post is represented by an array. The first element is the post itself, the second is the children of the array.

Instance Method Summary collapse

Constructor Details

#initialize(posts) ⇒ PostSort

Constructor.

Parameters:


19
20
21
22
# File 'app/models/concerns/course/discussion/post/ordering_concern.rb', line 19

def initialize(posts)
  @posts = posts
  @sorted = sort(nil)
end

Instance Method Details

#lastCourse::Discussion::Post?

Retrieves the last post topologically -- the last post at every branch.

Returns:


28
29
30
31
32
33
34
# File 'app/models/concerns/course/discussion/post/ordering_concern.rb', line 28

def last
  current_thread = @sorted.last
  return nil unless current_thread

  current_thread = current_thread.second.last until current_thread.second.empty?
  current_thread.first
end