diff options
author | Xavier Noria <fxn@hashref.com> | 2010-08-20 17:18:57 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2010-08-20 17:18:57 +0200 |
commit | 0c75ec51a9b60252125534b8f9a3f6406713a957 (patch) | |
tree | 34165e93f7dd01157f77e4e70a46ec6a5bed77c8 /activerecord | |
parent | 87398e531d0bb23b30787fe26d86678dba322895 (diff) | |
parent | 2ffa50f5a9fac08e08869687006031b70322497a (diff) | |
download | rails-0c75ec51a9b60252125534b8f9a3f6406713a957.tar.gz rails-0c75ec51a9b60252125534b8f9a3f6406713a957.tar.bz2 rails-0c75ec51a9b60252125534b8f9a3f6406713a957.zip |
Merge remote branch 'rails/master'
Diffstat (limited to 'activerecord')
21 files changed, 97 insertions, 77 deletions
diff --git a/activerecord/Rakefile b/activerecord/Rakefile index c1e90cc099..c2d63cda23 100644 --- a/activerecord/Rakefile +++ b/activerecord/Rakefile @@ -1,8 +1,5 @@ -gem 'rdoc', '>= 2.5.9' -require 'rdoc' require 'rake' require 'rake/testtask' -require 'rdoc/task' require 'rake/packagetask' require 'rake/gempackagetask' @@ -163,28 +160,6 @@ end task :build_frontbase_databases => 'frontbase:build_databases' task :rebuild_frontbase_databases => 'frontbase:rebuild_databases' - -# Generate the RDoc documentation - -RDoc::Task.new { |rdoc| - rdoc.rdoc_dir = 'doc' - rdoc.title = "Active Record -- Object-relation mapping put on rails" - rdoc.options << '-f' << 'horo' - rdoc.options << '--main' << 'README.rdoc' - rdoc.options << '--charset' << 'utf-8' - rdoc.rdoc_files.include('README.rdoc', 'RUNNING_UNIT_TESTS', 'CHANGELOG') - rdoc.rdoc_files.include('lib/**/*.rb') - rdoc.rdoc_files.exclude('lib/active_record/vendor/*') - rdoc.rdoc_files.include('dev-utils/*.rb') -} - -# Enhance rdoc task to copy referenced images also -task :rdoc do - FileUtils.mkdir_p "doc/files/examples/" - FileUtils.copy "examples/associations.png", "doc/files/examples/associations.png" -end - - spec = eval(File.read('activerecord.gemspec')) Rake::GemPackageTask.new(spec) do |p| diff --git a/activerecord/activerecord.gemspec b/activerecord/activerecord.gemspec index 67d521d56b..a3c0acb370 100644 --- a/activerecord/activerecord.gemspec +++ b/activerecord/activerecord.gemspec @@ -24,5 +24,5 @@ Gem::Specification.new do |s| s.add_dependency('activesupport', version) s.add_dependency('activemodel', version) s.add_dependency('arel', '~> 0.4.0') - s.add_dependency('tzinfo', '~> 0.3.22') + s.add_dependency('tzinfo', '~> 0.3.23') end diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index b5159eead3..132e9cf882 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -492,7 +492,12 @@ module ActiveRecord def create_record(attrs) attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash) ensure_owner_is_not_new - record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) do + + _scope = self.construct_scope[:create] + csm = @reflection.klass.send(:current_scoped_methods) + options = (csm.blank? || !_scope.is_a?(Hash)) ? _scope : _scope.merge(csm.where_values_hash) + + record = @reflection.klass.send(:with_scope, :create => options) do @reflection.build_association(attrs) end if block_given? diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 9d3ee9528a..90241dd897 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -918,7 +918,11 @@ module ActiveRecord #:nodoc: self else begin - compute_type(type_name) + if store_full_sti_class + ActiveSupport::Dependencies.constantize(type_name) + else + compute_type(type_name) + end rescue NameError raise SubclassNotFound, "The single-table inheritance mechanism failed to locate the subclass: '#{type_name}'. " + @@ -1171,7 +1175,7 @@ MSG if type_name.match(/^::/) # If the type is prefixed with a scope operator then we assume that # the type_name is an absolute reference. - type_name.constantize + ActiveSupport::Dependencies.constantize(type_name) else # Build a list of candidates to search for candidates = [] @@ -1180,7 +1184,7 @@ MSG candidates.each do |candidate| begin - constant = candidate.constantize + constant = ActiveSupport::Dependencies.constantize(candidate) return constant if candidate == constant.to_s rescue NameError => e # We don't want to swallow NoMethodError < NameError errors diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb index c203581735..a31973d529 100644 --- a/activerecord/lib/active_record/callbacks.rb +++ b/activerecord/lib/active_record/callbacks.rb @@ -235,7 +235,7 @@ module ActiveRecord :after_initialize, :after_find, :after_touch, :before_validation, :after_validation, :before_save, :around_save, :after_save, :before_create, :around_create, :after_create, :before_update, :around_update, :after_update, - :before_destroy, :around_destroy, :after_destroy + :before_destroy, :around_destroy, :after_destroy, :after_commit, :after_rollback ] included do diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index 288ce5aebb..37e584a629 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -93,11 +93,7 @@ module ActiveRecord # #connection can be called any number of times; the connection is # held in a hash keyed by the thread id. def connection - if conn = @reserved_connections[current_connection_id] - conn - else - @reserved_connections[current_connection_id] = checkout - end + @reserved_connections[current_connection_id] ||= checkout end # Signal that the thread is finished with the current connection. @@ -326,7 +322,7 @@ module ActiveRecord # already been opened. def connected?(klass) conn = retrieve_connection_pool(klass) - conn ? conn.connected? : false + conn && conn.connected? end # Remove the connection for this class. This will close the active diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 9950387420..d797f9c6c3 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -31,6 +31,7 @@ module ActiveRecord mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslca] || config[:sslkey] default_flags = Mysql.const_defined?(:CLIENT_MULTI_RESULTS) ? Mysql::CLIENT_MULTI_RESULTS : 0 + default_flags |= Mysql::CLIENT_FOUND_ROWS if Mysql.const_defined?(:CLIENT_FOUND_ROWS) options = [host, username, password, database, port, socket, default_flags] ConnectionAdapters::MysqlAdapter.new(mysql, logger, options, config) end diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index cfe4d23965..03b06205d4 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -316,16 +316,19 @@ module ActiveRecord @to_sql ||= arel.to_sql end - def scope_for_create - @scope_for_create ||= begin - @create_with_value || Hash[ - @where_values.find_all { |w| + def where_values_hash + Hash[@where_values.find_all { |w| w.respond_to?(:operator) && w.operator == :== }.map { |where| [where.operand1.name, where.operand2.respond_to?(:value) ? where.operand2.value : where.operand2] }] + end + + def scope_for_create + @scope_for_create ||= begin + @create_with_value || where_values_hash end end diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 64edcc1ef3..7cf73f7eb3 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -163,19 +163,17 @@ module ActiveRecord def perform_calculation(operation, column_name, options = {}) operation = operation.to_s.downcase + distinct = nil + if operation == "count" column_name ||= (select_for_count || :all) - joins = arel.joins(arel) - if joins.present? && joins =~ /LEFT OUTER/i + if arel.joins(arel) =~ /LEFT OUTER/i distinct = true column_name = @klass.primary_key if column_name == :all end - distinct = nil if column_name.to_s =~ /\s*DISTINCT\s+/i - distinct ||= options[:distinct] - else - distinct = nil + distinct = nil if column_name =~ /\s*DISTINCT\s+/i end distinct = options[:distinct] || distinct diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 0bf0b37900..0b5e9b4fb2 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -229,7 +229,7 @@ module ActiveRecord arel.project(selects.last) end else - arel.project(@klass.quoted_table_name + '.*') + arel.project(Arel::SqlLiteral.new(@klass.quoted_table_name + '.*')) end end diff --git a/activerecord/test/cases/adapters/mysql2/connection_test.rb b/activerecord/test/cases/adapters/mysql2/connection_test.rb index b973da621b..26091c713b 100644 --- a/activerecord/test/cases/adapters/mysql2/connection_test.rb +++ b/activerecord/test/cases/adapters/mysql2/connection_test.rb @@ -27,7 +27,7 @@ class MysqlConnectionTest < ActiveRecord::TestCase sleep 2 @connection.verify! assert @connection.active? - end + end private diff --git a/activerecord/test/cases/associations/extension_test.rb b/activerecord/test/cases/associations/extension_test.rb index e9240de673..efaab8569e 100644 --- a/activerecord/test/cases/associations/extension_test.rb +++ b/activerecord/test/cases/associations/extension_test.rb @@ -46,16 +46,13 @@ class AssociationsExtensionsTest < ActiveRecord::TestCase assert_equal projects(:action_controller), david.projects_extended_by_name.find_most_recent end + def test_extension_name + extension = Proc.new {} + name = :association_name - def test_extension_name - extension = Proc.new {} - name = :association_name - - assert_equal 'DeveloperAssociationNameAssociationExtension', Developer.send(:create_extension_modules, name, extension, []).first.name - assert_equal 'MyApplication::Business::DeveloperAssociationNameAssociationExtension', MyApplication::Business::Developer.send(:create_extension_modules, name, extension, []).first.name + assert_equal 'DeveloperAssociationNameAssociationExtension', Developer.send(:create_extension_modules, name, extension, []).first.name + assert_equal 'MyApplication::Business::DeveloperAssociationNameAssociationExtension', MyApplication::Business::Developer.send(:create_extension_modules, name, extension, []).first.name assert_equal 'MyApplication::Business::DeveloperAssociationNameAssociationExtension', MyApplication::Business::Developer.send(:create_extension_modules, name, extension, []).first.name assert_equal 'MyApplication::Business::DeveloperAssociationNameAssociationExtension', MyApplication::Business::Developer.send(:create_extension_modules, name, extension, []).first.name end - - end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 7e10a8ceeb..63fc15bca3 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -13,6 +13,8 @@ require 'models/reader' require 'models/tagging' require 'models/invoice' require 'models/line_item' +require 'models/car' +require 'models/bulb' class HasManyAssociationsTestForCountWithFinderSql < ActiveRecord::TestCase class Invoice < ActiveRecord::Base @@ -47,6 +49,23 @@ class HasManyAssociationsTest < ActiveRecord::TestCase Client.destroyed_client_ids.clear end + def test_create_from_association_should_respect_default_scope + car = Car.create(:name => 'honda') + assert_equal 'honda', car.name + + bulb = Bulb.create + assert_equal 'defaulty', bulb.name + + bulb = car.bulbs.build + assert_equal 'defaulty', bulb.name + + bulb = car.bulbs.create + assert_equal 'defaulty', bulb.name + + bulb = car.bulbs.create(:name => 'exotic') + assert_equal 'exotic', bulb.name + end + def test_create_resets_cached_counters person = Person.create!(:first_name => 'tenderlove') post = Post.first diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 4693cb45fc..52382f5afc 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -249,8 +249,8 @@ class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::Test assert_equal customer1, order.billing assert_equal customer2, order.shipping - assert_equal num_orders +1, Order.count - assert_equal num_customers +2, Customer.count + assert_equal num_orders + 1, Order.count + assert_equal num_customers + 2, Customer.count end def test_store_association_in_two_relations_with_one_save @@ -268,8 +268,8 @@ class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::Test assert_equal customer, order.billing assert_equal customer, order.shipping - assert_equal num_orders +1, Order.count - assert_equal num_customers +1, Customer.count + assert_equal num_orders + 1, Order.count + assert_equal num_customers + 1, Customer.count end def test_store_association_in_two_relations_with_one_save_in_existing_object @@ -287,8 +287,8 @@ class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::Test assert_equal customer, order.billing assert_equal customer, order.shipping - assert_equal num_orders +1, Order.count - assert_equal num_customers +1, Customer.count + assert_equal num_orders + 1, Order.count + assert_equal num_customers + 1, Customer.count end def test_store_association_in_two_relations_with_one_save_in_existing_object_with_values @@ -311,14 +311,14 @@ class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::Test assert_equal customer, order.billing assert_equal customer, order.shipping - assert_equal num_orders +1, Order.count - assert_equal num_customers +2, Customer.count + assert_equal num_orders + 1, Order.count + assert_equal num_customers + 2, Customer.count end def test_store_association_with_a_polymorphic_relationship num_tagging = Tagging.count tags(:misc).create_tagging(:taggable => posts(:thinking)) - assert_equal num_tagging +1, Tagging.count + assert_equal num_tagging + 1, Tagging.count end end @@ -372,7 +372,7 @@ class TestDefaultAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCa assert firm.save assert !client.new_record? - assert_equal no_of_clients+1, Client.count + assert_equal no_of_clients + 1, Client.count end def test_invalid_build @@ -403,8 +403,8 @@ class TestDefaultAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCa assert !new_firm.new_record? assert !c.new_record? assert_equal new_firm, c.firm - assert_equal no_of_firms+1, Firm.count # Firm was saved to database. - assert_equal no_of_clients+2, Client.count # Clients were saved to database. + assert_equal no_of_firms + 1, Firm.count # Firm was saved to database. + assert_equal no_of_clients + 2, Client.count # Clients were saved to database. assert_equal 2, new_firm.clients_of_firm.size assert_equal 2, new_firm.clients_of_firm(true).size @@ -1061,7 +1061,7 @@ module AutosaveAssociationOnACollectionAssociationTests end def test_should_allow_to_bypass_validations_on_the_associated_models_on_create - assert_difference("#{ @association_name == :birds ? 'Bird' : 'Parrot' }.count", +2) do + assert_difference("#{ @association_name == :birds ? 'Bird' : 'Parrot' }.count", 2) do 2.times { @pirate.send(@association_name).build } @pirate.save(:validate => false) end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index b2799f649a..55f0b1ce21 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1409,7 +1409,7 @@ class BasicsTest < ActiveRecord::TestCase end def test_compute_type_no_method_error - String.any_instance.stubs(:constantize).raises(NoMethodError) + ActiveSupport::Dependencies.stubs(:constantize).raises(NoMethodError) assert_raises NoMethodError do ActiveRecord::Base.send :compute_type, 'InvalidModel' end diff --git a/activerecord/test/cases/callbacks_test.rb b/activerecord/test/cases/callbacks_test.rb index dc7f82b001..8a84f19836 100644 --- a/activerecord/test/cases/callbacks_test.rb +++ b/activerecord/test/cases/callbacks_test.rb @@ -461,7 +461,12 @@ class CallbacksTest < ActiveRecord::TestCase [ :before_validation, :proc ], [ :before_validation, :object ], [ :before_validation, :block ], - [ :before_validation, :returning_false ] + [ :before_validation, :returning_false ], + [ :after_rollback, :block ], + [ :after_rollback, :object ], + [ :after_rollback, :proc ], + [ :after_rollback, :string ], + [ :after_rollback, :method ], ], david.history end diff --git a/activerecord/test/cases/pooled_connections_test.rb b/activerecord/test/cases/pooled_connections_test.rb index e61960059e..de5fa140ba 100644 --- a/activerecord/test/cases/pooled_connections_test.rb +++ b/activerecord/test/cases/pooled_connections_test.rb @@ -89,7 +89,7 @@ class PooledConnectionsTest < ActiveRecord::TestCase def test_undefined_connection_returns_false old_handler = ActiveRecord::Base.connection_handler ActiveRecord::Base.connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new - assert_equal false, ActiveRecord::Base.connected? + assert ! ActiveRecord::Base.connected? ensure ActiveRecord::Base.connection_handler = old_handler end diff --git a/activerecord/test/cases/transaction_callbacks_test.rb b/activerecord/test/cases/transaction_callbacks_test.rb index cc146f5574..85f222bca2 100644 --- a/activerecord/test/cases/transaction_callbacks_test.rb +++ b/activerecord/test/cases/transaction_callbacks_test.rb @@ -260,22 +260,26 @@ class TransactionObserverCallbacksTest < ActiveRecord::TestCase class TopicWithObserverAttachedObserver < ActiveRecord::Observer def after_commit(record) - record.history.push :"TopicWithObserverAttachedObserver#after_commit" + record.history.push "after_commit" end def after_rollback(record) - record.history.push :"TopicWithObserverAttachedObserver#after_rollback" + record.history.push "after_rollback" end end def test_after_commit_called + assert TopicWithObserverAttachedObserver.instance, 'should have observer' + topic = TopicWithObserverAttached.new topic.save! - assert_equal topic.history, [:"TopicWithObserverAttachedObserver#after_commit"] + assert_equal %w{ after_commit }, topic.history end def test_after_rollback_called + assert TopicWithObserverAttachedObserver.instance, 'should have observer' + topic = TopicWithObserverAttached.new Topic.transaction do @@ -283,6 +287,6 @@ class TransactionObserverCallbacksTest < ActiveRecord::TestCase raise ActiveRecord::Rollback end - assert_equal topic.history, [:"TopicWithObserverObserver#after_rollback"] + assert_equal %w{ after_rollback }, topic.history end end diff --git a/activerecord/test/models/bulb.rb b/activerecord/test/models/bulb.rb new file mode 100644 index 0000000000..9eefc5803a --- /dev/null +++ b/activerecord/test/models/bulb.rb @@ -0,0 +1,7 @@ +class Bulb < ActiveRecord::Base + + default_scope :conditions => {:name => 'defaulty' } + + belongs_to :car + +end diff --git a/activerecord/test/models/car.rb b/activerecord/test/models/car.rb index faf4e6cbc0..903ec53288 100644 --- a/activerecord/test/models/car.rb +++ b/activerecord/test/models/car.rb @@ -1,4 +1,5 @@ class Car < ActiveRecord::Base + has_many :bulbs has_many :tyres has_many :engines has_many :wheels, :as => :wheelable diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 8017e13920..7657e00800 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -198,6 +198,11 @@ ActiveRecord::Schema.define do t.integer :car_id end + create_table :bulbs, :force => true do |t| + t.integer :car_id + t.string :name + end + create_table :entrants, :force => true do |t| t.string :name, :null => false t.integer :course_id, :null => false |