From 78a18392e15c3de1c70b44e285d1742687624dd1 Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Tue, 11 Nov 2008 11:22:13 +0100 Subject: Remove redundant uniq --- activerecord/lib/active_record/association_preload.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index 6e194ab9b4..69300e5ce5 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -312,7 +312,7 @@ module ActiveRecord table_name = klass.quoted_table_name primary_key = klass.primary_key column_type = klass.columns.detect{|c| c.name == primary_key}.type - ids = id_map.keys.uniq.map do |id| + ids = id_map.keys.map do |id| if column_type == :integer id.to_i elsif column_type == :float -- cgit v1.2.3 From 57d795bad43d4a3e5eef7151099a8e40808a1031 Mon Sep 17 00:00:00 2001 From: Ken Collins Date: Thu, 13 Nov 2008 10:45:57 -0600 Subject: Make sure any Fixnum returned by a DB sum is type cast to a Float before standard converstion to a BigDecimal [#8994 state:resolved] Signed-off-by: Joshua Peek --- activerecord/lib/active_record/calculations.rb | 2 +- activerecord/test/cases/calculations_test.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index 6f4e02b430..65512d534a 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -286,7 +286,7 @@ module ActiveRecord case operation when 'count' then value.to_i when 'sum' then type_cast_using_column(value || '0', column) - when 'avg' then value && (value == 0 ? 0.0.to_d : value.to_d) + when 'avg' then value && (value.is_a?(Fixnum) ? value.to_f : value).to_d else type_cast_using_column(value, column) end end diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 0fa61500c0..8bd0dd0f6e 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -25,6 +25,11 @@ class CalculationsTest < ActiveRecord::TestCase def test_should_return_nil_as_average assert_nil NumericData.average(:bank_balance) end + + def test_type_cast_calculated_value_should_convert_db_averages_of_fixnum_class_to_decimal + assert_equal 0, NumericData.send(:type_cast_calculated_value, 0, nil, 'avg') + assert_equal 53.0, NumericData.send(:type_cast_calculated_value, 53, nil, 'avg') + end def test_should_get_maximum_of_field assert_equal 60, Account.maximum(:credit_limit) -- cgit v1.2.3 From 9a88ab64bb45ddb2bdcf80fab9203111d8f8abb4 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 12 Nov 2008 11:33:09 -0800 Subject: Move fixtures settings from AR::TestCase to railties test_help --- activerecord/lib/active_record/test_case.rb | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/test_case.rb b/activerecord/lib/active_record/test_case.rb index eabf06fc3b..d5f43f56e6 100644 --- a/activerecord/lib/active_record/test_case.rb +++ b/activerecord/lib/active_record/test_case.rb @@ -1,15 +1,7 @@ require "active_support/test_case" -module ActiveRecord +module ActiveRecord class TestCase < ActiveSupport::TestCase #:nodoc: - self.fixture_path = FIXTURES_ROOT - self.use_instantiated_fixtures = false - self.use_transactional_fixtures = true - - def create_fixtures(*table_names, &block) - Fixtures.create_fixtures(FIXTURES_ROOT, table_names, {}, &block) - end - def assert_date_from_db(expected, actual, message = nil) # SQL Server doesn't have a separate column type just for dates, # so the time is in the string and incorrectly formatted -- cgit v1.2.3 From db7daa04b858f224b8b34a5dc94fe5804c1c6400 Mon Sep 17 00:00:00 2001 From: Amos King Date: Mon, 10 Nov 2008 09:26:19 -0600 Subject: Fix typo in pool_conections_test [#1350 state:committed] Signed-off-by: David Heinemeier Hansson --- activerecord/test/cases/pooled_connections_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/test/cases/pooled_connections_test.rb b/activerecord/test/cases/pooled_connections_test.rb index 2a5e9509b3..2649a9358a 100644 --- a/activerecord/test/cases/pooled_connections_test.rb +++ b/activerecord/test/cases/pooled_connections_test.rb @@ -77,7 +77,7 @@ class PooledConnectionsTest < ActiveRecord::TestCase conn_pool.checkin(conn) end - def test_not_connected_defined_connection_reutnrs_false + def test_not_connected_defined_connection_returns_false ActiveRecord::Base.establish_connection(@connection) assert ! ActiveRecord::Base.connected? end -- cgit v1.2.3 From ff4ccb8334e4f5b5bdccbd5dc6876b4e43d9565e Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 14 Nov 2008 12:01:26 +0100 Subject: Revert "Move fixtures settings from AR::TestCase to railties test_help" -- it broke all the tests! This reverts commit 9a88ab64bb45ddb2bdcf80fab9203111d8f8abb4. --- activerecord/lib/active_record/test_case.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/test_case.rb b/activerecord/lib/active_record/test_case.rb index d5f43f56e6..eabf06fc3b 100644 --- a/activerecord/lib/active_record/test_case.rb +++ b/activerecord/lib/active_record/test_case.rb @@ -1,7 +1,15 @@ require "active_support/test_case" -module ActiveRecord +module ActiveRecord class TestCase < ActiveSupport::TestCase #:nodoc: + self.fixture_path = FIXTURES_ROOT + self.use_instantiated_fixtures = false + self.use_transactional_fixtures = true + + def create_fixtures(*table_names, &block) + Fixtures.create_fixtures(FIXTURES_ROOT, table_names, {}, &block) + end + def assert_date_from_db(expected, actual, message = nil) # SQL Server doesn't have a separate column type just for dates, # so the time is in the string and incorrectly formatted -- cgit v1.2.3 From 61e43700b85de753b6254893d5365e04d3465b9a Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 14 Nov 2008 12:26:50 +0100 Subject: Prepare for RC2 --- activerecord/CHANGELOG | 2 +- activerecord/lib/active_record/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 4ca062b535..c2299b56ad 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,4 +1,4 @@ -*2.2.1 [RC2 or 2.2 final]* +*2.2.1 [RC2] (November 14th, 2008)* * Ensure indices don't flip order in schema.rb #1266 [Jordi Bunster] diff --git a/activerecord/lib/active_record/version.rb b/activerecord/lib/active_record/version.rb index 2479b75789..3c5a9b7df8 100644 --- a/activerecord/lib/active_record/version.rb +++ b/activerecord/lib/active_record/version.rb @@ -2,7 +2,7 @@ module ActiveRecord module VERSION #:nodoc: MAJOR = 2 MINOR = 2 - TINY = 0 + TINY = 1 STRING = [MAJOR, MINOR, TINY].join('.') end -- cgit v1.2.3 From 3be853b59d3175e74ef4564b78309c10bc0cc550 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 14 Nov 2008 14:08:26 +0100 Subject: A few more dependency updates --- activerecord/Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/Rakefile b/activerecord/Rakefile index f192646547..49c51ecb0c 100644 --- a/activerecord/Rakefile +++ b/activerecord/Rakefile @@ -171,7 +171,7 @@ spec = Gem::Specification.new do |s| s.files = s.files + Dir.glob( "#{dir}/**/*" ).delete_if { |item| item.include?( "\.svn" ) } end - s.add_dependency('activesupport', '= 2.2.0' + PKG_BUILD) + s.add_dependency('activesupport', '= 2.2.1' + PKG_BUILD) s.files.delete FIXTURES_ROOT + "/fixture_database.sqlite" s.files.delete FIXTURES_ROOT + "/fixture_database_2.sqlite" -- cgit v1.2.3 From d3fd9971093101712e4cc97ccc534631888b673d Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Sat, 15 Nov 2008 01:59:12 -0500 Subject: fix assignment to has_one :through associations. Signed-off-by: Michael Koziarski --- .../associations/has_one_through_association.rb | 7 ++-- .../has_one_through_associations_test.rb | 40 +++++++++++++++++++++- activerecord/test/fixtures/organizations.yml | 5 +++ activerecord/test/models/member.rb | 2 ++ activerecord/test/models/member_detail.rb | 4 +++ activerecord/test/models/organization.rb | 4 +++ activerecord/test/schema/schema.rb | 10 ++++++ 7 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 activerecord/test/fixtures/organizations.yml create mode 100644 activerecord/test/models/member_detail.rb create mode 100644 activerecord/test/models/organization.rb (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index b78bd5d931..8073ebaf9f 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -8,11 +8,10 @@ module ActiveRecord current_object = @owner.send(@reflection.through_reflection.name) if current_object - klass.destroy(current_object) - @owner.clear_association_cache + current_object.update_attributes(construct_join_attributes(new_value)) + else + @owner.send(@reflection.through_reflection.name, klass.send(:create, construct_join_attributes(new_value))) end - - @owner.send(@reflection.through_reflection.name, klass.send(:create, construct_join_attributes(new_value))) end private diff --git a/activerecord/test/cases/associations/has_one_through_associations_test.rb b/activerecord/test/cases/associations/has_one_through_associations_test.rb index ff4021fe02..7d418de965 100644 --- a/activerecord/test/cases/associations/has_one_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_through_associations_test.rb @@ -3,9 +3,11 @@ require 'models/club' require 'models/member' require 'models/membership' require 'models/sponsor' +require 'models/organization' +require 'models/member_detail' class HasOneThroughAssociationsTest < ActiveRecord::TestCase - fixtures :members, :clubs, :memberships, :sponsors + fixtures :members, :clubs, :memberships, :sponsors, :organizations def setup @member = members(:groucho) @@ -120,4 +122,40 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase clubs(:moustache_club).send(:private_method) @member.club.send(:private_method) end + + def test_assigning_to_has_one_through_preserves_decorated_join_record + @organization = organizations(:nsa) + assert_difference 'MemberDetail.count', 1 do + @member_detail = MemberDetail.new(:extra_data => 'Extra') + @member.member_detail = @member_detail + @member.organization = @organization + end + assert_equal @organization, @member.organization + assert @organization.members.include?(@member) + assert_equal 'Extra', @member.member_detail.extra_data + end + + def test_reassigning_has_one_through + @organization = organizations(:nsa) + @new_organization = organizations(:discordians) + + assert_difference 'MemberDetail.count', 1 do + @member_detail = MemberDetail.new(:extra_data => 'Extra') + @member.member_detail = @member_detail + @member.organization = @organization + end + assert_equal @organization, @member.organization + assert_equal 'Extra', @member.member_detail.extra_data + assert @organization.members.include?(@member) + assert !@new_organization.members.include?(@member) + + assert_no_difference 'MemberDetail.count' do + @member.organization = @new_organization + end + assert_equal @new_organization, @member.organization + assert_equal 'Extra', @member.member_detail.extra_data + assert !@organization.members.include?(@member) + assert @new_organization.members.include?(@member) + end + end diff --git a/activerecord/test/fixtures/organizations.yml b/activerecord/test/fixtures/organizations.yml new file mode 100644 index 0000000000..25295bff87 --- /dev/null +++ b/activerecord/test/fixtures/organizations.yml @@ -0,0 +1,5 @@ +nsa: + name: No Such Agency +discordians: + name: Discordians + diff --git a/activerecord/test/models/member.rb b/activerecord/test/models/member.rb index 688725f200..77a37abb38 100644 --- a/activerecord/test/models/member.rb +++ b/activerecord/test/models/member.rb @@ -6,4 +6,6 @@ class Member < ActiveRecord::Base has_one :favourite_club, :through => :memberships, :conditions => ["memberships.favourite = ?", true], :source => :club has_one :sponsor, :as => :sponsorable has_one :sponsor_club, :through => :sponsor + has_one :member_detail + has_one :organization, :through => :member_detail end \ No newline at end of file diff --git a/activerecord/test/models/member_detail.rb b/activerecord/test/models/member_detail.rb new file mode 100644 index 0000000000..e731454556 --- /dev/null +++ b/activerecord/test/models/member_detail.rb @@ -0,0 +1,4 @@ +class MemberDetail < ActiveRecord::Base + belongs_to :member + belongs_to :organization +end diff --git a/activerecord/test/models/organization.rb b/activerecord/test/models/organization.rb new file mode 100644 index 0000000000..d79d5037c8 --- /dev/null +++ b/activerecord/test/models/organization.rb @@ -0,0 +1,4 @@ +class Organization < ActiveRecord::Base + has_many :member_details + has_many :members, :through => :member_details +end \ No newline at end of file diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index ab5c7c520b..6217e3bc1c 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -197,6 +197,12 @@ ActiveRecord::Schema.define do t.string :name end + create_table :member_details, :force => true do |t| + t.integer :member_id + t.integer :organization_id + t.string :extra_data + end + create_table :memberships, :force => true do |t| t.datetime :joined_on t.integer :club_id, :member_id @@ -249,6 +255,10 @@ ActiveRecord::Schema.define do t.integer :shipping_customer_id end + create_table :organizations, :force => true do |t| + t.string :name + end + create_table :owners, :primary_key => :owner_id ,:force => true do |t| t.string :name end -- cgit v1.2.3 From 789a3f5b035fd293a9e235672a97b683a56ba0c3 Mon Sep 17 00:00:00 2001 From: Will Bryant Date: Fri, 14 Nov 2008 11:04:53 +1300 Subject: Moved the * strings out of construct_finder_sql to a new default_select method so it can be overridden by plugins cleanly Signed-off-by: Michael Koziarski [#1371 state:resolved] --- activerecord/lib/active_record/base.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 757102eb6b..dcc8277849 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1612,9 +1612,17 @@ module ActiveRecord #:nodoc: end end + def default_select(qualified) + if qualified + quoted_table_name + '.*' + else + '*' + end + end + def construct_finder_sql(options) scope = scope(:find) - sql = "SELECT #{options[:select] || (scope && scope[:select]) || ((options[:joins] || (scope && scope[:joins])) && quoted_table_name + '.*') || '*'} " + sql = "SELECT #{options[:select] || (scope && scope[:select]) || default_select(options[:joins] || (scope && scope[:joins]))} " sql << "FROM #{(scope && scope[:from]) || options[:from] || quoted_table_name} " add_joins!(sql, options[:joins], scope) -- cgit v1.2.3