Module: Shrine::Plugins::Backgrounding::AttacherMethods

Defined in:
lib/shrine/plugins/backgrounding.rb

Instance Method Summary collapse

Instance Method Details

#destroy_attachedObject

Does a background destroy if destroy block was registered.



86
87
88
89
90
91
92
# File 'lib/shrine/plugins/backgrounding.rb', line 86

def destroy_attached
  if destroy? && destroy_block
    destroy_background
  else
    super
  end
end

#destroy_background(**options) ⇒ Object

Calls the registered destroy block.



95
96
97
98
99
# File 'lib/shrine/plugins/backgrounding.rb', line 95

def destroy_background(**options)
  fail Error, "destroy block is not registered" unless destroy_block

  background_block(destroy_block, **options)
end

#destroy_block(&block) ⇒ Object

Registers an instance-level deletion hook.

attacher.destroy_block do |attacher|
  Attachment::DestroyJob.perform_async(attacher.data)
end


64
65
66
67
# File 'lib/shrine/plugins/backgrounding.rb', line 64

def destroy_block(&block)
  @destroy_block = block if block
  @destroy_block
end

#initialize(*args) ⇒ Object

Inherits global hooks if defined.



39
40
41
42
43
# File 'lib/shrine/plugins/backgrounding.rb', line 39

def initialize(*args)
  super
  @destroy_block = self.class.destroy_block
  @promote_block = self.class.promote_block
end

#promote_background(**options) ⇒ Object

Calls the registered promote block.



79
80
81
82
83
# File 'lib/shrine/plugins/backgrounding.rb', line 79

def promote_background(**options)
  fail Error, "promote block is not registered" unless promote_block

  background_block(promote_block, **options)
end

#promote_block(&block) ⇒ Object

Registers an instance-level promotion hook.

attacher.promote_block do |attacher|
  Attachment::PromoteJob.perform_async(
    attacher.record,
    attacher.name
    attacher.file_data,
  )
end


54
55
56
57
# File 'lib/shrine/plugins/backgrounding.rb', line 54

def promote_block(&block)
  @promote_block = block if block
  @promote_block
end

#promote_cached(**options) ⇒ Object

Does a background promote if promote block was registered.



70
71
72
73
74
75
76
# File 'lib/shrine/plugins/backgrounding.rb', line 70

def promote_cached(**options)
  if promote? && promote_block
    promote_background
  else
    super
  end
end