mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 20:36:39 +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
|
module DiscourseDev
|
||||||
class Record
|
class Record
|
||||||
DEFAULT_COUNT = 30.freeze
|
DEFAULT_COUNT = 30.freeze
|
||||||
|
AUTO_POPULATED = "auto_populated"
|
||||||
|
|
||||||
attr_reader :model, :type
|
attr_reader :model, :type
|
||||||
|
|
||||||
|
@ -24,6 +25,7 @@ module DiscourseDev
|
||||||
|
|
||||||
def create!
|
def create!
|
||||||
record = model.create!(data)
|
record = model.create!(data)
|
||||||
|
record.custom_fields[AUTO_POPULATED] = true if record.respond_to?(:custom_fields)
|
||||||
yield(record) if block_given?
|
yield(record) if block_given?
|
||||||
DiscourseEvent.trigger(:after_create_dev_record, record, type)
|
DiscourseEvent.trigger(:after_create_dev_record, record, type)
|
||||||
record
|
record
|
||||||
|
@ -71,8 +73,12 @@ module DiscourseDev
|
||||||
self.new.populate!
|
self.new.populate!
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.random(model)
|
def self.random(model, use_existing_records: true)
|
||||||
offset = Faker::Number.between(from: 0, to: model.count - 1)
|
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
|
model.offset(offset).first
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -44,7 +44,7 @@ module DiscourseDev
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.random
|
def self.random
|
||||||
super(::User)
|
super(::User, use_existing_records: false)
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_random_avatar(user)
|
def set_random_avatar(user)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user