In Rails, I’m coding a series of controllers to generate XML. Each time I’m passing a number of properties in to to_xml like:
to_xml(:skip_types => true, :dasherize => false)
Is there a way I can set these as new default properties that will apply whenever to_xml is called in my app so that I don’t have to repeat myself?
Are you calling to_xml on a hash or an ActiveRecord model (or something else)?
I am not that you would want to, but you can easily monkey patch to_xml and redefine it to start with those parameters. I would suggest that you make a new method to_default_xml that simply called to_xml with the parameters you wanted
Update:
Since you want to add this to a couple of ActiveRecord models you could do two things, open up ActiveRecord::base (which is a bit hackish and fragile) or create a module and import it into every model you want to use with it. A little more typing, but much cleaner code.
I would put a class in lib/ that looks something like this:
Then in your models: