aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2006-03-10 03:50:00 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2006-03-10 03:50:00 +0000
commit3442e0c6714d766b9ea0ca2fc644fdf7753832aa (patch)
treee5eb45c589a0e4ff340dea0c5e2e3d0700b52274 /activerecord/lib/active_record
parentdb37c0c95fea1ddb3a34665f82d3cba9bced49f8 (diff)
downloadrails-3442e0c6714d766b9ea0ca2fc644fdf7753832aa.tar.gz
rails-3442e0c6714d766b9ea0ca2fc644fdf7753832aa.tar.bz2
rails-3442e0c6714d766b9ea0ca2fc644fdf7753832aa.zip
Added options to control the :only/:except for included associations on Base#to_xml [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3832 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-xactiverecord/lib/active_record/base.rb18
1 files changed, 11 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index c84f1c7778..623e5a30e9 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1522,25 +1522,29 @@ module ActiveRecord #:nodoc:
# Turns this record into XML
def to_xml(options = {})
- options[:root] ||= self.class.to_s.underscore
- options[:except] = Array(options[:except]) << self.class.inheritance_column unless options[:only]
- only_or_except = { :only => options[:only], :except => options[:except] }
+ options[:root] ||= self.class.to_s.underscore
+ options[:except] = Array(options[:except]) << self.class.inheritance_column unless options[:only]
+ 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)
- 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
when :has_many, :has_and_belongs_to_many
records = send(association).to_a
unless records.empty?
attributes_for_xml[association] = records.collect do |record|
- record.attributes(only_or_except)
+ record.attributes(association_options)
end
end
when :has_one, :belongs_to
if record = send(association)
- attributes_for_xml[association] = record.attributes(only_or_except)
+ attributes_for_xml[association] = record.attributes(association_options)
end
end
end