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:
Vinoth Kannan 2021-09-17 09:47:32 +05:30 committed by GitHub
parent 903439a14e
commit 2c7cc40be3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 3 deletions

View File

@ -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

View File

@ -44,7 +44,7 @@ module DiscourseDev
end
def self.random
super(::User)
super(::User, use_existing_records: false)
end
def set_random_avatar(user)