Ir para conteúdo principal
Lucas Caton

If you really need to create a monkey patch, do it properly

Lucas Caton

Lucas Caton

@lucascaton
Top-5-Ruby-IDE-Solutions-for-Web-Developers
Sometimes we need to create a monkey patch for a gem or external lib. In these cases, it's good to force it to fail if the gem has been bumped up. Let's use Paperclip as an example:
ruby
if Paperclip::VERSION != '1.2.3'
  # If you see this message, please test removing this file
  # If it's still required, please bump up the version above
  fail 'Please remove me, Paperclip version has changed'
end
If the gem doesn't provide you the version through a method, you can solve it with:
ruby
if Bundler.load.specs.find { |gem| gem.name == 'paperclip' }.version.to_s != '1.2.3'
  # If you see this message, please test removing this file
  # If it's still required, please bump up the version above
  fail 'Please remove me, Paperclip version has changed'
end
Or even when you're waiting for a new Rails version, e.g.:
ruby
fail 'Remove this file' if Rails::VERSION::MAJOR >= 5

Post atualizado em 02/10/2017, 10:00:00