Allow modifying class methods/properties in plugin-api?

plugin-api.modifyClass allows us to overwrite instance methods of existing classes, but there is no analogous way to overwrite class methods. It would be cool if there were.

In my case, I want to overwrite the user model’s createAccount method (https://github.com/discourse/discourse/blob/9df7fd4f308e007e4aa2cd17e40cee92b68fad07/app/assets/javascripts/discourse/models/user.js.es6#L624).

I got around it with this, drawing heavily on plugin-api.modifyClass

const klass = api.container.factoryFor('model:user');

klass.class.reopenClass({
  createAccount(attrs) {
    console.log('overwrites createAccount')
  }
})  

… which seems to work, but it’s a little kludgy.

@eviltrout thoughts? I think adding reopenClass or something like that in plugin api makes sense, not sure what the right naming here is though.

Sure reopenClass would be a good addition to the plugin api if you want to submit a PR @shoshber

4 Likes

Ok, I will do that! :grinning:

2 Likes

Quick update … we now have modifyClassStatic thanks to @shoshber

3 Likes