aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Koziarski <michael@koziarski.com>2007-06-30 03:31:48 +0000
committerMichael Koziarski <michael@koziarski.com>2007-06-30 03:31:48 +0000
commitd0ff4ec1936b6888ce199ef5131c3a2d866a968c (patch)
tree2432d3e567b68d6ee2a0e328ff8b2cd9438aa075
parent20045df2606a6c0c62bacfb9f9cb815ed11f7dd8 (diff)
downloadrails-d0ff4ec1936b6888ce199ef5131c3a2d866a968c.tar.gz
rails-d0ff4ec1936b6888ce199ef5131c3a2d866a968c.tar.bz2
rails-d0ff4ec1936b6888ce199ef5131c3a2d866a968c.zip
Don't call unsupported methods on associated objects when using :include, :method with to_xml [manfred, jwilger] Closes #7307
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7156 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/xml_serialization.rb6
-rw-r--r--activerecord/test/fixtures/author.rb4
-rw-r--r--activerecord/test/xml_serialization_test.rb16
4 files changed, 24 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 156f55e94e..a4130b8a5f 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Don't call unsupported methods on associated objects when using :include, :method with to_xml #7307, [manfred, jwilger]
+
* Define collection singular ids method for has_many :through associations. #8763 [lifofifo]
* Array attribute conditions work with proxied association collections. #8318 [kamal, theamazingrando]
diff --git a/activerecord/lib/active_record/xml_serialization.rb b/activerecord/lib/active_record/xml_serialization.rb
index df50ecc017..88ff46a004 100644
--- a/activerecord/lib/active_record/xml_serialization.rb
+++ b/activerecord/lib/active_record/xml_serialization.rb
@@ -178,10 +178,12 @@ module ActiveRecord #:nodoc:
end
def serializable_method_attributes
- Array(options[:methods]).collect { |name| MethodAttribute.new(name.to_s, @record) }
+ Array(options[:methods]).inject([]) do |method_attributes, name|
+ method_attributes << MethodAttribute.new(name.to_s, @record) if @record.respond_to?(name.to_s)
+ method_attributes
+ end
end
-
def add_attributes
(serializable_attributes + serializable_method_attributes).each do |attribute|
add_tag(attribute)
diff --git a/activerecord/test/fixtures/author.rb b/activerecord/test/fixtures/author.rb
index 6b34c7522c..61ba3c1551 100644
--- a/activerecord/test/fixtures/author.rb
+++ b/activerecord/test/fixtures/author.rb
@@ -67,6 +67,10 @@ class Author < ActiveRecord::Base
@post_log = []
end
+ def label
+ "#{id}-#{name}"
+ end
+
private
def log_before_adding(object)
@post_log << "before_adding#{object.id}"
diff --git a/activerecord/test/xml_serialization_test.rb b/activerecord/test/xml_serialization_test.rb
index 5cb40752ec..8c59d5d26b 100644
--- a/activerecord/test/xml_serialization_test.rb
+++ b/activerecord/test/xml_serialization_test.rb
@@ -1,6 +1,7 @@
require 'abstract_unit'
require 'fixtures/post'
require 'fixtures/author'
+require 'fixtures/tagging'
class Contact < ActiveRecord::Base
# mock out self.columns so no pesky db is needed for these tests
@@ -142,12 +143,23 @@ class DatabaseConnectedXmlSerializationTest < Test::Unit::TestCase
second_xml_size = david.to_xml(options).size
assert_equal first_xml_size, second_xml_size
end
-
def test_include_uses_association_name
- xml = authors(:david).to_xml :include=>:hello_posts, :indent=>0
+ xml = authors(:david).to_xml :include=>:hello_posts, :indent => 0
assert_match %r{<hello-posts type="array">}, xml
assert_match %r{<post>}, xml
assert_match %r{<sti-post>}, xml
end
+
+ def test_methods_are_called_on_object
+ xml = authors(:david).to_xml :methods => :label, :indent => 0
+ assert_match %r{<label>.*</label>}, xml
+ end
+
+ def test_should_not_call_methods_on_associations_that_dont_respond
+ xml = authors(:david).to_xml :include=>:hello_posts, :methods => :label, :indent => 2
+ assert !authors(:david).hello_posts.first.respond_to?(:label)
+ assert_match %r{^ <label>.*</label>}, xml
+ assert_no_match %r{^ <label>}, xml
+ end
end \ No newline at end of file