Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to monkey patch in Ruby on Rails

I want to add a method to ActiveStorage::Blob. I have this:

config/initializers/active_storage_cached_urls.rb

module ActiveStorageBlobCachedUrl
  def direct_url
    xxx || self.service_url
  end
end

end of config/environment.rb

ActiveStorage::Blob.include ActiveStorageBlobCachedUrl

I launched rails s in development, and it works. Then I updated one ruby file, such as a model, and I got an error: undefined method 'direct_url....

I guess something is reloaded without my monkey patch. What am I missing in order to have my monkey patch also reloaded?

like image 613
Nicolas Maloeuvre Avatar asked Dec 20 '25 21:12

Nicolas Maloeuvre


1 Answers

put this in a file in initializers/

module ActiveStorageBlobCachedUrl
  def direct_url
    # xx
  end
end


ActiveSupport::Reloader.to_prepare do
  ActiveStorage::Blob.include ActiveStorageBlobCachedUrl
end
like image 62
Nicolas Maloeuvre Avatar answered Dec 23 '25 11:12

Nicolas Maloeuvre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!