Monkey patching ActiveStorage::Attachment gets lost

active storage
activestorage::invariableerror
active storage public url
active storage permanent url
active storage get url
active storage direct upload
active storage attachment url
active_storage_blobs

So I decided to add an url attr_accessor to ActiveStorage::Attachment objects.

In development the patch holds for a while until it seems to "have been lost". Meaning it works for few minutes, then it does not work anymore. Then I need to restart the server in order to get the patch applied again. I believe I am not patching correctly and I would need advises in that mater.


Here is what I tried:

lib/ext/active_storage/attachment.rb

First try :

module ActiveStorageUrl
  extend ActiveSupport::Concern

  included do
    attr_accessor :url
  end
end

ActiveStorage::Attachment.send :include, ActiveStorageUrl

Second try

class ActiveStorage::Attachment < ActiveRecord::Base
  attr_accessor :url
end

And by the way in both case it's loaded with this:

config/initializers/monkey_patches.rb

require 'ext/active_storage/attachment'

So when it work I have no error message, but after a while the patch "diseapear" (lacking better terms), and I get the following error, telling me my attr_accessor is not there anymore. Rails must have reloaded ActiveStorage classes and my patch is lost.

Module::DelegationError in Products#images
url delegated to blob, but blob is nil

You are probably losing your monkey patch because the code gets reloaded and your ext/active_storage/attachment isn't re-required.

You can tell Rails to run a callback at startup and every time code is reloaded like this.

Rails.configuration.to_prepare do
  require 'ext/active_storage/attachment'
end

Permanent or direct links to ActiveStorage uploads � Issue #30431 , Because a unique URL is generated for every request it's not easy to cache, nor is it possi link_to document.attachment.blob.filename, url_for(document. attachment) I monkey batch ActiveStorage::RepresentationsController#show to Hi @dwightwatson , where do you put this monkey patch? 4 Monkey patching ActiveStorage::Attachment gets lost Mar 17 '19. 3 Mute ActiveStorage Logs Mar 17 '19. 3 Bootstrap popover is blinking on hover Jun 18 '17.

I placed my ActiveStorage::AttachmentMonkeyPatch in /app/models/active_storage/ I've added a callback to get informed if the Attachment has changed. It works fine, all the time.

Maybe this is the issue.

Transaction problem of active storage � Issue #32449 � rails/rails , GitHub is home to over 50 million developers working together to do ActiveRecord::Base.transaction do @user.avatar.attach Missing service configuration file in test/service/configurations.yml Note: I've been getting around this by monkey-patching ActiveStorage to not do a purge_later in the attach� Rifinements or monkey patching rails 4. 1. Safe and best way to monkey patch a rails gem. 9. Monkey patching ActiveStorage::Attachment gets lost. Hot Network Questions

Seems to have to do with delegate_missing_to, e.g.

delegate_missing_to :blob

https://github.com/rails/rails/blob/master/activestorage/app/models/active_storage/attachment.rb#L14

Going to where it's defined:

Anyway it might have to do with how attr_accessor works, I would try:

def url
  @url
end

def url=(url)
  @url = url
end

Instead of attr_accessor (which is a C function actually).

Otherwise a really really hacky way to solve this would be to check for ActiveStorage::Attachment.instance_methods.include?(:url) and monkey patch / include / prepend when not present.

monkeypatching – TechUtils.in, So I decided to add an url attr_accessor to ActiveStorage::Attachment objects. Rails must have reloaded ActiveStorage classes and my patch is lost. Monkey patching ActiveStorage::Attachment gets lost Hot Network Questions What is the verb for describing the movement of a cat's paw when it is trying to hit something with it?

Some notes on what's going on in ActiveStorage – Bibliographic , The first we'll look at is ActiveStorage::Blob, which actually represents a So what's the ActiveStorage::Attachment model? So while this blog post suggests monkey-patching Analyzer::ImageAnalyzer to add in GPS the sets of changes might get lost as both processes race to 1) load from db, 2) merge� Bounty: 150. I’m using Rails 5. I would like to create a sidekiq process running locally using the default queue. My worker class looks roughly like the below …

Migrating from Paperclip to ActiveStorage: a different approach, Even though (at the time of writing this article) ActiveStorage is not as mature as Since at Sortlist we have hundreds of thousands of attachments, we can't afford update that column is to create a callback on the ActiveStorage::Blob model. and we would have to monkey-patch ActiveStorage (source). Я поражая апи, чтобы получить изображение, которое они сохранили и использовать его в качестве профиля ПОС для пользователей нашего приложения.

Callback for Active Storage file upload, class ActiveStorageAttachment < ActiveRecord::Base after_update :after_update Active Storage is a framework in Ruby that makes it a breeze to upload files and ActiveStorage "Parent" Model Callback #35044 build a monkey patch and to copy over the entire class and then add to it, not sure if I'm missing something?

Comments
  • Seems to be the method I needed to know about. Thanks
  • Ok I defined geter and setter instead of attr_accessor. Will see how it goes. Thanks