Skip to content

Commit

Permalink
Added options to control the :only/:except for included associations …
Browse files Browse the repository at this point in the history
…on Base#to_xml [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3832 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Mar 10, 2006
1 parent db37c0c commit 3442e0c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
18 changes: 11 additions & 7 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -1522,25 +1522,29 @@ def readonly!


# Turns this record into XML # Turns this record into XML
def to_xml(options = {}) def to_xml(options = {})
options[:root] ||= self.class.to_s.underscore options[:root] ||= self.class.to_s.underscore
options[:except] = Array(options[:except]) << self.class.inheritance_column unless options[:only] options[:except] = Array(options[:except]) << self.class.inheritance_column unless options[:only]
only_or_except = { :only => options[:only], :except => options[:except] } root_only_or_except = { :only => options[:only], :except => options[:except] }


attributes_for_xml = attributes(only_or_except) attributes_for_xml = attributes(root_only_or_except)


if include_associations = options.delete(:include) if include_associations = options.delete(:include)
for association in Array(include_associations) include_has_options = include_associations.is_a?(Hash)

for association in include_has_options ? include_associations.keys : Array(include_associations)
association_options = include_has_options ? include_associations[association] : root_only_or_except

case self.class.reflect_on_association(association).macro case self.class.reflect_on_association(association).macro
when :has_many, :has_and_belongs_to_many when :has_many, :has_and_belongs_to_many
records = send(association).to_a records = send(association).to_a
unless records.empty? unless records.empty?
attributes_for_xml[association] = records.collect do |record| attributes_for_xml[association] = records.collect do |record|
record.attributes(only_or_except) record.attributes(association_options)
end end
end end
when :has_one, :belongs_to when :has_one, :belongs_to
if record = send(association) if record = send(association)
attributes_for_xml[association] = record.attributes(only_or_except) attributes_for_xml[association] = record.attributes(association_options)
end end
end end
end end
Expand Down
11 changes: 11 additions & 0 deletions activerecord/test/base_test.rb
Expand Up @@ -1202,6 +1202,17 @@ def test_to_xml_including_multiple_associations
assert xml.include?(%(<account>)) assert xml.include?(%(<account>))
assert xml.include?(%(<clients><client>)) assert xml.include?(%(<clients><client>))
end end

def test_to_xml_including_multiple_associations_with_options
xml = companies(:first_firm).to_xml(
:indent => 0, :skip_instruct => true,
:include => { :clients => { :only => :name } }
)

assert_equal "<firm>", xml.first(6)
assert xml.include?(%(<client><name>Summit</name></client>))
assert xml.include?(%(<clients><client>))
end


def test_except_attributes def test_except_attributes
assert_equal( assert_equal(
Expand Down

0 comments on commit 3442e0c

Please sign in to comment.