How to ensure callback order?

712 Views Asked by At

Say, I have a RoR application with multiple plugins. Each of these plugins implements an after_save callback on a model. I need to create one more plugin with two more callbacks, say, start and finish. How do I ensure the following order of callback execution?

  • MyPlugin.start
  • OtherPlugin_1.after_save_1
  • OtherPlugin_1.after_save_X
  • OtherPlugin_X.after_save_1
  • ...
  • MyPlugin.finish

I.e. my callback should be called before any other after_save and immediately after all other after_save callbacks have finished execution.

1

There are 1 best solutions below

0
Andriy Lesyuk On

This should be possible by using prepend: true for MyPlugin.start, as follows:

after_save :start, prepend: true

Assuming, that no other plugin uses :prepend.

Regarding MyPlugin.finish: the only option (as far as I know) is to make sure, that the plugin is executed last (e.g., by naming it zzz).