DEV: trigger new discourse events `after_create_dev_record` & `after_populate_dev_records`. (#13733)

After every new random record created using the `dev:populate` rake task a new Discourse event will be triggered. So the plugins can modify the records if needed.
This commit is contained in:
Vinoth Kannan 2021-07-15 11:11:41 +05:30 committed by GitHub
parent 31aa701518
commit 76fe3a16a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 5 deletions

View File

@ -18,18 +18,20 @@ module DiscourseDev
end end
@model = model @model = model
@type = model.to_s @type = model.to_s.downcase.to_sym
@count = count @count = count
end end
def create! def create!
record = model.create!(data) record = model.create!(data)
yield(record) if block_given? yield(record) if block_given?
DiscourseEvent.trigger(:after_create_dev_record, record, type)
record
end end
def populate! def populate!
if current_count >= @count if current_count >= @count
puts "Already have #{current_count} #{type.downcase} records" puts "Already have #{current_count} #{type} records"
Rake.application.top_level_tasks.each do |task_name| Rake.application.top_level_tasks.each do |task_name|
Rake::Task[task_name].reenable Rake::Task[task_name].reenable
@ -39,17 +41,20 @@ module DiscourseDev
return return
elsif current_count > 0 elsif current_count > 0
@count -= current_count @count -= current_count
puts "There are #{current_count} #{type.downcase} records. Creating #{@count} more." puts "There are #{current_count} #{type} records. Creating #{@count} more."
else else
puts "Creating #{@count} sample #{type.downcase} records" puts "Creating #{@count} sample #{type} records"
end end
records = []
@count.times do @count.times do
create! records << create!
putc "." putc "."
end end
DiscourseEvent.trigger(:after_populate_dev_records, records, type)
puts puts
records
end end
def current_count def current_count