mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 15:25:35 +08:00
DEV: only use the records that are auto populated by the task. (#14360)
Previously, it was using existing user and topic records to generate random posts.
This commit is contained in:
parent
903439a14e
commit
2c7cc40be3
|
@ -7,6 +7,7 @@ require 'faker'
|
|||
module DiscourseDev
|
||||
class Record
|
||||
DEFAULT_COUNT = 30.freeze
|
||||
AUTO_POPULATED = "auto_populated"
|
||||
|
||||
attr_reader :model, :type
|
||||
|
||||
|
@ -24,6 +25,7 @@ module DiscourseDev
|
|||
|
||||
def create!
|
||||
record = model.create!(data)
|
||||
record.custom_fields[AUTO_POPULATED] = true if record.respond_to?(:custom_fields)
|
||||
yield(record) if block_given?
|
||||
DiscourseEvent.trigger(:after_create_dev_record, record, type)
|
||||
record
|
||||
|
@ -71,8 +73,12 @@ module DiscourseDev
|
|||
self.new.populate!
|
||||
end
|
||||
|
||||
def self.random(model)
|
||||
offset = Faker::Number.between(from: 0, to: model.count - 1)
|
||||
def self.random(model, use_existing_records: true)
|
||||
model.joins(:_custom_fields).where("#{:type}_custom_fields.name = '#{AUTO_POPULATED}'") if !use_existing_records && model.new.respond_to?(:custom_fields)
|
||||
count = model.count
|
||||
raise "#{:type} records are not yet populated" if count == 0
|
||||
|
||||
offset = Faker::Number.between(from: 0, to: count - 1)
|
||||
model.offset(offset).first
|
||||
end
|
||||
end
|
||||
|
|
|
@ -44,7 +44,7 @@ module DiscourseDev
|
|||
end
|
||||
|
||||
def self.random
|
||||
super(::User)
|
||||
super(::User, use_existing_records: false)
|
||||
end
|
||||
|
||||
def set_random_avatar(user)
|
||||
|
|
Loading…
Reference in New Issue
Block a user