Passing additional options/params to background promote


Is there any supported way to pass additional options to background jobs, like for example the current user, but when using persistence plugins like activerecord?

I read is possible when registering background blocks on the attacher instances here

But when using activerecord i wasn’t directly managing the attacher as the promote calls are automatically called on saved.

Eventually i can override the save call, but if there is a better supported way with shrine would be good to know :slight_smile:

Yes, the promote is called automatically, but you can still access the attacher and register a promote hook before that happens that adds the current user, as shown in that section.

Alternatively, you can put the current user in the attacher context, and in your global promote hook include in your job arguments:

Shrine::Attacher.promote_block do
  # fetch current user from attacher context
  current_user = context.fetch(:current_user)

# assign current user to the attacher context in your controller
photo.image_attacher.context[:current_user] = current_user

Thanks! I was thinking more of a global shrine hook so not having to assign for example the current_user on every controller (as in my use case i need to always pass the current_user).
But i can use some activerecord callback for that so no problem at all :slight_smile:
Thanks for the quick answer as always!

Yeah, Shrine doesn’t know your controller context, as it’s designed to be callable from anywhere. I don’t think ActiveRecord callbacks have controller access either :thinking: You could use Rails’ CurrentAttributes feature to assign the current user per-request, and then you could read that in your global Shrine promote hook.