Class: Shrine::Plugins::KitheControllableBackgrounding

Inherits:
Object
  • Object
show all
Defined in:
lib/shrine/plugins/kithe_controllable_backgrounding.rb

Overview

Set up shrine ‘backgrounding`, where promotion and deletion can happen in a background job.

shrinerb.com/docs/getting-started#backgrounding shrinerb.com/docs/plugins/backgrounding

By default, kithe does promotion and deletion in kithe-provided ActiveJob classes.

But this plugin implements code to let you use kithe_promotion_directives to make them happen inline instead, or disable them.

asset.file_attacher.set_promotion_directives(promote: false)
asset.file_attacher.set_promotion_directives(promote: :inline)
asset.file_attacher.set_promotion_directives(promote: "inline")

asset.file_attacher.set_promotion_directives(delete: :inline)

Class Method Summary collapse

Class Method Details

.configure(uploader, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/shrine/plugins/kithe_controllable_backgrounding.rb', line 25

def self.configure(uploader, options = {})

  # promote using shrine backgrounding, but can be effected by promotion_directives[:promote]
  uploader::Attacher.promote_block do
    Kithe::TimingPromotionDirective.new(key: :promote, directives: self.promotion_directives) do |directive|
      if directive.inline?
        promote
      elsif directive.background?
        # What shrine normally expects for backgrounding, plus promotion_directives
        Kithe::AssetPromoteJob.perform_later(self.class.name, record.class.name, record.id, name.to_s, file_data, self.promotion_directives)
      end
    end
  end

  uploader::Attacher.destroy_block do
    Kithe::TimingPromotionDirective.new(key: :delete, directives: self.promotion_directives) do |directive|
      if directive.inline?
        destroy
      elsif directive.background?
        # What shrine normally expects for backgrounding
        Kithe::AssetDeleteJob.perform_later(self.class.name, data)
      end
    end
  end
end

.load_dependencies(uploader) ⇒ Object



20
21
22
23
# File 'lib/shrine/plugins/kithe_controllable_backgrounding.rb', line 20

def self.load_dependencies(uploader, *)
  uploader.plugin :backgrounding
  uploader.plugin :kithe_promotion_directives
end