Class: Shrine::Plugins::KithePromotionHooks

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

Overview

This adds some features around shrine promotion that we found useful for dealing with backgrounding promotion.

  • It will run shrine uploader metadata extraction routines on _any promotion_, also adding a ‘promoting: true` key to the shrine context for that metadata extraction. (Using shrine refresh_metadata plugin)

  • We separately give our Kithe::Asset model class an activemodel callback “promotion” hook. This plugin will call those callbacks around promotion (whether background or foreground) – before, after, or around.

    If a callback does a ‘throw :abort` before promotion, it can cancel promotion. This could be used to cancel promotion for a validation failure of some kind – you’d want to somehow store or notify what happened, otherwise to the app and it’s users it will just look like the thing was never promoted for unknown reasons.

    After promotion hooks can be used to hook into things you want to do only after a promotion; since promotion is backgrounded it would be otherwise inconvenient to execute something only after promotion completes.

    The default Kithe::Asset hooks into after_promotion to run derivatives creation routines.

  • A special :promotion_directives key in the shrine context, which will be serialized and restored to be preserved even accross background promotion. It is intended to hold a hash of arbitrary key/values. The special key :skip_callbacks, when set to a truthy value, will prevent the promotion callbacks discussed above from happening. So if you want to save a Kithe::Asset and have promotion happen as usual, but not trigger any callbacks (including derivative creation):

    some_asset.file = some_assignable_file some_asset.file_attacher.set_promotion_directives(skip_callbacks: true) some_asset.save!

    You can add other arbitrary keys which your own code in an uploader or promotion callback may consult, with ‘set_promotion_directives` as above. To consult, check attacher.promotion_directives

    You can also set promotion directives globally for Kithe::Asset or a sub-class, in a class method. Especially useful for batch processing.

    Kithe::Asset.promotion_directives = { promote: :inline, create_derivatives: :inline }
    

Defined Under Namespace

Modules: AttacherClassMethods, AttacherMethods

Class Method Summary collapse

Class Method Details

.load_dependencies(uploader) ⇒ Object



53
54
55
56
# File 'lib/shrine/plugins/kithe_promotion_hooks.rb', line 53

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