Changeset 7519
- Timestamp:
- 09/20/07 23:22:30 (1 year ago)
- Files:
-
- trunk/activerecord/CHANGELOG (modified) (1 diff)
- trunk/activerecord/lib/active_record.rb (modified) (2 diffs)
- trunk/activerecord/lib/active_record/associations/association_collection.rb (modified) (1 diff)
- trunk/activerecord/lib/active_record/serialization.rb (added)
- trunk/activerecord/lib/active_record/serializers (added)
- trunk/activerecord/lib/active_record/serializers/json_serializer.rb (added)
- trunk/activerecord/lib/active_record/serializers/xml_serializer.rb (moved) (moved from trunk/activerecord/lib/active_record/xml_serialization.rb) (4 diffs)
- trunk/activerecord/test/fixtures/contact.rb (added)
- trunk/activerecord/test/serialization_test.rb (added)
- trunk/activerecord/test/xml_serialization_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/CHANGELOG
r7510 r7519 1 1 *SVN* 2 3 * Added ActiveRecord::Base#to_json/from_json (currently does not support :include like to_xml) [DHH] 4 5 * Added ActiveRecord::Base#from_xml [DHH]. Example: 6 7 xml = "<person><name>David</name></person>" 8 Person.new.from_xml(xml).name # => "David" 2 9 3 10 * Define dynamic finders as real methods after first usage. [bscofield] trunk/activerecord/lib/active_record.rb
r7498 r7519 50 50 require 'active_record/schema' 51 51 require 'active_record/calculations' 52 require 'active_record/ xml_serialization'52 require 'active_record/serialization' 53 53 require 'active_record/attribute_methods' 54 54 … … 66 66 include ActiveRecord::Reflection 67 67 include ActiveRecord::Calculations 68 include ActiveRecord:: XmlSerialization68 include ActiveRecord::Serialization 69 69 include ActiveRecord::AttributeMethods 70 70 end trunk/activerecord/lib/active_record/associations/association_collection.rb
r7511 r7519 158 158 end 159 159 end 160 160 161 161 162 protected trunk/activerecord/lib/active_record/serializers/xml_serializer.rb
r7367 r7519 1 1 module ActiveRecord #:nodoc: 2 module XmlSerialization2 module Serialization 3 3 # Builds an XML document to represent the model. Some configuration is 4 4 # availble through +options+, however more complicated cases should use … … 125 125 block_given? ? serializer.to_s(&block) : serializer.to_s 126 126 end 127 128 def from_xml(xml) 129 self.attributes = Hash.from_xml(xml).values.first 130 self 131 end 127 132 end 128 133 129 class XmlSerializer #:nodoc: 130 attr_reader :options 131 132 def initialize(record, options = {}) 133 @record, @options = record, options.dup 134 end 135 134 class XmlSerializer < ActiveRecord::Serialization::Serializer #:nodoc: 136 135 def builder 137 136 @builder ||= begin … … 165 164 # :only is set, always delete :except. 166 165 def serializable_attributes 167 attribute_names = @record.attribute_names 168 169 if options[:only] 170 options.delete(:except) 171 attribute_names = attribute_names & Array(options[:only]).collect { |n| n.to_s } 172 else 173 options[:except] = Array(options[:except]) | Array(@record.class.inheritance_column) 174 attribute_names = attribute_names - options[:except].collect { |n| n.to_s } 175 end 176 177 attribute_names.collect { |name| Attribute.new(name, @record) } 166 serializable_attribute_names.collect { |name| Attribute.new(name, @record) } 178 167 end 179 168 … … 266 255 end 267 256 end 268 269 alias_method :to_s, :serialize270 257 271 258 class Attribute #:nodoc: trunk/activerecord/test/xml_serialization_test.rb
r7173 r7519 1 1 require 'abstract_unit' 2 require 'fixtures/contact' 2 3 require 'fixtures/post' 3 4 require 'fixtures/author' 4 5 require 'fixtures/tagging' 5 6 require 'fixtures/comment' 6 7 class Contact < ActiveRecord::Base8 # mock out self.columns so no pesky db is needed for these tests9 def self.columns() @columns ||= []; end10 def self.column(name, sql_type = nil, default = nil, null = true)11 columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)12 end13 14 column :name, :string15 column :age, :integer16 column :avatar, :binary17 column :created_at, :datetime18 column :awesome, :boolean19 column :preferences, :string20 21 serialize :preferences22 end23 7 24 8 class XmlSerializationTest < Test::Unit::TestCase … … 48 32 end 49 33 50 def test_should_allow_attribute_filtering51 @xml = Contact.new.to_xml :only => [:age, :name]52 assert_match %r{<name}, @xml53 assert_match %r{<age}, @xml54 assert_no_match %r{<created-at}, @xml55 56 @xml = Contact.new.to_xml :except => [:age, :name]57 assert_no_match %r{<name}, @xml58 assert_no_match %r{<age}, @xml59 assert_match %r{<created-at}, @xml60 end61 62 34 def test_should_include_yielded_additions 63 35 @xml = Contact.new.to_xml do |xml|