Class: DiscourseDev::Topic

Inherits:
Record
  • Object
show all
Defined in:
lib/discourse_dev/topic.rb

Constant Summary

Constants inherited from Record

Record::DEFAULT_COUNT

Instance Attribute Summary

Attributes inherited from Record

#model, #type

Instance Method Summary collapse

Methods inherited from Record

#index, populate!, random

Constructor Details

#initializeTopic

Returns a new instance of Topic.



9
10
11
12
# File 'lib/discourse_dev/topic.rb', line 9

def initialize
  @settings = DiscourseDev.config.topic
  super(::Topic, @settings[:count])
end

Instance Method Details

#create!Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/discourse_dev/topic.rb', line 65

def create!
  @category = Category.random
  user = self.user
  topic = Faker::DiscourseMarkdown.with_user(user.id) { data }
  post = PostCreator.new(user, topic).create!

  if override = @settings.dig(:replies, :overrides).find { |o| o[:title] == topic[:title] }
    reply_count = override[:count]
  else
    reply_count = Faker::Number.between(from: @settings.dig(:replies, :min), to: @settings.dig(:replies, :max))
  end

  Post.new(post.topic, reply_count).populate!
end

#current_countObject



95
96
97
98
# File 'lib/discourse_dev/topic.rb', line 95

def current_count
  category_definition_topic_ids = ::Category.pluck(:topic_id)
  ::Topic.where(archetype: :regular).where.not(id: category_definition_topic_ids).count
end

#dataObject



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
# File 'lib/discourse_dev/topic.rb', line 14

def data
  max_views = 0

  case Faker::Number.between(from: 0, to: 5)
  when 0
    max_views = 10
  when 1
    max_views = 100
  when 2
    max_views = SiteSetting.topic_views_heat_low
  when 3
    max_views = SiteSetting.topic_views_heat_medium
  when 4
    max_views = SiteSetting.topic_views_heat_high
  when 5
    max_views = SiteSetting.topic_views_heat_high + SiteSetting.topic_views_heat_medium
  end

  {
    title: title[0, SiteSetting.max_topic_title_length],
    raw: Faker::DiscourseMarkdown.sandwich(sentences: 5),
    category: @category.id,
    created_at: Faker::Time.between(from: DiscourseDev.config.start_date, to: DateTime.now),
    tags: tags,
    topic_opts: {
      import_mode: true,
      views: Faker::Number.between(from: 1, to: max_views),
      custom_fields: { dev_sample: true }
    },
    skip_validations: true
  }
end

#delete_unwanted_sidekiq_jobsObject



100
101
102
103
104
# File 'lib/discourse_dev/topic.rb', line 100

def delete_unwanted_sidekiq_jobs
  Sidekiq::ScheduledSet.new.each do |job|
    job.delete if job.item["class"] == "Jobs::UserEmail"
  end
end

#populate!Object



80
81
82
83
# File 'lib/discourse_dev/topic.rb', line 80

def populate!
  super
  delete_unwanted_sidekiq_jobs
end

#tagsObject



55
56
57
58
59
60
61
62
63
# File 'lib/discourse_dev/topic.rb', line 55

def tags
  @tags = []

  Faker::Number.between(from: @settings.dig(:tags, :min), to: @settings.dig(:tags, :max)).times do
    @tags << Faker::Discourse.tag
  end

  @tags.uniq
end

#titleObject



47
48
49
50
51
52
53
# File 'lib/discourse_dev/topic.rb', line 47

def title
  if index < I18n.t("faker.discourse.topics").count
    Faker::Discourse.unique.topic
  else
    Faker::Lorem.unique.sentence(word_count: 5, supplemental: true, random_words_to_add: 4).chomp(".")
  end
end

#userObject



85
86
87
88
89
90
91
92
93
# File 'lib/discourse_dev/topic.rb', line 85

def user
  return User.random if @category.groups.blank?

  group_ids = @category.groups.pluck(:id)
  user_ids = ::GroupUser.where(group_id: group_ids).pluck(:user_id)
  user_count = user_ids.count
  position = Faker::Number.between(from: 0, to: user_count - 1)
  ::User.find(user_ids[position] || Discourse::SYSTEM_USER_ID)
end