2013-07-12 14:38:20 -04:00
|
|
|
require_dependency 'topic_list'
|
|
|
|
|
|
|
|
class SuggestedTopicsBuilder
|
|
|
|
|
|
|
|
attr_reader :excluded_topic_ids
|
|
|
|
|
|
|
|
def initialize(topic)
|
|
|
|
@excluded_topic_ids = [topic.id]
|
2013-08-27 20:51:49 -04:00
|
|
|
@category_id = topic.category_id
|
2013-11-21 16:43:22 -05:00
|
|
|
@category_topic_ids = Category.pluck(:topic_id).compact
|
2013-07-12 14:38:20 -04:00
|
|
|
@results = []
|
|
|
|
end
|
|
|
|
|
2013-08-27 20:51:49 -04:00
|
|
|
|
|
|
|
def add_results(results, priority=:low)
|
2013-07-12 14:38:20 -04:00
|
|
|
|
2013-07-29 19:54:29 -04:00
|
|
|
# WARNING .blank? will execute an Active Record query
|
|
|
|
return unless results
|
2013-07-12 14:38:20 -04:00
|
|
|
|
|
|
|
# Only add results if we don't have those topic ids already
|
|
|
|
results = results.where('topics.id NOT IN (?)', @excluded_topic_ids)
|
|
|
|
.where(closed: false, archived: false, visible: true)
|
2013-07-29 19:54:29 -04:00
|
|
|
.to_a
|
2013-11-21 16:43:22 -05:00
|
|
|
.reject { |topic| @category_topic_ids.include?(topic.id) }
|
2013-07-12 14:38:20 -04:00
|
|
|
|
2013-07-29 19:54:29 -04:00
|
|
|
unless results.empty?
|
|
|
|
# Keep track of the ids we've added
|
|
|
|
@excluded_topic_ids.concat results.map {|r| r.id}
|
2013-08-27 20:51:49 -04:00
|
|
|
splice_results(results,priority)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def splice_results(results, priority)
|
|
|
|
if @category_id &&
|
|
|
|
priority == :high &&
|
|
|
|
non_category_index = @results.index{|r| r.category_id != @category_id}
|
|
|
|
|
|
|
|
category_results, non_category_results = results.partition{|r| r.category_id == @category_id}
|
|
|
|
|
|
|
|
@results.insert non_category_index, *category_results
|
|
|
|
@results.concat non_category_results
|
|
|
|
else
|
2013-07-29 19:54:29 -04:00
|
|
|
@results.concat results
|
|
|
|
end
|
2013-07-12 14:38:20 -04:00
|
|
|
end
|
|
|
|
|
2013-08-27 20:51:49 -04:00
|
|
|
def results
|
|
|
|
@results.first(SiteSetting.suggested_topics)
|
|
|
|
end
|
|
|
|
|
2013-07-12 14:38:20 -04:00
|
|
|
def results_left
|
|
|
|
SiteSetting.suggested_topics - @results.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def full?
|
2013-08-27 20:51:49 -04:00
|
|
|
results_left <= 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def category_results_left
|
|
|
|
SiteSetting.suggested_topics - @results.count{|r| r.category_id == @category_id}
|
|
|
|
end
|
|
|
|
|
|
|
|
def category_full?
|
|
|
|
if @category_id
|
|
|
|
|
|
|
|
else
|
|
|
|
full?
|
|
|
|
end
|
2013-07-12 14:38:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def size
|
|
|
|
@results.size
|
|
|
|
end
|
|
|
|
|
2013-07-29 19:54:29 -04:00
|
|
|
end
|