02 October 2008

Rails refactoring exercise

I started my blog after reading jay field's post on the underuse of modules and 'design pattern in ruby' book. Mainly because i thought it would be interesting, as a learning process, to explore the silent power of modules.

So knowing Rails was doing a heavy use of alias_method_chain as it's AOP trick, i did a shallow dive in ActiveRecord's code to see how it could be transformed in the MultiInheritance-like-style of modules.




UPDATE
I published the post unfinished not thinking it would attract much attention without annoucement : so sorry for the lack of explanation.
  • the main idea is to move (almost?) all public instance methods of ActiveRecord::Base in a Behavior module called ActiveRecord::Core - which makes sense since they're the methods to be deCORAted.
  • The uninclude gem is here to enable the possibility for a plugin to override an AR "core" behavior in a sub "everyday" model.
  • Another way I thought of was this (below), unfortunately i get : NoMethodError: super: no superclass method ‘save’




  • another solution would be to use the callstack to intercept the presence of MyTransaction#save and tell Transaction to do a transparent super.