ActiveSupport::Concern support Module#prepend?

I’ve monkey patched this behavior into a gem and I was wondering if core would be interested in a PR to add Module#prepend support to ActiveSupport#Concern. I’ll add tests of course.

The code is here:

My monkey patch work for what I’m trying to do but I suspect a better implementation would actually have @_dependencies be an array of arrays with each set being the module in the first element then :include or :prepend in the send. Then when append_features or prepend_features iterates over @_dependencies the send element will be used to call the proper inclusion method.

The only wild card is how ClassMethods will get extended. My understanding is that extend will append class methods to the base class. I cannot seem to find a prepend version of extend.


Something like this would be a better implementation:

Resurrecting a three-year-old thread…

Is there any interest in AS::Concern better supporting prepend? I would find it very useful. There seem to be some solid implementation ideas in the quoted message. Did anyone ever submit as a PR? Is it worthwhile to do? I couldn’t find any existing github Issues or PR’s on this.


Resurrenting a five-year-old thread…

If the thread is about having a concern, and allow users to include or prepend it.

It works as of now, for me at least.

jona…@friendsoftheweb.com於 2016年4月7日星期四 UTC+9上午3時47分43秒寫道:

Sorry I need to clarify. I just found out that we added some dark magic in order for prepend to work in concern.

So just ordinary concern wouldn’t work with prepend.

ma…@goodlife.tw於 2018年6月1日星期五 UTC+8上午9時32分08秒寫道:

Whilst this discussion was started quite a few years ago, it is still or again relevant, now that Rails 5 has deprecated alias_method_chain and in the meantime we’ve entered the realm of Rails 6.

There is no good way to prepend a concern to an existing class in Rails. This while there are quite a few cases in which it would make sense to wrap user-defined code in some concern-defined (e.g. library-defined) way.

What are your current thoughts about this?

~Wiebe-Marten (“Marten”) Wijnja / Qqwy

FWIW, we had a similar problem and ended up writing this lib to help: