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

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

Ruby

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:

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:

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.:

fail 'Remove this file' if Rails::VERSION::MAJOR >= 5

Ps.: Paperclip gem was just an example :-)

Comments are closed.