In Chapter 5, he talks about fixing ActiveRecord's to_xml() method. I am a fan of Rails, but I am not a fan of the solution he chose in his book. IMHO, it's kind of ugly and un-DRY. In other words, inelegant.
My first attempt at a better solution is to write my own to_xml() method, which is as follows:
def to_xml(options=nil, &block)I pasted the above into each of the four models defined in the Pomodo project Peter guides his readers through in the book, and it works, albeit un-DRY.
super options == nil ? { :dasherize => false } : options.merge(:dasherize => false), &block
end
Then I did some reading on how I might override the built-in method at a single place, and came across this article. So I then removed the code from the model classes and placed the following at the very end of the pomodo/config/environment.rb file:
class ActiveRecord::BaseTo my surprise, it works, too. It has fewer lines of code than both my first stab and Peter's solution, and it is very DRY. In other words, it is elegant.
def to_xml(options=nil, &block)
super options == nil ? { :dasherize => false } : options.merge(:dasherize => false), &block
end
end
But now I am left with this question: Why does this work?
I am not as well-versed in Ruby/Rails. Can someone help enlighten me?
No comments:
Post a Comment