Using ActiveModel::Validation context with Shrine validations

ActiveModel lets us provide validation contexts using on, as per

I’m in the process of switching from Paperclip to Shrine, and one of my uploaders used to rely on that behavior – we needed specific validations only in one context. We used to pass save(context: :foo), but now we can’t, as Shrine validates the files as a separate process

What are the possible workarounds for this issue? I don’t feel like adding any fields to my model, to be honest

If you’re fine with Shrine running the file validations, and you just don’t want to copy the errors into the model, you can disable the automatic validate hook and provide your own:

Shrine.plugin :activerecord, validations: false
class Photo < ActiveRecord::Base
  include ImageUploader::Attachment(:image)

  validate(on: :foo) { image_attacher.send(:activerecord_validate) }

If you need Shrine to skip running file validations altogether, you can do it per attachment assignment, by using Attacher#assign and passing validate: false (assuming you’re on Shrine 3):

# instead of photo.image = file
photo.image_attacher.assign(file, validate: false)

Oh, that’s an interesting way to work around it. Thank you!

In my case, I’d like to skip validations unless the context is present, and I want to copy the errors – but I can work around from that.

I think I can take it from there – I’ll just look at the sources for ActiveRecord and see what I can do

Thank you!