diff options
Diffstat (limited to 'activemodel/lib/active_model/serializers/xml.rb')
-rwxr-xr-x[-rw-r--r--] | activemodel/lib/active_model/serializers/xml.rb | 46 |
1 files changed, 38 insertions, 8 deletions
diff --git a/activemodel/lib/active_model/serializers/xml.rb b/activemodel/lib/active_model/serializers/xml.rb index 5084298210..cf742d0569 100644..100755 --- a/activemodel/lib/active_model/serializers/xml.rb +++ b/activemodel/lib/active_model/serializers/xml.rb @@ -110,12 +110,18 @@ module ActiveModel end end - # TODO This can likely be cleaned up to simple use ActiveSupport::XmlMini.to_tag as well. + # TODO: This can likely be cleaned up to simple use ActiveSupport::XmlMini.to_tag as well. def add_associations(association, records, opts) merged_options = opts.merge(options.slice(:builder, :indent)) merged_options[:skip_instruct] = true - if records.is_a?(Enumerable) + [:skip_types, :dasherize, :camelize].each do |key| + merged_options[key] = options[key] if merged_options[key].nil? && !options[key].nil? + end + + if records.respond_to?(:to_ary) + records = records.to_ary + tag = ActiveSupport::XmlMini.rename_key(association.to_s, options) type = options[:skip_types] ? { } : {:type => "array"} association_name = association.to_s.singularize @@ -159,8 +165,8 @@ module ActiveModel # Returns XML representing the model. Configuration can be # passed through +options+. # - # Without any +options+, the returned XML string will include all the model's - # attributes. For example: + # Without any +options+, the returned XML string will include all the + # model's attributes. # # user = User.find(1) # user.to_xml @@ -170,21 +176,45 @@ module ActiveModel # <id type="integer">1</id> # <name>David</name> # <age type="integer">16</age> - # <created-at type="datetime">2011-01-30T22:29:23Z</created-at> + # <created-at type="dateTime">2011-01-30T22:29:23Z</created-at> # </user> # - # The <tt>:only</tt> and <tt>:except</tt> options can be used to limit the attributes - # included, and work similar to the +attributes+ method. + # The <tt>:only</tt> and <tt>:except</tt> options can be used to limit the + # attributes included, and work similar to the +attributes+ method. # # To include the result of some method calls on the model use <tt>:methods</tt>. # # To include associations use <tt>:include</tt>. # - # For further documentation see activerecord/lib/active_record/serializers/xml_serializer.xml. + # For further documentation, see <tt>ActiveRecord::Serialization#to_xml</tt> def to_xml(options = {}, &block) Serializer.new(self, options).serialize(&block) end + # Sets the model +attributes+ from a JSON string. Returns +self+. + # + # class Person + # include ActiveModel::Serializers::Xml + # + # attr_accessor :name, :age, :awesome + # + # def attributes=(hash) + # hash.each do |key, value| + # instance_variable_set("@#{key}", value) + # end + # end + # + # def attributes + # instance_values + # end + # end + # + # xml = { name: 'bob', age: 22, awesome:true }.to_xml + # person = Person.new + # person.from_xml(xml) # => #<Person:0x007fec5e3b3c40 @age=22, @awesome=true, @name="bob"> + # person.name # => "bob" + # person.age # => 22 + # person.awesome # => true def from_xml(xml) self.attributes = Hash.from_xml(xml).values.first self |