Class: RailsAIPromptable::BackgroundJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
lib/rails_ai_promptable/background_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(klass_name, record_id, context, kwargs) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rails_ai_promptable/background_job.rb', line 14

def perform(klass_name, record_id, context, kwargs)
  klass = klass_name.constantize
  record = klass.find_by(id: record_id)
  return unless record

  result = record.ai_generate(context: context, **(kwargs || {}))

  # Call the callback method if defined on the record
  record.ai_generation_completed(result) if record.respond_to?(:ai_generation_completed)

  # Store result in ai_generated_content attribute if it exists
  if record.respond_to?(:ai_generated_content=)
    record.ai_generated_content = result
    record.save if record.respond_to?(:save)
  end

  RailsAIPromptable.configuration.logger.info("[rails_ai_promptable] background generation completed for #{klass_name}##{record_id}")

  result
end