Module: Alchemy::StorageAdapter::ActiveStorage::PictureClassMethods

Defined in:
app/models/alchemy/storage_adapter/active_storage.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 5

def self.included(base)
  base.has_one_attached :image_file do |attachable|
    Alchemy.storage_adapter.preprocessor_class.new(attachable).call
    Alchemy.storage_adapter.preprocessor_class.generate_thumbs!(attachable)
  end

  base.after_create_commit if: :svg? do
    SanitizeSvgJob.perform_later(self, file_accessor: :image_file)
  end

  # The image file's dirty state is cleared by the time
  # +after_update_commit+ runs, so capture it here to know whether the
  # blob was actually replaced (rather than only its metadata updated).
  base.before_update do
    @image_file_replaced = image_file.changed?
  end

  base.after_update_commit if: -> { @image_file_replaced && svg? } do
    SanitizeSvgJob.perform_later(self, file_accessor: :image_file)
  end
end