I want to add a method to ActiveStorage::Variant
How to do it?
I have this for ActiveStorage::Blob and it works when I modify code without reloading the server :
config/initializers/active_storage_direct_url.rb
:
module ActiveStorageDirectUrl
def cloudfront_url(expires_at = nil)
# xx
end
end
ActiveSupport.on_load(:active_storage_blob) do
ActiveStorage::Blob.include ActiveStorageBlobCachedUrl
end
But the issue is when I do it for ActiveStorage::Variant. I tried this :
ActiveSupport.on_load(:active_storage_blob) do
ActiveStorage::Variant.include ActiveStorageVariantCachedUrl
end
ActiveSupport.on_load(:active_storage_variant) do
ActiveStorage::Variant.include ActiveStorageVariantCachedUrl
end
But it in both case, in dev environment, when I modify some code and without restarting the server, it says undefined method 'direct_url' for #<ActiveStorage::Variant:0x00007fc04fa45530>
When I reload the rails server
it works though. Same thing for sidekiq, I need to reload it.
Use :
ActiveSupport::Reloader.to_prepare do
ActiveStorage::Variant.include ActiveStorageVariantCachedUrl
end