Class: GeneratedImageImporter

Inherits:
Object
  • Object
show all
Defined in:
app/services/generated_image_importer.rb

Overview

Promotes a pending +GeneratedImage+ into a permanent +Image+ library record
and executes any source-record side effects (e.g. back-filling avatar IDs).

Extracted from +GeneratedImagesController+ so background workers can perform
the same operation without going through a controller action.

Usage:
result = GeneratedImageImporter.call(generated_image, title: "...", tags: [...])
result.success? # => true
result.image # => Image record
result.notice # => flash-ready string

Defined Under Namespace

Classes: Result

Constant Summary collapse

MAX_SLUG_ATTEMPTS =

Upper bound for slug retries when concurrent imports race on (type, slug).

1000

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generated_image, title:, tags:, category: nil, notes: nil) ⇒ GeneratedImageImporter

Returns a new instance of GeneratedImageImporter.



33
34
35
36
37
38
39
# File 'app/services/generated_image_importer.rb', line 33

def initialize(generated_image, title:, tags:, category: nil, notes: nil)
  @gen      = generated_image
  @title    = title
  @tags     = tags
  @category = category
  @notes    = notes
end

Class Method Details

.call(generated_image, title:, tags:, category: nil, notes: nil) ⇒ Result

Parameters:

  • generated_image (GeneratedImage)
  • title (String)
  • tags (Array<String>)
  • category (String, nil) (defaults to: nil)
  • notes (String, nil) (defaults to: nil)

Returns:



29
30
31
# File 'app/services/generated_image_importer.rb', line 29

def self.call(generated_image, title:, tags:, category: nil, notes: nil)
  new(generated_image, title: title, tags: tags, category: category, notes: notes).call
end

Instance Method Details

#callObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/services/generated_image_importer.rb', line 41

def call
  ApplicationRecord.transaction do
    image = promote_to_image_without_own_transaction
    raise StandardError, 'Failed to import image.' if image.blank?

    @gen.update!(status: 'imported')
    notice = source_record_callback(image)

    Result.new(image: image, notice: notice)
  end
rescue StandardError => e
  Rails.logger.error "[GeneratedImageImporter] #{e.class}: #{e.message}"
  ErrorReporting.error(e) if defined?(ErrorReporting)
  Result.new(error: e.message)
end