-
Notifications
You must be signed in to change notification settings - Fork 51
/
similar_topics.rb
48 lines (36 loc) · 1.11 KB
/
similar_topics.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
42
43
44
45
46
47
48
# frozen_string_literal: true
class CustomWizard::RealtimeValidation::SimilarTopics
attr_accessor :user
def initialize(user)
@user = user
end
class SimilarTopic
def initialize(topic)
@topic = topic
end
attr_reader :topic
def blurb
Search::GroupedSearchResults.blurb_for(cooked: @topic.try(:blurb))
end
end
def perform(params)
title = params[:title]
raw = params[:raw]
categories = params[:categories]
time_n_value = params[:time_n_value]
time_unit = params[:time_unit]
result = CustomWizard::RealtimeValidation::Result.new(:similar_topic)
if title.length < SiteSetting.min_title_similar_length
return result
end
topics = Topic.similar_to(title, raw, user).to_a
topics.select! { |t| categories.include?(t.category.id.to_s) } if categories.present?
if time_n_value.to_i > 0 && time_unit.present?
topics.select! { |t| t.created_at >= time_n_value.to_i.send(time_unit).ago }
end
topics.map! { |t| SimilarTopic.new(t) }
result.items = topics
result.serializer_opts = { root: :similar_topics }
result
end
end