Module: Nesta::Plugin::RelatedByCategory

Defined in:
lib/nesta-plugin-related-by-category/init.rb,
lib/nesta-plugin-related-by-category/version.rb

Defined Under Namespace

Modules: Helpers

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.articles_through_categories(page) ⇒ Object



28
29
30
31
32
33
# File 'lib/nesta-plugin-related-by-category/init.rb', line 28

def self.articles_through_categories(page)
  related = page.categories.map {|c| c.articles}.flatten
  set = Set.new(related).to_a
  set.delete page
  set
end

.display(results, number) ⇒ Object



43
44
45
# File 'lib/nesta-plugin-related-by-category/init.rb', line 43

def self.display(results, number)
  results.take(number)
end

.for_page(page, number) ⇒ Object



47
48
49
# File 'lib/nesta-plugin-related-by-category/init.rb', line 47

def self.for_page(page, number)
  display(ranked_by_relevance(page), number)
end

.ranked_by_relevance(page) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nesta-plugin-related-by-category/init.rb', line 15

def self.ranked_by_relevance(page)
  results = {}
  articles_through_categories(page).each do |article|
    rank = relevance(page, article)
    if results.has_key? rank
      results[rank] << article
    else
      results.store(rank, [article])
    end
  end
  results.sort.reverse.flatten.reject {|el| !el.class.eql? Page}
end

.relevance(page, other) ⇒ Object



35
36
37
# File 'lib/nesta-plugin-related-by-category/init.rb', line 35

def self.relevance(page, other)
  (tags(page) & tags(other)).count
end

.tags(page) ⇒ Object



39
40
41
# File 'lib/nesta-plugin-related-by-category/init.rb', line 39

def self.tags(page)
  page.categories.map {|c| c.heading}
end