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.

3 comments:

Tyler Tompkins said...

The code you posted is very elegant. It's inspired me to do further investigations into the use of modules in ruby/rails.

Charly said...

Thanks ! I would love it if this avenue was explored wether it leads to a change or not.
Whenever i'll have time i'll put my nose back into it, and try and keep the blog updated. Their are many things to consider :
- include vs extend
- super_as(Validation) in facets lib
- the new inheritance scheme in 1.9

etc etc

Avdi said...

Nice. I am so glad to see people reawakening to the power of Modules in Ruby.