aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorMarcel Molina <marcel@vernix.org>2006-04-29 23:00:47 +0000
committerMarcel Molina <marcel@vernix.org>2006-04-29 23:00:47 +0000
commit3fec943aca9ab73bf00d5dffa3f73452f33bab88 (patch)
treecd708510c8be8951440580f6a233674b8066e6bf /activerecord
parentaa72c465ec4aa641e9ea161110a43ad9c02d167b (diff)
downloadrails-3fec943aca9ab73bf00d5dffa3f73452f33bab88.tar.gz
rails-3fec943aca9ab73bf00d5dffa3f73452f33bab88.tar.bz2
rails-3fec943aca9ab73bf00d5dffa3f73452f33bab88.zip
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
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/base.rb16
-rwxr-xr-xactiverecord/test/base_test.rb6
-rwxr-xr-xactiverecord/test/fixtures/company.rb4
4 files changed, 28 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 7583b72a45..bd595c177b 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Allow AR::Base#to_xml to include methods too. Closes #4921. [johan@textdrive.com]
+
* Replace superfluous name_to_class_name variant with camelize. [Marcel Molina Jr.]
* Replace alias method chaining with Module#alias_method_chain. [Marcel Molina Jr.]
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:
# <credit-limit type="integer">50</credit-limit>
# </account>
# </firm>
+ #
+ # To include any methods on the object(s) being called use :methods
+ #
+ # firm.to_xml :methods => [ :calculated_earnings, :real_earnings ]
+ #
+ # <firm>
+ # # ... normal attributes as shown above ...
+ # <calculated-earnings>100000000000000000</calculated-earnings>
+ # <real-earnings>5</real-earnings>
+ # </firm>
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)
diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb
index 2f834772a6..4ccc2c8588 100755
--- a/activerecord/test/base_test.rb
+++ b/activerecord/test/base_test.rb
@@ -1273,6 +1273,12 @@ class BasicsTest < Test::Unit::TestCase
assert xml.include?(%(<clients><client>))
end
+ def test_to_xml_including_methods
+ xml = Company.new.to_xml(:methods => :arbitrary_method, :skip_instruct => true)
+ assert_equal "<company>", xml.first(9)
+ assert xml.include?(%(<arbitrary-method>I am Jack's profound disappointment</arbitrary-method>))
+ end
+
def test_except_attributes
assert_equal(
%w( author_name type id approved replies_count bonus_time written_on content author_email_address parent_id last_read),
diff --git a/activerecord/test/fixtures/company.rb b/activerecord/test/fixtures/company.rb
index 526748784e..250deebb21 100755
--- a/activerecord/test/fixtures/company.rb
+++ b/activerecord/test/fixtures/company.rb
@@ -5,6 +5,10 @@ class Company < ActiveRecord::Base
validates_presence_of :name
has_one :dummy_account, :foreign_key => "firm_id", :class_name => "Account"
+
+ def arbitrary_method
+ "I am Jack's profound disappointment"
+ end
end