- Jul 2023
-
activerecord-hackery.github.io activerecord-hackery.github.io
-
If Arel does not have the predicate you are looking for, consider monkey patching it:
-
- Jun 2023
-
www.soulcutter.com www.soulcutter.com
-
I’ve heard-suggested that ActiveSupport, which does a ton of monkey-patching of core classes, would make potentially-nice refinements. I don’t hold this opinion strongly, but I disagree with that idea. A big value proposition of ActiveSupport is that it is “omnipresent” and sets a new baseline for ruby behaviors - as such, being global really makes the most sense. I don’t know that anyone would be pleased to sprinkle using ActiveSupport in all their files that use it - they don’t even want to THINK about the fact that they’re using it.
-
- Jul 2022
-
stackoverflow.com stackoverflow.com
-
Create a new controller to override the original: app/controllers/active_storage/blobs_controller.rb
Original comment:
I've never seen monkey patching done quite like this.
Usually you can't just "override" a class. You can only reopen it. You can't change its superclass. (If you needed to, you'd have to remove the old constant first.)
Rails has already defined ActiveStorage::BlobsController!
I believe the only reason this works:
class ActiveStorage::BlobsController < ActiveStorage::BaseController
is because it's reopening the existing class. We don't even need to specify the
< Base
class. (We can't change it, in any case.)They do the same thing here: - https://github.com/ackama/rails-template/pull/284/files#diff-2688f6f31a499b82cb87617d6643a0a5277dc14f35f15535fd27ef80a68da520
Correction: I guess this doesn't actually monkey patch it. I guess it really does override the original from activestorage gem and prevent it from getting loaded. How does it do that? I'm guessing it's because activestorage relies on autoloading constants, and when the constant
ActiveStorage::BlobsController
is first encountered/referenced, autoloading looks in paths in a certain order, and finds the version in the app'sapp/controllers/active_storage/blobs_controller.rb
before it ever gets a chance to look in the gem's paths for that same path/file.If instead of using autoloading, it had used
require_relative
(or evenrequire
?? but that might have still found the app-defined version earlier in the load path), then it would have loaded the model from activestorage first, and then (possibly) loaded the model from our app, which (probably) would have reopened it, as I originally commented.
-
-
github.com github.com
-
ActiveSupport.on_load :active_storage_blob do def accessible_to?(accessor) attachments.includes(:record).any? { |attachment| attachment.accessible_to?(accessor) } || attachments.none? end end ActiveSupport.on_load :active_storage_attachment do def accessible_to?(accessor) record.try(:accessible_to?, accessor) end end ActiveSupport.on_load :action_text_rich_text do def accessible_to?(accessor) record.try(:accessible_to?, accessor) end end module ActiveStorage::Authorize extend ActiveSupport::Concern included do before_action :require_authorization end private def require_authorization head :forbidden unless authorized? end def authorized? @blob.accessible_to?(Current.identity) end end Rails.application.config.to_prepare do ActiveStorage::Blobs::RedirectController.include ActiveStorage::Authorize ActiveStorage::Blobs::ProxyController.include ActiveStorage::Authorize ActiveStorage::Representations::RedirectController.include ActiveStorage::Authorize ActiveStorage::Representations::ProxyController.include ActiveStorage::Authorize end
Interesting, rather clean approach, I think
-
- Aug 2021
-
-
I have a rule that I won't allow Capybara to be monkey-patched in Poltergeist. This gives some indication to users about whether something is non-standard. So basically all non-standard stuff must be on page.driver rather than page (or a node).
-
- Jul 2021
-
deveiate.org deveiate.org
-
If you prefer monkeypatching (around 70) linguistics methods directly onto core classes, you can do that by adding a 'monkeypatch' option to ::use:
-
- Apr 2021
-
github.com github.com
-
Yes, you are right. That was a very bad workaround. Stubbing methods on NilClass can be compared to switching to dark side of force. Powerful but comes with a huge price. I highly don't recommend using my workaround from 1 year ago.
-
- Mar 2021
-
askubuntu.com askubuntu.com
-
Now, if you also want deskopen to pass through any command-line parameters, you can instead use this slightly-modified version: #!/bin/sh desktop_file=$1 shift `grep '^Exec' "${desktop_file}" | sed 's/^Exec=//' | sed 's/%.//'` "$@" &
-
-
unix.stackexchange.com unix.stackexchange.com
-
A solution is almost indicated in the question: hinder xdg-open from choosing exo-open. A brute-force approach is to copy /usr/bin/xdg-open to /usr/local/bin (/usr/local/bin is earlier in PATH unless PATH has been modified) and to patch it to use open_generic instead of exo_open (unlike the XFCE4-specific exo-open, open_generic does honor xdg mime types)
-
-
trailblazer.to trailblazer.to
-
patch: -> { step Destroy.method(:tidy_storage), before: :delete_model }
-
- Feb 2021
-
trailblazer.to trailblazer.to
-
Patching has no implicit, magical side-effects and is strongly encouraged to customize flows for a specific case in a quick and consise way.
-
-
dry-rb.org dry-rb.org
-
Another solution is using the Safe Navigation Operator &. introduced in Ruby 2.3 which is a bit better because this is a language feature rather than an opinionated runtime environment pollution
-
-
github.com github.com
-
I don't see a way to hook into Devise's route mapping load process to add the action (several monkey patching attempts failed)
-
-
stackoverflow.com stackoverflow.com
-
According to this comment you might want to override the structurally_incompatible_values_for_or to overcome the issue: def structurally_incompatible_values_for_or(other) Relation::SINGLE_VALUE_METHODS.reject { |m| send("#{m}_value") == other.send("#{m}_value") } + (Relation::MULTI_VALUE_METHODS - [:eager_load, :references, :extending]).reject { |m| send("#{m}_values") == other.send("#{m}_values") } + (Relation::CLAUSE_METHODS - [:having, :where]).reject { |m| send("#{m}_clause") == other.send("#{m}_clause") } end
-
- Dec 2020
-
www.npmjs.com www.npmjs.com
-
You can afford to make a proper PR to upstream.
-
The change would be useful to other people as-is.
-
The change is too consequential to be developed in situ.
-
Patches can be reviewed as part of your normal review process, forks probably can't
-
Get told in big red letters when the dependency changed and you need to check that your fix is still valid.
-
Keep your patches colocated with the code that depends on them.
-
Sometimes forks need extra build steps, e.g. with react-native for Android. Forget that noise.
Tags
- too hard/complicated/non-trivial
- forking (patching vs. forking)
- proper
- pull request
- limited time: not right now (maybe later)
- when to _
- helpful
- review process
- code organization: co-location
- taking the time to do it right/properly
- patching (patching vs. forking)
- patching vs. forking
- doing it right/properly
- errors/warnings are helpful for development
- advantages/merits/pros
Annotators
URL
-
- Oct 2020
-
ponyfoo.com ponyfoo.comPony Foo1
-
There are other features you *could* actually polyfill, such as Array.of, Number.isNaN or Object.assign, because those don’t introduce syntax changes to the language – except that you shouldn’t.
-
-
humanwhocodes.com humanwhocodes.com
-
github.com github.com
-
Polyfills are naughty as they patch native APIs, while ponyfills are pure and don't affect the environment.
-
How are ponyfills better than polyfills? A polyfill is code that adds missing functionality by monkey patching an API. Unfortunately, it usually globally patches built-ins, which affects all code running in the environment. This is especially problematic when a polyfill is not fully spec compliant (which in some cases is impossible), as it could cause very hard to debug bugs and inconsistencies. Or when the spec for a new feature changes and your code depends on behavior that a module somewhere else in the dependency tree polyfills differently. In general, you should not modify API's you don't own.
-
- Sep 2020
-
github.com github.com
-
Want to use this now? Existing projects A more robust and maintainable way to incorporate this patch into your workflow using patch-package:
-
- Jul 2020
-
github.com github.com
-
But I'll definitely take underscore mixins over extending String.prototype or other clunky implementations any day.
-
- Mar 2020
-
www.sitepoint.com www.sitepoint.com
-
Instead of re-opening Ruby classes like that (I get involuntary twitches), for our little exercise we are going to invent another name
IMHO, re-opening classes is okay. Certainly better than duplicating an entire core Ruby class and giving it a silly, less-meaningful name. (Though I'm not sure he actually intended people to use Lax instead of Lazy. I think he was just showing how easy it is to implement Lazy from scratch in Ruby.)
-
-
stackoverflow.com stackoverflow.com
- Oct 2019
- Jan 2019
-
www.bitwig.com www.bitwig.com
-
allowing common connections to be made wirelessly
This sounds a bit like “send” and “receive” in some (visual) programming languages and modular synths. But the fact that it’s based on common connections sounds quite clever.
-