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!, populate!, random

Constructor Details

#initialize(count = DEFAULT_COUNT) ⇒ Topic

Returns a new instance of Topic.



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

def initialize(count = DEFAULT_COUNT)
  super(::Topic, count)
end

Instance Method Details

#create!Object



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

def create!
  @category = Category.random
  topic = data
  post = PostCreator.new(user, topic).create!

  if topic[:title] == "Coolest thing you have seen today"
    reply_count = 99
  else
    reply_count = Faker::Number.between(from: 0, to: 12)
  end

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

#current_countObject



88
89
90
91
# File 'lib/discourse_dev/topic.rb', line 88

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



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

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::Markdown.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

#tagsObject



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

def tags
  @tags = []

  Faker::Number.between(from: 0, to: 3).times do
    @tags << Faker::Discourse.tag
  end

  @tags.uniq
end

#titleObject



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

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

#userObject



78
79
80
81
82
83
84
85
86
# File 'lib/discourse_dev/topic.rb', line 78

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