From 040ee1d651c481326193c971935c06bd762594cb Mon Sep 17 00:00:00 2001 From: Brennan Dunn Date: Mon, 18 May 2009 20:19:49 -0400 Subject: [PATCH] Support for multiparameter attribute assignment on virtual attribute writers --- activerecord/lib/active_record/base.rb | 3 ++- activerecord/test/cases/base_test.rb | 7 +++++++ activerecord/test/models/customer.rb | 3 +++ 3 files changed, 12 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 8a65945..f1441fe 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -3043,7 +3043,8 @@ module ActiveRecord #:nodoc: def execute_callstack_for_multiparameter_attributes(callstack) errors = [] callstack.each do |name, values| - klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass + reflection_or_column = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)) + klass = reflection_or_column ? reflection_or_column.klass : Time if values.empty? send(name + "=", nil) else diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 59aa695..16ac9f9 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1161,6 +1161,13 @@ class BasicsTest < ActiveRecord::TestCase customer.attributes = attributes assert_equal address, customer.address end + + def test_multiparameter_assignment_on_virtual_attributes + customer = Customer.new + attributes = { "birthday(1i)" => "1984", "birthday(2i)" => "7", "birthday(3i)" => "9" } + customer.attributes = attributes + assert_equal Time.local(1984, 7, 9), customer.birthday + end def test_attributes_on_dummy_time # Oracle, and Sybase do not have a TIME datatype. diff --git a/activerecord/test/models/customer.rb b/activerecord/test/models/customer.rb index e258ccd..6a41406 100644 --- a/activerecord/test/models/customer.rb +++ b/activerecord/test/models/customer.rb @@ -3,6 +3,9 @@ class Customer < ActiveRecord::Base composed_of :balance, :class_name => "Money", :mapping => %w(balance amount), :converter => Proc.new { |balance| balance.to_money } composed_of :gps_location, :allow_nil => true composed_of :fullname, :mapping => %w(name to_s), :constructor => Proc.new { |name| Fullname.parse(name) }, :converter => :parse + + attr_accessor :birthday + end class Address -- 1.6.2.2