Class: SuggestedTopicsBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/suggested_topics_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(topic) ⇒ SuggestedTopicsBuilder

Returns a new instance of SuggestedTopicsBuilder.



6
7
8
9
10
11
# File 'lib/suggested_topics_builder.rb', line 6

def initialize(topic)
  @excluded_topic_ids = [topic.id]
  @category_id = topic.category_id
  @category_topic_ids = Category.topic_ids
  @results = []
end

Instance Attribute Details

#excluded_topic_idsObject (readonly)

Returns the value of attribute excluded_topic_ids.



4
5
6
# File 'lib/suggested_topics_builder.rb', line 4

def excluded_topic_ids
  @excluded_topic_ids
end

Instance Method Details

#add_results(results, priority = :low) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/suggested_topics_builder.rb', line 13

def add_results(results, priority = :low)
  # WARNING .blank? will execute an Active Record query
  return unless results

  # Only add results if we don't have those topic ids already
  results = results.where("topics.id NOT IN (?)", @excluded_topic_ids).where(visible: true)

  # If limit suggested to category is enabled, restrict to that category
  if @category_id && SiteSetting.limit_suggested_to_category?
    results = results.where(category_id: @category_id)
  end

  results = results.to_a
  results.reject! { |topic| @category_topic_ids.include?(topic.id) }

  unless results.empty?
    # protect against dupes
    temp = results
    results = []
    temp.each do |r|
      if !@excluded_topic_ids.include?(r.id)
        results << r
        @excluded_topic_ids << r.id
      end
    end

    splice_results(results, priority)
  end
end

#category_results_leftObject



75
76
77
# File 'lib/suggested_topics_builder.rb', line 75

def category_results_left
  SiteSetting.suggested_topics - @results.count { |r| r.category_id == @category_id }
end

#full?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/suggested_topics_builder.rb', line 71

def full?
  results_left <= 0
end

#resultsObject



63
64
65
# File 'lib/suggested_topics_builder.rb', line 63

def results
  @results.first(SiteSetting.suggested_topics)
end

#results_leftObject



67
68
69
# File 'lib/suggested_topics_builder.rb', line 67

def results_left
  SiteSetting.suggested_topics - @results.size
end

#sizeObject



79
80
81
# File 'lib/suggested_topics_builder.rb', line 79

def size
  @results.size
end

#splice_results(results, priority) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/suggested_topics_builder.rb', line 43

def splice_results(results, priority)
  if priority == :ultra_high
    @results.insert 0, *results
  elsif @category_id && priority == :high
    # Topics from category @category_id need to be first in the list, all others after.
    other_category_index = @results.index { |r| r.category_id != @category_id }
    category_results, other_category_results =
      results.partition { |r| r.category_id == @category_id }

    if other_category_index
      @results.insert other_category_index, *category_results
    else
      @results.concat category_results
    end
    @results.concat other_category_results
  else
    @results.concat results
  end
end