È passato un po’ di tempo - 5 anni a quanto pare, ma dalla memoria…
Il codice del job, ad esempio:
# school-points\jobs\deferred_topic_creation.rb
module Jobs
class DeferredTopicCreation < Jobs::Base
def execute(args)
creator_user_id = args[:creator_user_id]
custom_fields = {
event_start: args[:event_start],
event_end: args[:event_end]
}
creator_user = User.find(creator_user_id) || User.find(Discourse::SYSTEM_USER_ID)
post_creator = PostCreator.new(
creator_user,
args.except!(:creator_user_id, :event_start, :event_end)
)
post = post_creator.create
topic = Topic.find(post.topic_id)
topic.custom_fields = custom_fields
topic.save!
end
end
end
dovrebbe essere CARICATO nel blocco after_initialize. Se ciò non viene fatto, il job non può essere creato. Questo non crea un topic, ma semplicemente registra il codice come un job che può essere accodato in seguito.
Quindi, quando si desidera effettivamente creare un topic in differita (per te, dopo la tua richiesta AJAX), dovresti eseguire qualcosa di simile:
opts = {
title: title,
raw: content,
category: child_category[:id],
creator_user_id: creator_user_id,
event_start: event_start.to_time.to_i,
event_end: event_end.to_time.to_i
}
Jobs.enqueue(:deferred_topic_creation, opts)
Spero che questo chiarisca le cose.