ActiveStorage Callbacks

I’m not sure how common this is, but wondering if its worth working on a PR for ActiveStorage to allow you to use callbacks. Something like this for example to run a method after analyze is complete:

has_one_attached :artwork, after_analyze: :my_method

I had a similar need – in my case I want to insert a cover letter at page 1 of a PDF attachment – and I came up with this:

https://gist.github.com/benwalsh/d611608ea9f4ff6e1ec1efd0d3755b37

In the model, then:

include ActiveStorageCallbacks

perform_attach welcome_pack: { before: :insert_cover_letter }

``

I tried a more sophisticated approach that injected the custom callback into the callback chain but it was hard so I gave up.

Hello. I am also interested in standard callbacks for active storage.

Something like:

has_one_attached :avatar, 
  before_add: :before_avatar_add, 
  after_add: :after_avatar_add, 
  before_remove: :before_avatar_remove, 
  after_remove: :after_avatar_remove

I can work on the PR now – would like feedback whether it makes sense in the grand scheme of active storage. I noticed that the OP has after_analyze: but I’m not sure about that one since it would involve more work.

Update:

I misread the association callbacks documentation. Those callbacks only exist for the has_many variant. Nevermind! Maybe this would make more sense if the callbacks are consistent between has_one and has_many.