diff options
author | Rob Anderton <rob.anderton@thewebfellas.com> | 2008-08-18 19:13:01 +0100 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2008-09-10 18:28:47 +0200 |
commit | 2cee51d5c1d143f6fe0096ba6cbd1db1ecbe2d90 (patch) | |
tree | 69385e859aef550418365d4553bebe5a4af5cb77 /activerecord/test | |
parent | 7c9851dbb6f841ffbf3ebd16e9f57c04319d3a39 (diff) | |
download | rails-2cee51d5c1d143f6fe0096ba6cbd1db1ecbe2d90.tar.gz rails-2cee51d5c1d143f6fe0096ba6cbd1db1ecbe2d90.tar.bz2 rails-2cee51d5c1d143f6fe0096ba6cbd1db1ecbe2d90.zip |
Added :constructor and :converter options to composed_of and deprecated the conversion block
Signed-off-by: Michael Koziarski <michael@koziarski.com>
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/aggregations_test.rb | 35 | ||||
-rw-r--r-- | activerecord/test/fixtures/customers.yml | 11 | ||||
-rw-r--r-- | activerecord/test/models/customer.rb | 20 |
3 files changed, 64 insertions, 2 deletions
diff --git a/activerecord/test/cases/aggregations_test.rb b/activerecord/test/cases/aggregations_test.rb index 75d1f27e07..b6656c8631 100644 --- a/activerecord/test/cases/aggregations_test.rb +++ b/activerecord/test/cases/aggregations_test.rb @@ -107,6 +107,41 @@ class AggregationsTest < ActiveRecord::TestCase customers(:david).gps_location = nil assert_equal nil, customers(:david).gps_location end + + def test_custom_constructor + assert_equal 'Barney GUMBLE', customers(:barney).fullname.to_s + assert_kind_of Fullname, customers(:barney).fullname + end + + def test_custom_converter + customers(:barney).fullname = 'Barnoit Gumbleau' + assert_equal 'Barnoit GUMBLEAU', customers(:barney).fullname.to_s + assert_kind_of Fullname, customers(:barney).fullname + end +end + +class DeprecatedAggregationsTest < ActiveRecord::TestCase + class Person < ActiveRecord::Base; end + + def test_conversion_block_is_deprecated + assert_deprecated 'conversion block has been deprecated' do + Person.composed_of(:balance, :class_name => "Money", :mapping => %w(balance amount)) { |balance| balance.to_money } + end + end + + def test_conversion_block_used_when_converter_option_is_nil + Person.composed_of(:balance, :class_name => "Money", :mapping => %w(balance amount)) { |balance| balance.to_money } + assert_raise(NoMethodError) { Person.new.balance = 5 } + end + + def test_converter_option_overrides_conversion_block + Person.composed_of(:balance, :class_name => "Money", :mapping => %w(balance amount), :converter => Proc.new { |balance| Money.new(balance) }) { |balance| balance.to_money } + + person = Person.new + assert_nothing_raised { person.balance = 5 } + assert_equal 5, person.balance.amount + assert_kind_of Money, person.balance + end end class OverridingAggregationsTest < ActiveRecord::TestCase diff --git a/activerecord/test/fixtures/customers.yml b/activerecord/test/fixtures/customers.yml index f802aac5ea..0399ff83b9 100644 --- a/activerecord/test/fixtures/customers.yml +++ b/activerecord/test/fixtures/customers.yml @@ -6,7 +6,7 @@ david: address_city: Scary Town address_country: Loony Land gps_location: 35.544623640962634x-105.9309951055148 - + zaphod: id: 2 name: Zaphod @@ -14,4 +14,13 @@ zaphod: address_street: Avenue Road address_city: Hamlet Town address_country: Nation Land + gps_location: NULL + +barney: + id: 3 + name: Barney Gumble + balance: 1 + address_street: Quiet Road + address_city: Peaceful Town + address_country: Tranquil Land gps_location: NULL
\ No newline at end of file diff --git a/activerecord/test/models/customer.rb b/activerecord/test/models/customer.rb index 030bbc6237..e258ccdb6c 100644 --- a/activerecord/test/models/customer.rb +++ b/activerecord/test/models/customer.rb @@ -1,7 +1,8 @@ class Customer < ActiveRecord::Base composed_of :address, :mapping => [ %w(address_street street), %w(address_city city), %w(address_country country) ], :allow_nil => true - composed_of(:balance, :class_name => "Money", :mapping => %w(balance amount)) { |balance| balance.to_money } + 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 end class Address @@ -53,3 +54,20 @@ class GpsLocation self.latitude == other.latitude && self.longitude == other.longitude end end + +class Fullname + attr_reader :first, :last + + def self.parse(str) + return nil unless str + new(*str.to_s.split) + end + + def initialize(first, last = nil) + @first, @last = first, last + end + + def to_s + "#{first} #{last.upcase}" + end +end
\ No newline at end of file |