class Bridgetown::RelatedPosts

TODO: to be retired once the Resource engine is made official

Attributes

lsi[RW]
post[R]
site[R]

Public Class Methods

new(post) click to toggle source
# File lib/bridgetown-core/related_posts.rb, line 12
def initialize(post)
  @post = post
  @site = post.site
  if site.config.lsi
    Bridgetown::Utils::RequireGems.require_with_graceful_fail("classifier-reborn")
  end
end

Public Instance Methods

build() click to toggle source
# File lib/bridgetown-core/related_posts.rb, line 20
def build
  return [] unless site.collections.posts.docs.size > 1

  if site.config.lsi
    build_index
    lsi_related_posts
  else
    most_recent_posts
  end
end
build_index() click to toggle source
# File lib/bridgetown-core/related_posts.rb, line 31
def build_index
  self.class.lsi ||= begin
    lsi = ClassifierReborn::LSI.new(auto_rebuild: false)
    Bridgetown.logger.info("Populating LSI...")

    site.collections.posts.docs.each do |x|
      lsi.add_item(x)
    end

    Bridgetown.logger.info("Rebuilding index...")
    lsi.build_index
    Bridgetown.logger.info("")
    lsi
  end
end
most_recent_posts() click to toggle source
# File lib/bridgetown-core/related_posts.rb, line 51
def most_recent_posts
  @most_recent_posts ||= (site.collections.posts.docs.last(11).reverse - [post]).first(10)
end