Shrine 3.x: getting the "context" in the derivatives plugin


In Shrine 2.x, using the “context” variable with the versions plugin, I can analyze my model and perform a crop if required:

process(:store) do |io, context| do |original|      
  # load original
  pipeline = ImageProcessing::MiniMagick.source(original) 

  # cropping
  if context[:record].cropping?    
    pipeline = pipeline.crop("#{context[:record].crop_w}x#{context[:record].crop_h}+#{context[:record].crop_x}+#{context[:record].crop_y}") 

How to handle this in Shrine 3, as this variable is not present in the derivatives plugin ?

Attacher.derivatives_processor do |original|

Is-it possible to get the “context” using the “original” variable ?

Sorry, it seems that context[:record] is always accessible from the derivatives processor even if the variable is not present in the Attacher.derivatives_processor. So it should work the same way.

Yes, the Attacher.derivatives_processor block is evaluated in context of the Attacher instance, so it has access to Attacher#context. The record is also accessible through Attacher#record, so you can use that directly to make the code shorter:

Attacher.derivatives do |original|
  magick = ImageProcessing::MiniMagick.source(original)

  if record.cropping?
    magick = magick.crop("#{record.crop_w}x#{record.crop_h}+#{record.crop_x}+#{record.crop_y}")

  # ...

Yes, it’s really easier to work with “record”. Thanks janko.