Class: DiscourseDev::Post
Constant Summary
Constants inherited
from Record
Record::AUTO_POPULATED, 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
populate!
Constructor Details
#initialize(topic, count) ⇒ Post
Returns a new instance of Post.
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/discourse_dev/post.rb', line 10
def initialize(topic, count)
super(::Post, count)
@topic = topic
category = topic.category
@max_likes_count = DiscourseDev.config.post[:max_likes_count]
if category&.groups.present?
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.
8
9
10
|
# File 'lib/discourse_dev/post.rb', line 8
def topic
@topic
end
|
Class Method Details
.add_replies!(args) ⇒ Object
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
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/discourse_dev/post.rb', line 80
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
|
.random ⇒ Object
116
117
118
|
# File 'lib/discourse_dev/post.rb', line 116
def self.random
super(::Post)
end
|
Instance Method Details
#current_count ⇒ Object
76
77
78
|
# File 'lib/discourse_dev/post.rb', line 76
def current_count
topic.posts_count - 1
end
|
#data ⇒ Object
24
25
26
27
28
29
30
31
32
|
# File 'lib/discourse_dev/post.rb', line 24
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
54
55
56
57
58
59
60
|
# 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
70
71
72
73
74
|
# File 'lib/discourse_dev/post.rb', line 70
def populate!
generate_likes(topic.first_post)
super(ignore_current_count: true)
end
|
#user ⇒ Object
62
63
64
65
66
67
68
|
# File 'lib/discourse_dev/post.rb', line 62
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
|