2021-06-14 23:04:44 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "discourse_dev"
|
|
|
|
require "rails"
|
|
|
|
require "faker"
|
|
|
|
|
|
|
|
module DiscourseDev
|
|
|
|
class Record
|
|
|
|
DEFAULT_COUNT = 30.freeze
|
2021-09-17 12:17:32 +08:00
|
|
|
AUTO_POPULATED = "auto_populated"
|
2021-06-14 23:04:44 +08:00
|
|
|
|
|
|
|
attr_reader :model, :type
|
|
|
|
|
|
|
|
def initialize(model, count = DEFAULT_COUNT)
|
|
|
|
@@initialized ||=
|
|
|
|
begin
|
|
|
|
Faker::Discourse.unique.clear
|
|
|
|
RateLimiter.disable
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
@model = model
|
2021-07-15 13:41:41 +08:00
|
|
|
@type = model.to_s.downcase.to_sym
|
2021-06-14 23:04:44 +08:00
|
|
|
@count = count
|
|
|
|
end
|
|
|
|
|
|
|
|
def create!
|
|
|
|
record = model.create!(data)
|
2021-09-17 12:17:32 +08:00
|
|
|
record.custom_fields[AUTO_POPULATED] = true if record.respond_to?(:custom_fields)
|
2021-06-14 23:04:44 +08:00
|
|
|
yield(record) if block_given?
|
2021-07-15 13:41:41 +08:00
|
|
|
DiscourseEvent.trigger(:after_create_dev_record, record, type)
|
|
|
|
record
|
2021-06-14 23:04:44 +08:00
|
|
|
end
|
|
|
|
|
2021-07-15 20:15:32 +08:00
|
|
|
def populate!(ignore_current_count: false)
|
2021-07-20 14:55:59 +08:00
|
|
|
unless Discourse.allow_dev_populate?
|
|
|
|
raise 'To run this rake task in a production site, set the value of `ALLOW_DEV_POPULATE` environment variable to "1"'
|
|
|
|
end
|
|
|
|
|
2023-04-20 08:53:10 +08:00
|
|
|
unless ignore_current_count || @ignore_current_count
|
2021-07-15 20:15:32 +08:00
|
|
|
if current_count >= @count
|
|
|
|
puts "Already have #{current_count} #{type} records"
|
2021-06-14 23:04:44 +08:00
|
|
|
|
2021-07-15 20:15:32 +08:00
|
|
|
Rake.application.top_level_tasks.each { |task_name| Rake::Task[task_name].reenable }
|
2021-06-14 23:04:44 +08:00
|
|
|
|
2021-07-15 20:15:32 +08:00
|
|
|
Rake::Task["dev:repopulate"].invoke
|
|
|
|
return
|
|
|
|
elsif current_count > 0
|
|
|
|
@count -= current_count
|
|
|
|
puts "There are #{current_count} #{type} records. Creating #{@count} more."
|
|
|
|
else
|
|
|
|
puts "Creating #{@count} sample #{type} records"
|
|
|
|
end
|
2021-06-14 23:04:44 +08:00
|
|
|
end
|
|
|
|
|
2021-07-15 13:41:41 +08:00
|
|
|
records = []
|
2021-06-14 23:04:44 +08:00
|
|
|
@count.times do
|
2021-07-15 13:41:41 +08:00
|
|
|
records << create!
|
2021-07-15 20:15:32 +08:00
|
|
|
putc "." unless type == :post
|
2021-06-14 23:04:44 +08:00
|
|
|
end
|
|
|
|
|
2021-07-15 20:15:32 +08:00
|
|
|
puts unless type == :post
|
2021-07-15 13:41:41 +08:00
|
|
|
DiscourseEvent.trigger(:after_populate_dev_records, records, type)
|
|
|
|
records
|
2021-06-14 23:04:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_count
|
|
|
|
model.count
|
|
|
|
end
|
|
|
|
|
2023-04-20 08:53:10 +08:00
|
|
|
def self.populate!(**args)
|
|
|
|
self.new(**args).populate!
|
2021-06-14 23:04:44 +08:00
|
|
|
end
|
|
|
|
|
2021-09-17 12:17:32 +08:00
|
|
|
def self.random(model, use_existing_records: true)
|
|
|
|
if !use_existing_records && model.new.respond_to?(:custom_fields)
|
|
|
|
model.joins(:_custom_fields).where("#{:type}_custom_fields.name = '#{AUTO_POPULATED}'")
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2021-09-17 12:17:32 +08:00
|
|
|
count = model.count
|
|
|
|
raise "#{:type} records are not yet populated" if count == 0
|
|
|
|
|
|
|
|
offset = Faker::Number.between(from: 0, to: count - 1)
|
2021-06-14 23:04:44 +08:00
|
|
|
model.offset(offset).first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|