From 3fec943aca9ab73bf00d5dffa3f73452f33bab88 Mon Sep 17 00:00:00 2001 From: Marcel Molina Date: Sat, 29 Apr 2006 23:00:47 +0000 Subject: Allow AR::Base#to_xml to include methods too. Closes #4921. [johan@textdrive.com] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4314 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/base.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index e7fa8434e6..511af69428 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1690,6 +1690,16 @@ module ActiveRecord #:nodoc: # 50 # # + # + # To include any methods on the object(s) being called use :methods + # + # firm.to_xml :methods => [ :calculated_earnings, :real_earnings ] + # + # + # # ... normal attributes as shown above ... + # 100000000000000000 + # 5 + # def to_xml(options = {}) options[:root] ||= self.class.to_s.underscore options[:except] = Array(options[:except]) << self.class.inheritance_column unless options[:only] # skip type column @@ -1697,6 +1707,12 @@ module ActiveRecord #:nodoc: attributes_for_xml = attributes(root_only_or_except) + if methods_to_call = options.delete(:methods) + [*methods_to_call].each do |meth| + attributes_for_xml[meth] = send(meth) + end + end + if include_associations = options.delete(:include) include_has_options = include_associations.is_a?(Hash) -- cgit v1.2.3