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, populate!, random
Constructor Details
#initialize(topic, 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)
super(::Post, count)
@topic = topic
category = topic.category
@max_likes_count = DiscourseDev.config.post[: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
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
102
103
104
|
# File 'lib/discourse_dev/post.rb', line 71
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|
begin
user = User.random
reply = Faker::DiscourseMarkdown.with_user(user.id) do
{
topic_id: topic.id,
raw: Faker::DiscourseMarkdown.sandwich(sentences: 5),
skip_validations: true
}
end
PostCreator.new(user, reply).create!
rescue ActiveRecord::RecordNotSaved => e
puts e
end
end
puts "Done!"
end
|
Instance Method Details
#create! ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/discourse_dev/post.rb', line 35
def create!
user = self.user
data = Faker::DiscourseMarkdown.with_user(user.id) { self.data }
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::DiscourseMarkdown.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
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/discourse_dev/post.rb', line 43
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
63
64
65
66
67
68
69
|
# File 'lib/discourse_dev/post.rb', line 63
def populate!
generate_likes(topic.first_post)
@count.times do
create!
end
end
|
#user ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/discourse_dev/post.rb', line 55
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
|