Ticket #9294: to_xml_attribute_names_conflict.diff
| File to_xml_attribute_names_conflict.diff, 5.5 kB (added by juanjo.bazan, 11 months ago) |
|---|
-
activesupport/lib/active_support/vendor/builder/xmlbase.rb
old new 29 29 # is the tag name, the arguements are the same as the tags 30 30 # implemented via <tt>method_missing</tt>. 31 31 def tag!(sym, *args, &block) 32 self.__send__(sym, *args, &block) 32 #A prefix is added (_railsprefix_) to the name of the method 33 # to avoid conflicts with internal methods(like 'open', 'notify' or 'system') 34 #The prefix is eliminated in method_missing. 35 self.__send__("_rails_prefix_#{sym}", *args, &block) 33 36 end 34 37 35 38 # Create XML markup based on the name of the method. This method 36 39 # is never invoked directly, but is called for each markup method 37 40 # in the markup block. 38 41 def method_missing(sym, *args, &block) 42 #remove the prefix added in tag! method 43 sym=sym.to_s 44 sym.sub!("_rails_prefix_","") 45 39 46 text = nil 40 47 attrs = nil 41 48 sym = "#{sym}:#{args.shift}" if args.first.kind_of?(Symbol) 42 49 args.each do |arg| 43 case arg 44 when Hash 45 attrs ||= {} 46 attrs.merge!(arg) 47 else 48 text ||= '' 49 text << arg.to_s 50 end 50 case arg 51 when Hash 52 attrs ||= {} 53 attrs.merge!(arg) 54 55 else 56 text ||= '' 57 text << arg.to_s 58 end 51 59 end 52 60 if block 53 unless text.nil?54 raise ArgumentError, "XmlMarkup cannot mix a text argument with a block"55 end56 _capture_outer_self(block) unless defined?(@self) && !@self.nil?57 _indent58 _start_tag(sym, attrs)59 _newline60 _nested_structures(block)61 _indent62 _end_tag(sym)63 _newline61 unless text.nil? 62 raise ArgumentError, "XmlMarkup cannot mix a text argument with a block" 63 end 64 _capture_outer_self(block) unless defined?(@self) && !@self.nil? 65 _indent 66 _start_tag(sym, attrs) 67 _newline 68 _nested_structures(block) 69 _indent 70 _end_tag(sym) 71 _newline 64 72 elsif text.nil? 65 _indent66 _start_tag(sym, attrs, true)67 _newline73 _indent 74 _start_tag(sym, attrs, true) 75 _newline 68 76 else 69 _indent70 _start_tag(sym, attrs)71 text! text72 _end_tag(sym)73 _newline77 _indent 78 _start_tag(sym, attrs) 79 text! text 80 _end_tag(sym) 81 _newline 74 82 end 75 83 @target 76 84 end -
activerecord/test/xml_serialization_test.rb
old new 17 17 column :created_at, :datetime 18 18 column :awesome, :boolean 19 19 column :preferences, :string 20 column :open, :string 21 column :notify, :boolean 20 22 21 23 serialize :preferences 22 24 end … … 66 68 67 69 assert_match %r{<creator>David</creator>}, @xml 68 70 end 71 72 def test_attribute_filtering_should_work_with_special_names 73 @xml = Contact.new.to_xml :only => [:notify, :name] 74 assert_match %r{<notify}, @xml 75 assert_no_match %r{<open}, @xml 76 77 @xml = Contact.new.to_xml :except => [:notify] 78 assert_no_match %r{<notify}, @xml 79 assert_match %r{<open}, @xml 80 end 81 69 82 end 70 83 71 84 class DefaultXmlSerializationTest < Test::Unit::TestCase 72 85 def setup 73 @xml = Contact.new(:name => 'aaron stack', :age => 25, :avatar => 'binarydata', :created_at => Time.utc(2006, 8, 1), :awesome => false, :preferences => { :gem => 'ruby' } ).to_xml86 @xml = Contact.new(:name => 'aaron stack', :age => 25, :avatar => 'binarydata', :created_at => Time.utc(2006, 8, 1), :awesome => false, :preferences => { :gem => 'ruby' }, :open=>"24/7", :notify=>true).to_xml 74 87 end 75 88 76 89 def test_should_serialize_string … … 98 111 def test_should_serialize_yaml 99 112 assert_match %r{<preferences type=\"yaml\">--- \n:gem: ruby\n</preferences>}, @xml 100 113 end 114 115 def test_should_accept_special_named_columns 116 assert_match %r{<open>.*</open>}, @xml 117 assert_match %r{<notify type=\"boolean\">.*</notify>}, @xml 118 end 101 119 end 102 120 103 121 class NilXmlSerializationTest < Test::Unit::TestCase … … 186 204 assert types.include?('StiPost') 187 205 end 188 206 207 def test_calling_special_names 208 @xml = authors(:david).to_xml do |xml| 209 xml.open "9:00 to 17:00" 210 xml.tag!("notify",[true]) 211 end 212 213 assert_match %r{<open>9:00 to 17:00</open>}, @xml 214 assert_match %r{<notify>true</notify>}, @xml 215 end 216 189 217 end -
activerecord/lib/active_record/xml_serialization.rb
old new 320 320 end 321 321 322 322 def compute_value 323 value = @record. send(name)323 value = @record.is_a?(ActiveRecord::Base) && @record.class.serialized_attributes.has_key?(name) ? @record.send(name+"_before_type_cast") : @record.send(name) 324 324 325 325 if formatter = Hash::XML_FORMATTING[type.to_s] 326 326 value ? formatter.call(value) : nil