diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/aggregations.rb | 3 | ||||
-rw-r--r-- | activerecord/test/aggregations_test.rb | 22 | ||||
-rw-r--r-- | activerecord/test/fixtures/customer.rb | 17 | ||||
-rw-r--r-- | activerecord/test/fixtures/customers.yml | 1 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/db2.sql | 1 | ||||
-rwxr-xr-x | activerecord/test/fixtures/db_definitions/mysql.sql | 1 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/oci.sql | 1 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/postgresql.sql | 1 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/sqlite.sql | 3 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/sqlserver.sql | 1 |
11 files changed, 47 insertions, 6 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 756ed0907b..2ef7bc581a 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added better defaults for composed_of, so statements like composed_of :time_zone, :mapping => %w( time_zone time_zone ) can be written without the mapping part (it's now assumed) + * Added MacroReflection#macro which will return a symbol describing the macro used (like :composed_of or :has_many) #718, #248 [james@slashetc.com] diff --git a/activerecord/lib/active_record/aggregations.rb b/activerecord/lib/active_record/aggregations.rb index 6a82e53b03..208dc35321 100644 --- a/activerecord/lib/active_record/aggregations.rb +++ b/activerecord/lib/active_record/aggregations.rb @@ -118,12 +118,13 @@ module ActiveRecord # composed_of :temperature, :mapping => %w(reading celsius) # composed_of :balance, :class_name => "Money", :mapping => %w(balance amount) # composed_of :address, :mapping => [ %w(address_street street), %w(address_city city) ] + # composed_of :gps_location def composed_of(part_id, options = {}) validate_options([ :class_name, :mapping ], options.keys) name = part_id.id2name class_name = options[:class_name] || name_to_class_name(name) - mapping = options[:mapping] + mapping = options[:mapping] || [ name, name ] reader_method(name, class_name, mapping) writer_method(name, class_name, mapping) diff --git a/activerecord/test/aggregations_test.rb b/activerecord/test/aggregations_test.rb index 99b7b72684..94830862b5 100644 --- a/activerecord/test/aggregations_test.rb +++ b/activerecord/test/aggregations_test.rb @@ -2,10 +2,7 @@ require 'abstract_unit' require 'fixtures/customer' class AggregationsTest < Test::Unit::TestCase - def setup - @customers = create_fixtures "customers" - @david = Customer.find(1) - end + fixtures :customers def test_find_single_value_object assert_equal 50, @david.balance.amount @@ -30,4 +27,21 @@ class AggregationsTest < Test::Unit::TestCase @david.balance = Money.new(100) assert_raises(TypeError) { @david.balance.instance_eval { @amount = 20 } } end + + def test_inferred_mapping + assert_equal "35.544623640962634", @david.gps_location.latitude + assert_equal "-105.9309951055148", @david.gps_location.longitude + + @david.gps_location = GpsLocation.new("39x-110") + + assert_equal "39", @david.gps_location.latitude + assert_equal "-110", @david.gps_location.longitude + + @david.save + + @david.reload + + assert_equal "39", @david.gps_location.latitude + assert_equal "-110", @david.gps_location.longitude + end end
\ No newline at end of file diff --git a/activerecord/test/fixtures/customer.rb b/activerecord/test/fixtures/customer.rb index 5275a5209d..c36d4d33a8 100644 --- a/activerecord/test/fixtures/customer.rb +++ b/activerecord/test/fixtures/customer.rb @@ -1,6 +1,7 @@ class Customer < ActiveRecord::Base composed_of :address, :mapping => [ %w(address_street street), %w(address_city city), %w(address_country country) ] composed_of :balance, :class_name => "Money", :mapping => %w(balance amount) + composed_of :gps_location end class Address @@ -27,4 +28,20 @@ class Money def exchange_to(other_currency) Money.new((amount * EXCHANGE_RATES["#{currency}_TO_#{other_currency}"]).floor, other_currency) end +end + +class GpsLocation + attr_reader :gps_location + + def initialize(gps_location) + @gps_location = gps_location + end + + def latitude + gps_location.split("x").first + end + + def longitude + gps_location.split("x").last + end end
\ No newline at end of file diff --git a/activerecord/test/fixtures/customers.yml b/activerecord/test/fixtures/customers.yml index 1a83a54a9c..9169d7d413 100644 --- a/activerecord/test/fixtures/customers.yml +++ b/activerecord/test/fixtures/customers.yml @@ -5,3 +5,4 @@ david: address_street: Funny Street address_city: Scary Town address_country: Loony Land + gps_location: 35.544623640962634x-105.9309951055148 diff --git a/activerecord/test/fixtures/db_definitions/db2.sql b/activerecord/test/fixtures/db_definitions/db2.sql index 46e326d504..07569c27ed 100644 --- a/activerecord/test/fixtures/db_definitions/db2.sql +++ b/activerecord/test/fixtures/db_definitions/db2.sql @@ -58,6 +58,7 @@ CREATE TABLE customers ( address_street varchar(100) default NULL, address_city varchar(100) default NULL, address_country varchar(100) default NULL, + gps_location varchar(100) default NULL, PRIMARY KEY (id) ); diff --git a/activerecord/test/fixtures/db_definitions/mysql.sql b/activerecord/test/fixtures/db_definitions/mysql.sql index 1064373f7f..c864074dbd 100755 --- a/activerecord/test/fixtures/db_definitions/mysql.sql +++ b/activerecord/test/fixtures/db_definitions/mysql.sql @@ -59,6 +59,7 @@ CREATE TABLE `customers` ( `address_street` varchar(100) default NULL, `address_city` varchar(100) default NULL, `address_country` varchar(100) default NULL, + `gps_location` varchar(100) default NULL, PRIMARY KEY (`id`) ); diff --git a/activerecord/test/fixtures/db_definitions/oci.sql b/activerecord/test/fixtures/db_definitions/oci.sql index 86d7a03165..16d6e660a2 100644 --- a/activerecord/test/fixtures/db_definitions/oci.sql +++ b/activerecord/test/fixtures/db_definitions/oci.sql @@ -83,6 +83,7 @@ create table customers ( address_street varchar(100) default null, address_city varchar(100) default null, address_country varchar(100) default null, + gps_location varchar(100) default null, primary key (id) ); diff --git a/activerecord/test/fixtures/db_definitions/postgresql.sql b/activerecord/test/fixtures/db_definitions/postgresql.sql index f779027286..ab392c5688 100644 --- a/activerecord/test/fixtures/db_definitions/postgresql.sql +++ b/activerecord/test/fixtures/db_definitions/postgresql.sql @@ -65,6 +65,7 @@ CREATE TABLE customers ( address_street character varying, address_city character varying, address_country character varying, + gps_location character varying, PRIMARY KEY (id) ); SELECT setval('customers_id_seq', 100); diff --git a/activerecord/test/fixtures/db_definitions/sqlite.sql b/activerecord/test/fixtures/db_definitions/sqlite.sql index 988973b0ff..d57c3889e0 100644 --- a/activerecord/test/fixtures/db_definitions/sqlite.sql +++ b/activerecord/test/fixtures/db_definitions/sqlite.sql @@ -53,7 +53,8 @@ CREATE TABLE 'customers' ( 'balance' INTEGER DEFAULT 0, 'address_street' TEXT DEFAULT NULL, 'address_city' TEXT DEFAULT NULL, - 'address_country' TEXT DEFAULT NULL + 'address_country' TEXT DEFAULT NULL, + 'gps_location' TEXT DEFAULT NULL ); CREATE TABLE 'movies' ( diff --git a/activerecord/test/fixtures/db_definitions/sqlserver.sql b/activerecord/test/fixtures/db_definitions/sqlserver.sql index 743a5383fe..d658b6266d 100644 --- a/activerecord/test/fixtures/db_definitions/sqlserver.sql +++ b/activerecord/test/fixtures/db_definitions/sqlserver.sql @@ -57,6 +57,7 @@ CREATE TABLE customers ( address_street varchar(100) default NULL, address_city varchar(100) default NULL, address_country varchar(100) default NULL, + gps_location varchar(100) default NULL, PRIMARY KEY (id) ); |