Skip to content

Instantly share code, notes, and snippets.

@Narnach
Created May 11, 2022 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Narnach/8090d704f33e666071e6da0909d6f8b1 to your computer and use it in GitHub Desktop.
Save Narnach/8090d704f33e666071e6da0909d6f8b1 to your computer and use it in GitHub Desktop.
# If you have one or two monkey patches, you can easily stick them in an initializer, like this:
# ./config/initializers/monkey_patch_*.rb
# Or if you have some sort of load order requirement, prepend numbers to put them before other initializers:
# ./config/initializers/000_monkey_patch_*.rb
# If you have a lot, you might want to put them in ./app/monkey_patches or some other clearly marked location.
# Require the dependency to ensure it's loaded/available
require 'path/to/class/we/patch' # TODO
# TODO: Describe the problem this monkey patch solves. If possible refer to issue threads, PRs and commits with more details.
# Code in here is executed before every request in development mode, but only once in production mode.
# Here it makes sure the contained monkey patch is always applied, even if the patched gem's code is reloaded.
# `to_prepare` is available from Rails 5, and is intended to play nice with Zeitwerk (Rails 6.0)
Rails.configuration.to_prepare do
checked_version = '1.2.3' # TODO: put the current version here
current_version = TheGemWePatchHere::VERSION # TODO: point it at the gem's version method/constant
if Gem::Version.new(current_version) > Gem::Version.new(checked_version)
raise "Please review this monkey patch still applies to version #{current_version} (last checked for #{checked_version})"
end
# This opens up an existing class, which will explode if the require above is not working as intended.
# This is a feature, because it helps you spot classes which no longer exist.
Namespace::To::ClassWithIssues.class_eval do
# TODO: Describe intention of the patch and where we deviate from the original code
# Source: TODO (link to file in source repository at tag we last checked it)
def some_method(arg1, arg2)
# TODO: upstream code goes in, then mark your changes so the next person looking at this
# can evaluate what you did and if it's still relevant for the next version.
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment