Class: DiscourseDev::Post
Constant Summary
Constants inherited
from Record
Record::DEFAULT_COUNT
Instance Attribute Summary collapse
Attributes inherited from Record
#model, #type
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Record
#current_count, #index, populate!, random
Constructor Details
#initialize(topic, count = DEFAULT_COUNT) ⇒ Post
Returns a new instance of Post.
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/discourse_dev/post.rb', line 11
def initialize(topic, count = DEFAULT_COUNT)
super(::Post, count)
@topic = topic
category = topic.category
@max_likes_count = DiscourseDev.config.max_likes_count
unless 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
@max_likes_count = @user_count - 1
end
end
|
Instance Attribute Details
#topic ⇒ Object
Returns the value of attribute topic.
9
10
11
|
# File 'lib/discourse_dev/post.rb', line 9
def topic
@topic
end
|
Class Method Details
.add_replies!(args) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/discourse_dev/post.rb', line 70
def self.add_replies!(args)
if !args[:topic_id]
puts "Topic ID is required. Aborting."
return
end
if !::Topic.find_by_id(args[:topic_id])
puts "Topic ID does not match topic in DB, aborting."
return
end
topic = ::Topic.find_by_id(args[:topic_id])
count = args[:count] ? args[:count].to_i : 50
puts "Creating #{count} replies in '#{topic.title}'"
count.times do |i|
@index = i
begin
reply = {
topic_id: topic.id,
raw: Faker::Markdown.sandwich(sentences: 5),
skip_validations: true
}
PostCreator.new(User.random, reply).create!
rescue ActiveRecord::RecordNotSaved => e
puts e
end
end
puts "Done!"
end
|
Instance Method Details
#create! ⇒ Object
35
36
37
38
39
|
# File 'lib/discourse_dev/post.rb', line 35
def create!
post = PostCreator.new(user, data).create!
topic.reload
generate_likes(post)
end
|
#data ⇒ Object
25
26
27
28
29
30
31
32
33
|
# File 'lib/discourse_dev/post.rb', line 25
def data
{
topic_id: topic.id,
raw: Faker::Markdown.sandwich(sentences: 5),
created_at: Faker::Time.between(from: topic.last_posted_at, to: DateTime.now),
skip_validations: true,
skip_guardian: true
}
end
|
#generate_likes(post) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/discourse_dev/post.rb', line 41
def generate_likes(post)
user_ids = [post.user_id]
Faker::Number.between(from: 0, to: @max_likes_count).times do
user = self.user
next if user_ids.include?(user.id)
PostActionCreator.new(user, post, PostActionType.types[:like], created_at: Faker::Time.between(from: post.created_at, to: DateTime.now)).perform
user_ids << user.id
end
end
|
#populate! ⇒ Object
61
62
63
64
65
66
67
68
|
# File 'lib/discourse_dev/post.rb', line 61
def populate!
generate_likes(topic.first_post)
@count.times do |i|
@index = i
create!
end
end
|
#user ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/discourse_dev/post.rb', line 53
def user
return User.random if topic.category.groups.blank?
return Discourse.system_user if @user_ids.blank?
position = Faker::Number.between(from: 0, to: @user_count - 1)
::User.find(@user_ids[position])
end
|