Class: Ratonvirus::Storage::ActiveStorage

Inherits:
Base
  • Object
show all
Defined in:
lib/ratonvirus/storage/active_storage.rb

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Ratonvirus::Storage::Base

Instance Method Details

#accept?(resource) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/ratonvirus/storage/active_storage.rb', line 23

def accept?(resource)
  resource.is_a?(::ActiveStorage::Attached::One) ||
    resource.is_a?(::ActiveStorage::Attached::Many)
end

#asset_path(asset, &block) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/ratonvirus/storage/active_storage.rb', line 43

def asset_path(asset, &block)
  return unless block_given?
  return if asset.nil?
  return unless asset.blob

  blob_path asset.blob, &block
end

#asset_remove(asset) ⇒ Object



51
52
53
# File 'lib/ratonvirus/storage/active_storage.rb', line 51

def asset_remove(asset)
  asset.purge
end

#changed?(_record, _attribute) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ratonvirus/storage/active_storage.rb', line 6

def changed?(_record, _attribute)
  # With Active Storage we assume the record is always changed because
  # there is currently no way to know if the attribute has actually
  # changed.
  #
  # Calling record.changed? will not also work because it is not marked
  # as dirty in case the Active Storage attachment has changed.
  #
  # NOTE:
  # This should be changed in the future as the `attachment_changes` was
  # introduced to Rails by this commit:
  # https://github.com/rails/rails/commit/e8682c5bf051517b0b265e446aa1a7eccfd47bf7
  #
  # However, it is still not available in Rails 5.2.x.
  true
end

#process(resource, &_block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ratonvirus/storage/active_storage.rb', line 28

def process(resource, &_block)
  return unless block_given?
  return if resource.nil?

  return unless resource.attached?

  if resource.is_a?(::ActiveStorage::Attached::One)
    yield processable(resource.attachment) if resource.attachment
  elsif resource.is_a?(::ActiveStorage::Attached::Many)
    resource.attachments.each do |attachment|
      yield processable(attachment)
    end
  end
end