From 32089cbcc9ca3fb935f783e7a7ef2b60b7d43006 Mon Sep 17 00:00:00 2001 From: Wes Oldenbeuving Date: Wed, 5 Nov 2008 18:27:23 +0100 Subject: Ensure ActiveRecord::ConnectionPool.connected? handles undefined connections. [#936 state:resolved] Signed-off-by: Pratik Naik --- .../active_record/connection_adapters/abstract/connection_pool.rb | 2 +- activerecord/test/cases/pooled_connections_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) (limited to 'activerecord') 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 432c341e6c..3016c329bd 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -324,7 +324,7 @@ module ActiveRecord # Returns true if a connection that's accessible to this class has # already been opened. def connected?(klass) - retrieve_connection_pool(klass).connected? + conn = retrieve_connection_pool(klass) ? conn.connected? : false end # Remove the connection for this class. This will close the active diff --git a/activerecord/test/cases/pooled_connections_test.rb b/activerecord/test/cases/pooled_connections_test.rb index 078ca1d679..3e8c617a89 100644 --- a/activerecord/test/cases/pooled_connections_test.rb +++ b/activerecord/test/cases/pooled_connections_test.rb @@ -73,6 +73,14 @@ class PooledConnectionsTest < ActiveRecord::TestCase assert ActiveRecord::ConnectionAdapters::AbstractAdapter === conn conn_pool.checkin(conn) end + + 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? + ensure + ActiveRecord::Base.connection_handler = old_handler + end end unless %w(FrontBase).include? ActiveRecord::Base.connection.adapter_name class AllowConcurrencyDeprecatedTest < ActiveRecord::TestCase -- cgit v1.2.3 From 0832bc63f4047d0ad0171c411460db23450213aa Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Thu, 6 Nov 2008 01:29:09 +0530 Subject: Make sure ActiveRecord::Base.connected? doesn't raise an exception for defined connections --- .../active_record/connection_adapters/abstract/connection_pool.rb | 3 ++- activerecord/test/cases/pooled_connections_test.rb | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'activerecord') 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 3016c329bd..54a17e20a9 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -324,7 +324,8 @@ module ActiveRecord # Returns true if a connection that's accessible to this class has # already been opened. def connected?(klass) - conn = retrieve_connection_pool(klass) ? conn.connected? : false + conn = retrieve_connection_pool(klass) + conn ? conn.connected? : false end # Remove the connection for this class. This will close the active diff --git a/activerecord/test/cases/pooled_connections_test.rb b/activerecord/test/cases/pooled_connections_test.rb index 3e8c617a89..36b45868b9 100644 --- a/activerecord/test/cases/pooled_connections_test.rb +++ b/activerecord/test/cases/pooled_connections_test.rb @@ -74,6 +74,11 @@ class PooledConnectionsTest < ActiveRecord::TestCase conn_pool.checkin(conn) end + def test_not_connected_defined_connection_reutnrs_false + ActiveRecord::Base.establish_connection(@connection) + assert ! ActiveRecord::Base.connected? + end + def test_undefined_connection_returns_false old_handler = ActiveRecord::Base.connection_handler ActiveRecord::Base.connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new -- cgit v1.2.3 From 077773257b682b7929e77ced3bbf46acf56a10c9 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 6 Nov 2008 10:00:15 +0100 Subject: Stop logging SHOW FIELDS and SET SQL_AUTO_IS_NULL=0 for the MysqlAdapter as they only clutter up the log and offer no value [DHH] --- activerecord/CHANGELOG | 2 ++ .../lib/active_record/connection_adapters/mysql_adapter.rb | 12 ++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 4ca062b535..154d9f71c4 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *2.2.1 [RC2 or 2.2 final]* +* Stop logging SHOW FIELDS and SET SQL_AUTO_IS_NULL=0 as they only clutter up the log and offer no value [DHH] + * Ensure indices don't flip order in schema.rb #1266 [Jordi Bunster] * Fixed that serialized strings should never be type-casted (i.e. turning "Yes" to a boolean) #857 [Andreas Korth] diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 1e452ae88a..edf54026ff 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -305,8 +305,12 @@ module ActiveRecord rows end - def execute(sql, name = nil) #:nodoc: - log(sql, name) { @connection.query(sql) } + def execute(sql, name = nil, skip_logging = false) #:nodoc: + if skip_logging + @connection.query(sql) + else + log(sql, name) { @connection.query(sql) } + end rescue ActiveRecord::StatementInvalid => exception if exception.message.split(":").first =~ /Packets out of order/ raise ActiveRecord::StatementInvalid, "'Packets out of order' error was received from the database. Please update your mysql bindings (gem install mysql) and read http://dev.mysql.com/doc/mysql/en/password-hashing.html for more information. If you're on Windows, use the Instant Rails installer to get the updated mysql bindings." @@ -437,7 +441,7 @@ module ActiveRecord def columns(table_name, name = nil)#:nodoc: sql = "SHOW FIELDS FROM #{quote_table_name(table_name)}" columns = [] - execute(sql, name).each { |field| columns << MysqlColumn.new(field[0], field[4], field[1], field[2] == "YES") } + execute(sql, name, true).each { |field| columns << MysqlColumn.new(field[0], field[4], field[1], field[2] == "YES") } columns end @@ -555,7 +559,7 @@ module ActiveRecord # By default, MySQL 'where id is null' selects the last inserted id. # Turn this off. http://dev.rubyonrails.org/ticket/6778 - execute("SET SQL_AUTO_IS_NULL=0") + execute("SET SQL_AUTO_IS_NULL=0", "ID NULL OFF", true) end def select(sql, name = nil) -- cgit v1.2.3 From a358d87e16fa876de29286b69474ab6aaee4a80b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 6 Nov 2008 13:02:32 +0100 Subject: Fixed the sanitize helper to avoid double escaping already properly escaped entities [#683 state:committed] --- activerecord/CHANGELOG | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 154d9f71c4..290c0d785c 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,6 +1,6 @@ *2.2.1 [RC2 or 2.2 final]* -* Stop logging SHOW FIELDS and SET SQL_AUTO_IS_NULL=0 as they only clutter up the log and offer no value [DHH] +* Stop logging SHOW FIELDS and SET SQL_AUTO_IS_NULL=0 for the MysqlAdapter as they only clutter up the log and offer no value [DHH] * Ensure indices don't flip order in schema.rb #1266 [Jordi Bunster] -- cgit v1.2.3 From 77697e03353ec06a4b12f42a32d7569797d1eb8f Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 6 Nov 2008 17:10:16 -0600 Subject: Fix memory leak issue in ActiveRecord scoped_methods --- activerecord/lib/active_record/base.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index a36a137f0d..757102eb6b 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2023,8 +2023,7 @@ module ActiveRecord #:nodoc: end def scoped_methods #:nodoc: - scoped_methods = (Thread.current[:scoped_methods] ||= {}) - scoped_methods[self] ||= [] + Thread.current[:"#{self}_scoped_methods"] ||= [] end def current_scoped_methods #:nodoc: -- cgit v1.2.3 From 9d4337ea13be371fd3fbf3ca8ba467e810888c37 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Fri, 7 Nov 2008 07:31:01 +0000 Subject: Revert commit which breaks all the tests. This reverts commit 8adb79b9b5983cda8dbdd4ef401661fbd51d8844. Conflicts: activerecord/CHANGELOG --- activerecord/CHANGELOG | 2 -- .../lib/active_record/connection_adapters/mysql_adapter.rb | 12 ++++-------- 2 files changed, 4 insertions(+), 10 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 290c0d785c..4ca062b535 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,7 +1,5 @@ *2.2.1 [RC2 or 2.2 final]* -* Stop logging SHOW FIELDS and SET SQL_AUTO_IS_NULL=0 for the MysqlAdapter as they only clutter up the log and offer no value [DHH] - * Ensure indices don't flip order in schema.rb #1266 [Jordi Bunster] * Fixed that serialized strings should never be type-casted (i.e. turning "Yes" to a boolean) #857 [Andreas Korth] diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index edf54026ff..1e452ae88a 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -305,12 +305,8 @@ module ActiveRecord rows end - def execute(sql, name = nil, skip_logging = false) #:nodoc: - if skip_logging - @connection.query(sql) - else - log(sql, name) { @connection.query(sql) } - end + def execute(sql, name = nil) #:nodoc: + log(sql, name) { @connection.query(sql) } rescue ActiveRecord::StatementInvalid => exception if exception.message.split(":").first =~ /Packets out of order/ raise ActiveRecord::StatementInvalid, "'Packets out of order' error was received from the database. Please update your mysql bindings (gem install mysql) and read http://dev.mysql.com/doc/mysql/en/password-hashing.html for more information. If you're on Windows, use the Instant Rails installer to get the updated mysql bindings." @@ -441,7 +437,7 @@ module ActiveRecord def columns(table_name, name = nil)#:nodoc: sql = "SHOW FIELDS FROM #{quote_table_name(table_name)}" columns = [] - execute(sql, name, true).each { |field| columns << MysqlColumn.new(field[0], field[4], field[1], field[2] == "YES") } + execute(sql, name).each { |field| columns << MysqlColumn.new(field[0], field[4], field[1], field[2] == "YES") } columns end @@ -559,7 +555,7 @@ module ActiveRecord # By default, MySQL 'where id is null' selects the last inserted id. # Turn this off. http://dev.rubyonrails.org/ticket/6778 - execute("SET SQL_AUTO_IS_NULL=0", "ID NULL OFF", true) + execute("SET SQL_AUTO_IS_NULL=0") end def select(sql, name = nil) -- cgit v1.2.3 From 26978e3ce84eda4e659fe9724e89c51efdd01781 Mon Sep 17 00:00:00 2001 From: Tekin Suleyman Date: Thu, 6 Nov 2008 20:00:54 +0000 Subject: Added :counter_sql as a valid key for habtm associations Signed-off-by: Michael Koziarski --- activerecord/lib/active_record/associations.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index c7cb6eb966..7f7819115c 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1609,7 +1609,7 @@ module ActiveRecord :class_name, :table_name, :join_table, :foreign_key, :association_foreign_key, :select, :conditions, :include, :order, :group, :limit, :offset, :uniq, - :finder_sql, :delete_sql, :insert_sql, + :finder_sql, :counter_sql, :delete_sql, :insert_sql, :before_add, :after_add, :before_remove, :after_remove, :extend, :readonly, :validate -- cgit v1.2.3 From 32a5cfcd7f8d14407f0c00ce2cdc82b1b568438e Mon Sep 17 00:00:00 2001 From: Tekin Suleyman Date: Thu, 6 Nov 2008 21:06:40 +0000 Subject: Added tests for HABTM associations with counter_sql Signed-off-by: Michael Koziarski [#1102 state:committed] --- .../has_and_belongs_to_many_associations_test.rb | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'activerecord') diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 2949f1d304..b5bedf3704 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -68,6 +68,16 @@ class DeveloperWithSymbolsForKeys < ActiveRecord::Base :foreign_key => "developer_id" end +class DeveloperWithCounterSQL < ActiveRecord::Base + set_table_name 'developers' + has_and_belongs_to_many :projects, + :class_name => "DeveloperWithCounterSQL", + :join_table => "developers_projects", + :association_foreign_key => "project_id", + :foreign_key => "developer_id", + :counter_sql => 'SELECT COUNT(*) AS count_all FROM projects INNER JOIN developers_projects ON projects.id = developers_projects.project_id WHERE developers_projects.developer_id =#{id}' +end + class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects, :parrots, :pirates, :treasures, :price_estimates, :tags, :taggings @@ -739,6 +749,19 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_nothing_raised { david.projects.count(:all, :conditions => '1=1') } end + def test_count + david = Developer.find(1) + assert_equal 2, david.projects.count + end + + def test_count_with_counter_sql + developer = DeveloperWithCounterSQL.create(:name => 'tekin') + developer.project_ids = [projects(:active_record).id] + developer.save + developer.reload + assert_equal 1, developer.projects.count + end + uses_mocha 'mocking Post.transaction' do def test_association_proxy_transaction_method_starts_transaction_in_association_class Post.expects(:transaction) -- cgit v1.2.3 From b0ee1bdf2650d7a8380d4e9be58bba8d9c5bd40e Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 7 Nov 2008 15:40:56 -0500 Subject: Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures instead. --- activerecord/lib/active_record/fixtures.rb | 284 ++++++++++++++-------------- activerecord/lib/active_record/test_case.rb | 3 + activerecord/test/cases/helper.rb | 17 +- 3 files changed, 153 insertions(+), 151 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 114141a646..24aabf0359 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -813,186 +813,190 @@ class Fixture #:nodoc: end end -module Test #:nodoc: - module Unit #:nodoc: - class TestCase #:nodoc: - setup :setup_fixtures - teardown :teardown_fixtures - - superclass_delegating_accessor :fixture_path - superclass_delegating_accessor :fixture_table_names - superclass_delegating_accessor :fixture_class_names - superclass_delegating_accessor :use_transactional_fixtures - superclass_delegating_accessor :use_instantiated_fixtures # true, false, or :no_instances - superclass_delegating_accessor :pre_loaded_fixtures - - self.fixture_table_names = [] - self.use_transactional_fixtures = false - self.use_instantiated_fixtures = true - self.pre_loaded_fixtures = false - - @@already_loaded_fixtures = {} - self.fixture_class_names = {} - - class << self - def set_fixture_class(class_names = {}) - self.fixture_class_names = self.fixture_class_names.merge(class_names) - end +module ActiveRecord + module TestFixtures + def self.included(base) + base.class_eval do + setup :setup_fixtures + teardown :teardown_fixtures + + superclass_delegating_accessor :fixture_path + superclass_delegating_accessor :fixture_table_names + superclass_delegating_accessor :fixture_class_names + superclass_delegating_accessor :use_transactional_fixtures + superclass_delegating_accessor :use_instantiated_fixtures # true, false, or :no_instances + superclass_delegating_accessor :pre_loaded_fixtures + + self.fixture_table_names = [] + self.use_transactional_fixtures = false + self.use_instantiated_fixtures = true + self.pre_loaded_fixtures = false + + @@already_loaded_fixtures = {} + self.fixture_class_names = {} + end - def fixtures(*table_names) - if table_names.first == :all - table_names = Dir["#{fixture_path}/*.yml"] + Dir["#{fixture_path}/*.csv"] - table_names.map! { |f| File.basename(f).split('.')[0..-2].join('.') } - else - table_names = table_names.flatten.map { |n| n.to_s } - end + base.extend ClassMethods + end + + module ClassMethods + def set_fixture_class(class_names = {}) + self.fixture_class_names = self.fixture_class_names.merge(class_names) + end - self.fixture_table_names |= table_names - require_fixture_classes(table_names) - setup_fixture_accessors(table_names) + def fixtures(*table_names) + if table_names.first == :all + table_names = Dir["#{fixture_path}/*.yml"] + Dir["#{fixture_path}/*.csv"] + table_names.map! { |f| File.basename(f).split('.')[0..-2].join('.') } + else + table_names = table_names.flatten.map { |n| n.to_s } end - def try_to_load_dependency(file_name) - require_dependency file_name - rescue LoadError => e - # Let's hope the developer has included it himself + self.fixture_table_names |= table_names + require_fixture_classes(table_names) + setup_fixture_accessors(table_names) + end + + def try_to_load_dependency(file_name) + require_dependency file_name + rescue LoadError => e + # Let's hope the developer has included it himself - # Let's warn in case this is a subdependency, otherwise - # subdependency error messages are totally cryptic - if ActiveRecord::Base.logger - ActiveRecord::Base.logger.warn("Unable to load #{file_name}, underlying cause #{e.message} \n\n #{e.backtrace.join("\n")}") - end + # Let's warn in case this is a subdependency, otherwise + # subdependency error messages are totally cryptic + if ActiveRecord::Base.logger + ActiveRecord::Base.logger.warn("Unable to load #{file_name}, underlying cause #{e.message} \n\n #{e.backtrace.join("\n")}") end + end - def require_fixture_classes(table_names = nil) - (table_names || fixture_table_names).each do |table_name| - file_name = table_name.to_s - file_name = file_name.singularize if ActiveRecord::Base.pluralize_table_names - try_to_load_dependency(file_name) - end + def require_fixture_classes(table_names = nil) + (table_names || fixture_table_names).each do |table_name| + file_name = table_name.to_s + file_name = file_name.singularize if ActiveRecord::Base.pluralize_table_names + try_to_load_dependency(file_name) end + end - def setup_fixture_accessors(table_names = nil) - table_names = [table_names] if table_names && !table_names.respond_to?(:each) - (table_names || fixture_table_names).each do |table_name| - table_name = table_name.to_s.tr('.', '_') + def setup_fixture_accessors(table_names = nil) + table_names = [table_names] if table_names && !table_names.respond_to?(:each) + (table_names || fixture_table_names).each do |table_name| + table_name = table_name.to_s.tr('.', '_') - define_method(table_name) do |*fixtures| - force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload + define_method(table_name) do |*fixtures| + force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload - @fixture_cache[table_name] ||= {} + @fixture_cache[table_name] ||= {} - instances = fixtures.map do |fixture| - @fixture_cache[table_name].delete(fixture) if force_reload + instances = fixtures.map do |fixture| + @fixture_cache[table_name].delete(fixture) if force_reload - if @loaded_fixtures[table_name][fixture.to_s] - @fixture_cache[table_name][fixture] ||= @loaded_fixtures[table_name][fixture.to_s].find - else - raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'" - end + if @loaded_fixtures[table_name][fixture.to_s] + @fixture_cache[table_name][fixture] ||= @loaded_fixtures[table_name][fixture.to_s].find + else + raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'" end - - instances.size == 1 ? instances.first : instances end - end - end - def uses_transaction(*methods) - @uses_transaction = [] unless defined?(@uses_transaction) - @uses_transaction.concat methods.map(&:to_s) + instances.size == 1 ? instances.first : instances + end end + end - def uses_transaction?(method) - @uses_transaction = [] unless defined?(@uses_transaction) - @uses_transaction.include?(method.to_s) - end + def uses_transaction(*methods) + @uses_transaction = [] unless defined?(@uses_transaction) + @uses_transaction.concat methods.map(&:to_s) end - def use_transactional_fixtures? - use_transactional_fixtures && - !self.class.uses_transaction?(method_name) + def uses_transaction?(method) + @uses_transaction = [] unless defined?(@uses_transaction) + @uses_transaction.include?(method.to_s) end + end - def setup_fixtures - return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank? + def use_transactional_fixtures? + use_transactional_fixtures && + !self.class.uses_transaction?(method_name) + end - if pre_loaded_fixtures && !use_transactional_fixtures - raise RuntimeError, 'pre_loaded_fixtures requires use_transactional_fixtures' - end + def setup_fixtures + return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank? - @fixture_cache = {} + if pre_loaded_fixtures && !use_transactional_fixtures + raise RuntimeError, 'pre_loaded_fixtures requires use_transactional_fixtures' + end - # Load fixtures once and begin transaction. - if use_transactional_fixtures? - if @@already_loaded_fixtures[self.class] - @loaded_fixtures = @@already_loaded_fixtures[self.class] - else - load_fixtures - @@already_loaded_fixtures[self.class] = @loaded_fixtures - end - ActiveRecord::Base.connection.increment_open_transactions - ActiveRecord::Base.connection.begin_db_transaction - # Load fixtures for every test. + @fixture_cache = {} + + # Load fixtures once and begin transaction. + if use_transactional_fixtures? + if @@already_loaded_fixtures[self.class] + @loaded_fixtures = @@already_loaded_fixtures[self.class] else - Fixtures.reset_cache - @@already_loaded_fixtures[self.class] = nil load_fixtures + @@already_loaded_fixtures[self.class] = @loaded_fixtures end - - # Instantiate fixtures for every test if requested. - instantiate_fixtures if use_instantiated_fixtures + ActiveRecord::Base.connection.increment_open_transactions + ActiveRecord::Base.connection.begin_db_transaction + # Load fixtures for every test. + else + Fixtures.reset_cache + @@already_loaded_fixtures[self.class] = nil + load_fixtures end - def teardown_fixtures - return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank? + # Instantiate fixtures for every test if requested. + instantiate_fixtures if use_instantiated_fixtures + end + + def teardown_fixtures + return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank? - unless use_transactional_fixtures? - Fixtures.reset_cache - end + unless use_transactional_fixtures? + Fixtures.reset_cache + end - # Rollback changes if a transaction is active. - if use_transactional_fixtures? && ActiveRecord::Base.connection.open_transactions != 0 - ActiveRecord::Base.connection.rollback_db_transaction - ActiveRecord::Base.connection.decrement_open_transactions - end - ActiveRecord::Base.clear_active_connections! + # Rollback changes if a transaction is active. + if use_transactional_fixtures? && ActiveRecord::Base.connection.open_transactions != 0 + ActiveRecord::Base.connection.rollback_db_transaction + ActiveRecord::Base.connection.decrement_open_transactions end + ActiveRecord::Base.clear_active_connections! + end - private - def load_fixtures - @loaded_fixtures = {} - fixtures = Fixtures.create_fixtures(fixture_path, fixture_table_names, fixture_class_names) - unless fixtures.nil? - if fixtures.instance_of?(Fixtures) - @loaded_fixtures[fixtures.name] = fixtures - else - fixtures.each { |f| @loaded_fixtures[f.name] = f } - end + private + def load_fixtures + @loaded_fixtures = {} + fixtures = Fixtures.create_fixtures(fixture_path, fixture_table_names, fixture_class_names) + unless fixtures.nil? + if fixtures.instance_of?(Fixtures) + @loaded_fixtures[fixtures.name] = fixtures + else + fixtures.each { |f| @loaded_fixtures[f.name] = f } end end + end - # for pre_loaded_fixtures, only require the classes once. huge speed improvement - @@required_fixture_classes = false + # for pre_loaded_fixtures, only require the classes once. huge speed improvement + @@required_fixture_classes = false - def instantiate_fixtures - if pre_loaded_fixtures - raise RuntimeError, 'Load fixtures before instantiating them.' if Fixtures.all_loaded_fixtures.empty? - unless @@required_fixture_classes - self.class.require_fixture_classes Fixtures.all_loaded_fixtures.keys - @@required_fixture_classes = true - end - Fixtures.instantiate_all_loaded_fixtures(self, load_instances?) - else - raise RuntimeError, 'Load fixtures before instantiating them.' if @loaded_fixtures.nil? - @loaded_fixtures.each do |table_name, fixtures| - Fixtures.instantiate_fixtures(self, table_name, fixtures, load_instances?) - end + def instantiate_fixtures + if pre_loaded_fixtures + raise RuntimeError, 'Load fixtures before instantiating them.' if Fixtures.all_loaded_fixtures.empty? + unless @@required_fixture_classes + self.class.require_fixture_classes Fixtures.all_loaded_fixtures.keys + @@required_fixture_classes = true + end + Fixtures.instantiate_all_loaded_fixtures(self, load_instances?) + else + raise RuntimeError, 'Load fixtures before instantiating them.' if @loaded_fixtures.nil? + @loaded_fixtures.each do |table_name, fixtures| + Fixtures.instantiate_fixtures(self, table_name, fixtures, load_instances?) end end + end - def load_instances? - use_instantiated_fixtures != :no_instances - end - end + def load_instances? + use_instantiated_fixtures != :no_instances + end end end diff --git a/activerecord/lib/active_record/test_case.rb b/activerecord/lib/active_record/test_case.rb index eabf06fc3b..588cf65156 100644 --- a/activerecord/lib/active_record/test_case.rb +++ b/activerecord/lib/active_record/test_case.rb @@ -1,7 +1,10 @@ require "active_support/test_case" +require "active_record/fixtures" module ActiveRecord class TestCase < ActiveSupport::TestCase #:nodoc: + include TestFixtures + self.fixture_path = FIXTURES_ROOT self.use_instantiated_fixtures = false self.use_transactional_fixtures = true diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index f7bdac8013..13988d5392 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -5,7 +5,6 @@ require 'config' require 'test/unit' require 'active_record' -require 'active_record/fixtures' require 'active_record/test_case' require 'connection' @@ -48,15 +47,11 @@ class << ActiveRecord::Base end unless ENV['FIXTURE_DEBUG'] - module Test #:nodoc: - module Unit #:nodoc: - class << TestCase #:nodoc: - def try_to_load_dependency_with_silence(*args) - ActiveRecord::Base.logger.silence { try_to_load_dependency_without_silence(*args)} - end - - alias_method_chain :try_to_load_dependency, :silence - end + module ActiveRecord::TestFixtures::ClassMethods + def try_to_load_dependency_with_silence(*args) + ActiveRecord::Base.logger.silence { try_to_load_dependency_without_silence(*args)} end + + alias_method_chain :try_to_load_dependency, :silence end -end \ No newline at end of file +end -- cgit v1.2.3 From 15c077492029dcba477aad2e29339803377cc1f8 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 7 Nov 2008 16:22:28 -0500 Subject: undef abstract methods instead of raising NotImplementedError. Still need the definitions for rdoc though. --- .../connection_adapters/abstract/database_statements.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb index 97c6cd4331..189c6c7b5a 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -31,13 +31,13 @@ module ActiveRecord # Returns an array of arrays containing the field values. # Order is the same as that returned by +columns+. def select_rows(sql, name = nil) - raise NotImplementedError, "select_rows is an abstract method" end + undef_method :select_rows # Executes the SQL statement in the context of this connection. - def execute(sql, name = nil) - raise NotImplementedError, "execute is an abstract method" + def execute(sql, name = nil, skip_logging = false) end + undef_method :execute # Returns the last auto-generated ID from the affected table. def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) @@ -163,8 +163,8 @@ module ActiveRecord # Returns an array of record hashes with the column names as keys and # column values as values. def select(sql, name = nil) - raise NotImplementedError, "select is an abstract method" end + undef_method :select # Returns the last auto-generated ID from the affected table. def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) -- cgit v1.2.3 From 1d803e51890e842f0c25ee3a016ed0311f2fa1b4 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 7 Nov 2008 16:22:56 -0500 Subject: Update AR tests --- activerecord/test/cases/fixtures_test.rb | 8 ++++---- activerecord/test/cases/validations_i18n_test.rb | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'activerecord') diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 6ba7597f56..ed2915b023 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -641,15 +641,15 @@ end class FixtureLoadingTest < ActiveRecord::TestCase uses_mocha 'reloading_fixtures_through_accessor_methods' do def test_logs_message_for_failed_dependency_load - Test::Unit::TestCase.expects(:require_dependency).with(:does_not_exist).raises(LoadError) + ActiveRecord::TestCase.expects(:require_dependency).with(:does_not_exist).raises(LoadError) ActiveRecord::Base.logger.expects(:warn) - Test::Unit::TestCase.try_to_load_dependency(:does_not_exist) + ActiveRecord::TestCase.try_to_load_dependency(:does_not_exist) end def test_does_not_logs_message_for_successful_dependency_load - Test::Unit::TestCase.expects(:require_dependency).with(:works_out_fine) + ActiveRecord::TestCase.expects(:require_dependency).with(:works_out_fine) ActiveRecord::Base.logger.expects(:warn).never - Test::Unit::TestCase.try_to_load_dependency(:works_out_fine) + ActiveRecord::TestCase.try_to_load_dependency(:works_out_fine) end end end diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index 42246f18b6..b2df98ca0a 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -2,7 +2,7 @@ require "cases/helper" require 'models/topic' require 'models/reply' -class ActiveRecordValidationsI18nTests < Test::Unit::TestCase +class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase def setup reset_callbacks Topic @topic = Topic.new -- cgit v1.2.3 From 529c2716992490a6eab55486788ca0d35c17e60b Mon Sep 17 00:00:00 2001 From: Nick Sieger Date: Sat, 8 Nov 2008 03:49:25 +0530 Subject: Simplify dispatcher callbacks to eliminate unnecessary stale thread purging. [Nick Sieger, Pratik Naik] Signed-off-by: Pratik Naik --- .../active_record/connection_adapters/abstract/connection_pool.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'activerecord') 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 54a17e20a9..cf760e334e 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -292,10 +292,7 @@ module ActiveRecord # and also returns connections to the pool cached by threads that are no # longer alive. def clear_active_connections! - @connection_pools.each_value do |pool| - pool.release_connection - pool.clear_stale_cached_connections! - end + @connection_pools.each_value {|pool| pool.release_connection } end # Clears the cache which maps classes -- cgit v1.2.3 From d20955f889223b6035dbc7d61acba9091bf7b7ed Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 8 Nov 2008 03:56:52 +0530 Subject: Don't leave open dangling connections in development mode. [#1335 state:resolved] --- activerecord/lib/active_record/connection_adapters/abstract_adapter.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index c5183357a1..f8fa969dc3 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -125,9 +125,8 @@ module ActiveRecord end # Returns true if its safe to reload the connection between requests for development mode. - # This is not the case for Ruby/MySQL and it's not necessary for any adapters except SQLite. def requires_reloading? - false + true end # Checks whether the connection to the database is still active (i.e. not stale). -- cgit v1.2.3 From a7f920f674d234f281d2491ebe6d74710a79e663 Mon Sep 17 00:00:00 2001 From: Ken Collins Date: Fri, 7 Nov 2008 20:39:06 -0600 Subject: If average value from DB is 0, make sure to convert it to a 0.0 float before calling #to_d on it [#1346 state:resolved] Signed-off-by: Joshua Peek --- activerecord/lib/active_record/calculations.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index 5e33cf1bd4..dd90580b3d 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.to_d + when 'avg' then value && value.to_f.to_d else type_cast_using_column(value, column) end end -- cgit v1.2.3 From dd77733f2fdb6dde2be7115fc31ad1dcbfccb5a1 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 8 Nov 2008 00:24:36 -0500 Subject: Timeout the connection pool monitor on ruby 1.8 only --- .../connection_adapters/abstract/connection_pool.rb | 14 +++++++++++--- activerecord/test/cases/pooled_connections_test.rb | 11 +++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) (limited to 'activerecord') 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 cf760e334e..901b17124c 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -65,15 +65,23 @@ module ActiveRecord # The default ConnectionPool maximum size is 5. def initialize(spec) @spec = spec + # The cache of reserved connections mapped to threads @reserved_connections = {} + # The mutex used to synchronize pool access @connection_mutex = Monitor.new @queue = @connection_mutex.new_cond - # default 5 second timeout - @timeout = spec.config[:wait_timeout] || 5 + + # default 5 second timeout unless on ruby 1.9 + @timeout = + if RUBY_VERSION < '1.9' + spec.config[:wait_timeout] || 5 + end + # default max pool size to 5 @size = (spec.config[:pool] && spec.config[:pool].to_i) || 5 + @connections = [] @checked_out = [] end @@ -187,7 +195,7 @@ module ActiveRecord # try looting dead threads clear_stale_cached_connections! if @size == @checked_out.size - raise ConnectionTimeoutError, "could not obtain a database connection within #{@timeout} seconds. The pool size is currently #{@size}, perhaps you need to increase it?" + raise ConnectionTimeoutError, "could not obtain a database connection#{" within #{@timeout} seconds" if @timeout}. The max pool size is currently #{@size}; consider increasing it." end end end diff --git a/activerecord/test/cases/pooled_connections_test.rb b/activerecord/test/cases/pooled_connections_test.rb index 36b45868b9..2a5e9509b3 100644 --- a/activerecord/test/cases/pooled_connections_test.rb +++ b/activerecord/test/cases/pooled_connections_test.rb @@ -28,10 +28,13 @@ class PooledConnectionsTest < ActiveRecord::TestCase end end - def test_pooled_connection_checkout - checkout_connections - assert_equal @connections.length, 2 - assert_equal @timed_out, 2 + # Will deadlock due to lack of Monitor timeouts in 1.9 + if RUBY_VERSION < '1.9' + def test_pooled_connection_checkout + checkout_connections + assert_equal @connections.length, 2 + assert_equal @timed_out, 2 + end end def checkout_checkin_connections(pool_size, threads) -- cgit v1.2.3 From 1df0a07f060291397dcbc8dd39e3abd3ad915a78 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 8 Nov 2008 22:49:00 -0500 Subject: lazy-initialize already loaded fixtures map --- activerecord/lib/active_record/fixtures.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 24aabf0359..a09f58fc23 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -832,7 +832,6 @@ module ActiveRecord self.use_instantiated_fixtures = true self.pre_loaded_fixtures = false - @@already_loaded_fixtures = {} self.fixture_class_names = {} end @@ -940,6 +939,7 @@ module ActiveRecord # Load fixtures for every test. else Fixtures.reset_cache + @@already_loaded_fixtures ||= {} @@already_loaded_fixtures[self.class] = nil load_fixtures end -- cgit v1.2.3 From 335a31524055c7dd79618ea79b3c18d827e25d3d Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 10 Nov 2008 14:16:43 -0600 Subject: Add simple case when DB calculations returns 0 instead of 0.0 [#1346 state:resolved] --- activerecord/lib/active_record/calculations.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index dd90580b3d..6f4e02b430 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.to_f.to_d + when 'avg' then value && (value == 0 ? 0.0.to_d : value.to_d) else type_cast_using_column(value, column) end end -- cgit v1.2.3 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 b17eb65d00242ae10ac9ed97ef22d88fdd710533 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 | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/test_case.rb b/activerecord/lib/active_record/test_case.rb index 588cf65156..02a12e4685 100644 --- a/activerecord/lib/active_record/test_case.rb +++ b/activerecord/lib/active_record/test_case.rb @@ -1,18 +1,8 @@ require "active_support/test_case" require "active_record/fixtures" -module ActiveRecord +module ActiveRecord class TestCase < ActiveSupport::TestCase #:nodoc: - include TestFixtures - - 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 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 1304b664924bfea54fd6dc0dc924ae3d126ff92d Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 13 Nov 2008 19:03:24 -0800 Subject: Remove superfluous require --- activerecord/lib/active_record/test_case.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/test_case.rb b/activerecord/lib/active_record/test_case.rb index 02a12e4685..d5f43f56e6 100644 --- a/activerecord/lib/active_record/test_case.rb +++ b/activerecord/lib/active_record/test_case.rb @@ -1,5 +1,4 @@ require "active_support/test_case" -require "active_record/fixtures" module ActiveRecord class TestCase < ActiveSupport::TestCase #:nodoc: -- 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 From 160b8a83444cd3784cba2396435ef06dba234122 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 15 Nov 2008 12:30:02 -0800 Subject: Set up fixtures for AR tests --- activerecord/test/cases/helper.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord') diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 13988d5392..02b39d7480 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -6,6 +6,7 @@ require 'test/unit' require 'active_record' require 'active_record/test_case' +require 'active_record/fixtures' require 'connection' # Show backtraces for deprecated behavior for quicker cleanup. @@ -55,3 +56,10 @@ unless ENV['FIXTURE_DEBUG'] alias_method_chain :try_to_load_dependency, :silence end end + +class ActiveSupport::TestCase + include ActiveRecord::TestFixtures + self.fixture_path = FIXTURES_ROOT + self.use_instantiated_fixtures = false + self.use_transactional_fixtures = true +end -- cgit v1.2.3 From 5fe543b6294df44d0e7670b78347564874a2f5e2 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 15 Nov 2008 12:31:54 -0800 Subject: Add create_fixtures method for tests --- activerecord/test/cases/helper.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activerecord') diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 02b39d7480..2382bfe4fe 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -62,4 +62,8 @@ class ActiveSupport::TestCase self.fixture_path = FIXTURES_ROOT self.use_instantiated_fixtures = false self.use_transactional_fixtures = true + + def create_fixtures(*table_names, &block) + Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names, {}, &block) + end end -- cgit v1.2.3 From ff594b2bc94ff2a942fe6ca05672387722dee686 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 16 Nov 2008 16:01:18 +0100 Subject: =?UTF-8?q?Added=20default=5Fscope=20to=20Base=20[#1381=20state:co?= =?UTF-8?q?mmitted]=20(Pawe=C5=82=20Kondzior)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- activerecord/CHANGELOG | 16 +++++++++++ activerecord/lib/active_record/base.rb | 10 +++++++ activerecord/test/cases/method_scoping_test.rb | 38 ++++++++++++++++++++++++++ activerecord/test/models/developer.rb | 12 ++++++++ 4 files changed, 76 insertions(+) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index c2299b56ad..84605b60ef 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,3 +1,19 @@ +*2.3.0/3.0* + +* Added default_scope to Base #1381 [Paweł Kondzior]. Example: + + class Person < ActiveRecord::Base + default_scope :order => 'last_name, first_name' + end + + class Company < ActiveRecord::Base + has_many :people + end + + Person.all # => Person.find(:all, :order => 'last_name, first_name') + Company.find(1).people # => Person.find(:all, :order => 'last_name, first_name', :conditions => { :company_id => 1 }) + + *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/base.rb b/activerecord/lib/active_record/base.rb index dcc8277849..9481c12b26 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2016,6 +2016,16 @@ module ActiveRecord #:nodoc: @@subclasses[self] + extra = @@subclasses[self].inject([]) {|list, subclass| list + subclass.subclasses } end + # Sets the default options for the model. The format of the + # method_scoping argument is the same as in with_scope. + # + # class Person << ActiveRecord::Base + # default_scope :find => { :order => 'last_name, first_name' } + # end + def default_scope(options = {}) + self.scoped_methods << { :find => options, :create => options.is_a?(Hash) ? options[:conditions] : {} } + end + # Test whether the given method and optional key are scoped. def scoped?(method, key = nil) #:nodoc: if current_scoped_methods && (scope = current_scoped_methods[method]) diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index ff10bfaf3e..79b24cd4fd 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -522,6 +522,44 @@ class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase end +class DefaultScopingTest < ActiveRecord::TestCase + fixtures :developers + + def test_default_scope + expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + assert_equal expected, received + end + + def test_method_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary } + assert_equal expected, received + end + + def test_nested_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.with_scope(:find => { :order => 'name DESC'}) do + DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + end + assert_equal expected, received + end + + def test_nested_exclusive_scope + expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.with_exclusive_scope(:find => { :limit => 100 }) do + DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + end + assert_equal expected, received + end + + def test_overwriting_default_scope + expected = Developer.find(:all, :order => 'salary').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary } + assert_equal expected, received + end +end + =begin # We disabled the scoping for has_one and belongs_to as we can't think of a proper use case diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index c08476f728..1844014011 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -77,3 +77,15 @@ class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base raise if projects.empty? end end + +class DeveloperOrderedBySalary < ActiveRecord::Base + self.table_name = 'developers' + default_scope :order => "salary DESC" + + def self.all_ordered_by_name + with_scope(:find => { :order => "name DESC" }) do + find(:all) + end + end + +end -- cgit v1.2.3 From ca23287b448c2e007a5c93e43e762a10e0007b7a Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 16 Nov 2008 16:35:52 +0100 Subject: =?UTF-8?q?Revert=20"Added=20default=5Fscope=20to=20Base=20[#1381?= =?UTF-8?q?=20state:committed]=20(Pawe=C5=82=20Kondzior)"=20--=20won't=20g?= =?UTF-8?q?el=20with=20threads.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit ff594b2bc94ff2a942fe6ca05672387722dee686. --- activerecord/CHANGELOG | 16 ----------- activerecord/lib/active_record/base.rb | 10 ------- activerecord/test/cases/method_scoping_test.rb | 38 -------------------------- activerecord/test/models/developer.rb | 12 -------- 4 files changed, 76 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 84605b60ef..c2299b56ad 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,19 +1,3 @@ -*2.3.0/3.0* - -* Added default_scope to Base #1381 [Paweł Kondzior]. Example: - - class Person < ActiveRecord::Base - default_scope :order => 'last_name, first_name' - end - - class Company < ActiveRecord::Base - has_many :people - end - - Person.all # => Person.find(:all, :order => 'last_name, first_name') - Company.find(1).people # => Person.find(:all, :order => 'last_name, first_name', :conditions => { :company_id => 1 }) - - *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/base.rb b/activerecord/lib/active_record/base.rb index 9481c12b26..dcc8277849 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2016,16 +2016,6 @@ module ActiveRecord #:nodoc: @@subclasses[self] + extra = @@subclasses[self].inject([]) {|list, subclass| list + subclass.subclasses } end - # Sets the default options for the model. The format of the - # method_scoping argument is the same as in with_scope. - # - # class Person << ActiveRecord::Base - # default_scope :find => { :order => 'last_name, first_name' } - # end - def default_scope(options = {}) - self.scoped_methods << { :find => options, :create => options.is_a?(Hash) ? options[:conditions] : {} } - end - # Test whether the given method and optional key are scoped. def scoped?(method, key = nil) #:nodoc: if current_scoped_methods && (scope = current_scoped_methods[method]) diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index 79b24cd4fd..ff10bfaf3e 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -522,44 +522,6 @@ class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase end -class DefaultScopingTest < ActiveRecord::TestCase - fixtures :developers - - def test_default_scope - expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary } - received = DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } - assert_equal expected, received - end - - def test_method_scope - expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } - received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary } - assert_equal expected, received - end - - def test_nested_scope - expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } - received = DeveloperOrderedBySalary.with_scope(:find => { :order => 'name DESC'}) do - DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } - end - assert_equal expected, received - end - - def test_nested_exclusive_scope - expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary } - received = DeveloperOrderedBySalary.with_exclusive_scope(:find => { :limit => 100 }) do - DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } - end - assert_equal expected, received - end - - def test_overwriting_default_scope - expected = Developer.find(:all, :order => 'salary').collect { |dev| dev.salary } - received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary } - assert_equal expected, received - end -end - =begin # We disabled the scoping for has_one and belongs_to as we can't think of a proper use case diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index 1844014011..c08476f728 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -77,15 +77,3 @@ class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base raise if projects.empty? end end - -class DeveloperOrderedBySalary < ActiveRecord::Base - self.table_name = 'developers' - default_scope :order => "salary DESC" - - def self.all_ordered_by_name - with_scope(:find => { :order => "name DESC" }) do - find(:all) - end - end - -end -- cgit v1.2.3 From d9f460a39b73fd2cf0f17f523cc4810d0bf44cac Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 16 Nov 2008 22:21:05 +0530 Subject: Ensure @@already_loaded_fixtures is initialized before use --- activerecord/lib/active_record/fixtures.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index a09f58fc23..9a82ff2ed4 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -925,6 +925,7 @@ module ActiveRecord end @fixture_cache = {} + @@already_loaded_fixtures ||= {} # Load fixtures once and begin transaction. if use_transactional_fixtures? @@ -939,7 +940,6 @@ module ActiveRecord # Load fixtures for every test. else Fixtures.reset_cache - @@already_loaded_fixtures ||= {} @@already_loaded_fixtures[self.class] = nil load_fixtures end -- cgit v1.2.3 From 2530d0eea8eaecd2c61f99225f050ff47973e9a0 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 16 Nov 2008 23:36:41 +0530 Subject: =?UTF-8?q?Added=20default=5Fscope=20to=20Base=20[#1381=20state:co?= =?UTF-8?q?mmitted]=20(Pawe=C5=82=20Kondzior)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- activerecord/CHANGELOG | 16 +++++++ activerecord/lib/active_record/base.rb | 16 ++++++- activerecord/test/cases/method_scoping_test.rb | 61 ++++++++++++++++++++++++++ activerecord/test/models/developer.rb | 12 +++++ 4 files changed, 104 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index c2299b56ad..c1d7297260 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,3 +1,19 @@ +*2.3.0/3.0* + +* Added default_scope to Base #1381 [Paweł Kondzior]. Example: + + class Person < ActiveRecord::Base + default_scope :order => 'last_name, first_name' + end + + class Company < ActiveRecord::Base + has_many :people + end + + Person.all # => Person.find(:all, :order => 'last_name, first_name') + Company.find(1).people # => Person.find(:all, :order => 'last_name, first_name', :conditions => { :company_id => 1 }) + + *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/base.rb b/activerecord/lib/active_record/base.rb index dcc8277849..d8b7a5a931 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -495,6 +495,10 @@ module ActiveRecord #:nodoc: superclass_delegating_accessor :store_full_sti_class self.store_full_sti_class = false + # Stores the default scope for the class + class_inheritable_accessor :default_scoping, :instance_writer => false + self.default_scoping = [] + class << self # Class methods # Find operates with four different retrieval approaches: # @@ -2016,6 +2020,16 @@ module ActiveRecord #:nodoc: @@subclasses[self] + extra = @@subclasses[self].inject([]) {|list, subclass| list + subclass.subclasses } end + # Sets the default options for the model. The format of the + # method_scoping argument is the same as in with_scope. + # + # class Person < ActiveRecord::Base + # default_scope :find => { :order => 'last_name, first_name' } + # end + def default_scope(options = {}) + self.default_scoping << { :find => options, :create => options.is_a?(Hash) ? options[:conditions] : {} } + end + # Test whether the given method and optional key are scoped. def scoped?(method, key = nil) #:nodoc: if current_scoped_methods && (scope = current_scoped_methods[method]) @@ -2031,7 +2045,7 @@ module ActiveRecord #:nodoc: end def scoped_methods #:nodoc: - Thread.current[:"#{self}_scoped_methods"] ||= [] + Thread.current[:"#{self}_scoped_methods"] ||= self.default_scoping end def current_scoped_methods #:nodoc: diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index ff10bfaf3e..4ac0018144 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -522,6 +522,67 @@ class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase end +class DefaultScopingTest < ActiveRecord::TestCase + fixtures :developers + + def test_default_scope + expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + assert_equal expected, received + end + + def test_default_scoping_with_threads + scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}] + + 2.times do + Thread.new { assert_equal scope, DeveloperOrderedBySalary.send(:scoped_methods) }.join + end + end + + def test_default_scoping_with_inheritance + scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}] + + # Inherit a class having a default scope and define a new default scope + klass = Class.new(DeveloperOrderedBySalary) + klass.send :default_scope, {} + + # Scopes added on children should append to parent scope + expected_klass_scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}, {:create=>nil, :find=>{}}] + assert_equal expected_klass_scope, klass.send(:scoped_methods) + + # Parent should still have the original scope + assert_equal scope, DeveloperOrderedBySalary.send(:scoped_methods) + end + + def test_method_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary } + assert_equal expected, received + end + + def test_nested_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.with_scope(:find => { :order => 'name DESC'}) do + DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + end + assert_equal expected, received + end + + def test_nested_exclusive_scope + expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.with_exclusive_scope(:find => { :limit => 100 }) do + DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + end + assert_equal expected, received + end + + def test_overwriting_default_scope + expected = Developer.find(:all, :order => 'salary').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary } + assert_equal expected, received + end +end + =begin # We disabled the scoping for has_one and belongs_to as we can't think of a proper use case diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index c08476f728..0c20f97502 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -77,3 +77,15 @@ class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base raise if projects.empty? end end + +class DeveloperOrderedBySalary < ActiveRecord::Base + self.table_name = 'developers' + default_scope :order => "salary DESC" + + def self.all_ordered_by_name + with_scope(:find => { :order => "name DESC" }) do + find(:all) + end + end + +end -- cgit v1.2.3 From e6c51051e4bbc1483ecc9e0837bb893197bbca83 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 16 Nov 2008 13:51:04 -0600 Subject: Ensure shared default_scoping stack is duped before assigning to thread local --- activerecord/lib/active_record/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index d8b7a5a931..68f44ef0f6 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2045,7 +2045,7 @@ module ActiveRecord #:nodoc: end def scoped_methods #:nodoc: - Thread.current[:"#{self}_scoped_methods"] ||= self.default_scoping + Thread.current[:"#{self}_scoped_methods"] ||= self.default_scoping.dup end def current_scoped_methods #:nodoc: -- cgit v1.2.3 From 32cb2345a54b9ab38461b2d4bb0dbd1706f2800e Mon Sep 17 00:00:00 2001 From: Tom Stuart Date: Mon, 17 Nov 2008 20:10:59 +0000 Subject: Fix default_scope to work in combination with named scopes Signed-off-by: David Heinemeier Hansson --- activerecord/lib/active_record/base.rb | 2 +- activerecord/test/cases/method_scoping_test.rb | 12 +++++++++--- activerecord/test/models/developer.rb | 14 +++++++------- 3 files changed, 17 insertions(+), 11 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 68f44ef0f6..cff5fd7a42 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2027,7 +2027,7 @@ module ActiveRecord #:nodoc: # default_scope :find => { :order => 'last_name, first_name' } # end def default_scope(options = {}) - self.default_scoping << { :find => options, :create => options.is_a?(Hash) ? options[:conditions] : {} } + self.default_scoping << { :find => options, :create => (options.is_a?(Hash) && options.has_key?(:conditions)) ? options[:conditions] : {} } end # Test whether the given method and optional key are scoped. diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index 4ac0018144..6372b4f6aa 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -532,7 +532,7 @@ class DefaultScopingTest < ActiveRecord::TestCase end def test_default_scoping_with_threads - scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}] + scope = [{ :create => {}, :find => { :order => 'salary DESC' } }] 2.times do Thread.new { assert_equal scope, DeveloperOrderedBySalary.send(:scoped_methods) }.join @@ -540,14 +540,14 @@ class DefaultScopingTest < ActiveRecord::TestCase end def test_default_scoping_with_inheritance - scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}] + scope = [{ :create => {}, :find => { :order => 'salary DESC' } }] # Inherit a class having a default scope and define a new default scope klass = Class.new(DeveloperOrderedBySalary) klass.send :default_scope, {} # Scopes added on children should append to parent scope - expected_klass_scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}, {:create=>nil, :find=>{}}] + expected_klass_scope = [{ :create => {}, :find => { :order => 'salary DESC' }}, { :create => {}, :find => {} }] assert_equal expected_klass_scope, klass.send(:scoped_methods) # Parent should still have the original scope @@ -568,6 +568,12 @@ class DefaultScopingTest < ActiveRecord::TestCase assert_equal expected, received end + def test_named_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.by_name.find(:all).collect { |dev| dev.salary } + assert_equal expected, received + end + def test_nested_exclusive_scope expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary } received = DeveloperOrderedBySalary.with_exclusive_scope(:find => { :limit => 100 }) do diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index 0c20f97502..92039a4f54 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -79,13 +79,13 @@ class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base end class DeveloperOrderedBySalary < ActiveRecord::Base - self.table_name = 'developers' - default_scope :order => "salary DESC" + self.table_name = 'developers' + default_scope :order => 'salary DESC' + named_scope :by_name, :order => 'name DESC' - def self.all_ordered_by_name - with_scope(:find => { :order => "name DESC" }) do - find(:all) - end + def self.all_ordered_by_name + with_scope(:find => { :order => 'name DESC' }) do + find(:all) end - + end end -- cgit v1.2.3 From 12118963acacc9c869bdd41ef8480a1a4e06d358 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Tue, 18 Nov 2008 09:59:46 +0100 Subject: use :en as a default locale (in favor of :en-US) Signed-off-by: David Heinemeier Hansson --- activerecord/lib/active_record.rb | 2 +- activerecord/lib/active_record/locale/en-US.yml | 54 ------------- activerecord/lib/active_record/locale/en.yml | 54 +++++++++++++ activerecord/test/cases/i18n_test.rb | 12 +-- activerecord/test/cases/validations_i18n_test.rb | 96 ++++++++++++------------ 5 files changed, 109 insertions(+), 109 deletions(-) delete mode 100644 activerecord/lib/active_record/locale/en-US.yml create mode 100644 activerecord/lib/active_record/locale/en.yml (limited to 'activerecord') diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 219cd30f94..7fdef17a45 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -78,4 +78,4 @@ require 'active_record/connection_adapters/abstract_adapter' require 'active_record/schema_dumper' require 'active_record/i18n_interpolation_deprecation' -I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en-US.yml' +I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml' diff --git a/activerecord/lib/active_record/locale/en-US.yml b/activerecord/lib/active_record/locale/en-US.yml deleted file mode 100644 index 421f0ebd60..0000000000 --- a/activerecord/lib/active_record/locale/en-US.yml +++ /dev/null @@ -1,54 +0,0 @@ -en-US: - activerecord: - errors: - # The values :model, :attribute and :value are always available for interpolation - # The value :count is available when applicable. Can be used for pluralization. - messages: - inclusion: "is not included in the list" - exclusion: "is reserved" - invalid: "is invalid" - confirmation: "doesn't match confirmation" - accepted: "must be accepted" - empty: "can't be empty" - blank: "can't be blank" - too_long: "is too long (maximum is {{count}} characters)" - too_short: "is too short (minimum is {{count}} characters)" - wrong_length: "is the wrong length (should be {{count}} characters)" - taken: "has already been taken" - not_a_number: "is not a number" - greater_than: "must be greater than {{count}}" - greater_than_or_equal_to: "must be greater than or equal to {{count}}" - equal_to: "must be equal to {{count}}" - less_than: "must be less than {{count}}" - less_than_or_equal_to: "must be less than or equal to {{count}}" - odd: "must be odd" - even: "must be even" - # Append your own errors here or at the model/attributes scope. - - # You can define own errors for models or model attributes. - # The values :model, :attribute and :value are always available for interpolation. - # - # For example, - # models: - # user: - # blank: "This is a custom blank message for {{model}}: {{attribute}}" - # attributes: - # login: - # blank: "This is a custom blank message for User login" - # Will define custom blank validation message for User model and - # custom blank validation message for login attribute of User model. - models: - - # Translate model names. Used in Model.human_name(). - #models: - # For example, - # user: "Dude" - # will translate User model name to "Dude" - - # Translate model attribute names. Used in Model.human_attribute_name(attribute). - #attributes: - # For example, - # user: - # login: "Handle" - # will translate User attribute "login" as "Handle" - diff --git a/activerecord/lib/active_record/locale/en.yml b/activerecord/lib/active_record/locale/en.yml new file mode 100644 index 0000000000..7e205435f7 --- /dev/null +++ b/activerecord/lib/active_record/locale/en.yml @@ -0,0 +1,54 @@ +en: + activerecord: + errors: + # The values :model, :attribute and :value are always available for interpolation + # The value :count is available when applicable. Can be used for pluralization. + messages: + inclusion: "is not included in the list" + exclusion: "is reserved" + invalid: "is invalid" + confirmation: "doesn't match confirmation" + accepted: "must be accepted" + empty: "can't be empty" + blank: "can't be blank" + too_long: "is too long (maximum is {{count}} characters)" + too_short: "is too short (minimum is {{count}} characters)" + wrong_length: "is the wrong length (should be {{count}} characters)" + taken: "has already been taken" + not_a_number: "is not a number" + greater_than: "must be greater than {{count}}" + greater_than_or_equal_to: "must be greater than or equal to {{count}}" + equal_to: "must be equal to {{count}}" + less_than: "must be less than {{count}}" + less_than_or_equal_to: "must be less than or equal to {{count}}" + odd: "must be odd" + even: "must be even" + # Append your own errors here or at the model/attributes scope. + + # You can define own errors for models or model attributes. + # The values :model, :attribute and :value are always available for interpolation. + # + # For example, + # models: + # user: + # blank: "This is a custom blank message for {{model}}: {{attribute}}" + # attributes: + # login: + # blank: "This is a custom blank message for User login" + # Will define custom blank validation message for User model and + # custom blank validation message for login attribute of User model. + models: + + # Translate model names. Used in Model.human_name(). + #models: + # For example, + # user: "Dude" + # will translate User model name to "Dude" + + # Translate model attribute names. Used in Model.human_attribute_name(attribute). + #attributes: + # For example, + # user: + # login: "Handle" + # will translate User attribute "login" as "Handle" + diff --git a/activerecord/test/cases/i18n_test.rb b/activerecord/test/cases/i18n_test.rb index ea06e377e3..b1db662eca 100644 --- a/activerecord/test/cases/i18n_test.rb +++ b/activerecord/test/cases/i18n_test.rb @@ -9,32 +9,32 @@ class ActiveRecordI18nTests < Test::Unit::TestCase end def test_translated_model_attributes - I18n.backend.store_translations 'en-US', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } } + I18n.backend.store_translations 'en', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } } assert_equal 'topic title attribute', Topic.human_attribute_name('title') end def test_translated_model_attributes_with_sti - I18n.backend.store_translations 'en-US', :activerecord => {:attributes => {:reply => {:title => 'reply title attribute'} } } + I18n.backend.store_translations 'en', :activerecord => {:attributes => {:reply => {:title => 'reply title attribute'} } } assert_equal 'reply title attribute', Reply.human_attribute_name('title') end def test_translated_model_attributes_with_sti_fallback - I18n.backend.store_translations 'en-US', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } } + I18n.backend.store_translations 'en', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } } assert_equal 'topic title attribute', Reply.human_attribute_name('title') end def test_translated_model_names - I18n.backend.store_translations 'en-US', :activerecord => {:models => {:topic => 'topic model'} } + I18n.backend.store_translations 'en', :activerecord => {:models => {:topic => 'topic model'} } assert_equal 'topic model', Topic.human_name end def test_translated_model_names_with_sti - I18n.backend.store_translations 'en-US', :activerecord => {:models => {:reply => 'reply model'} } + I18n.backend.store_translations 'en', :activerecord => {:models => {:reply => 'reply model'} } assert_equal 'reply model', Reply.human_name end def test_translated_model_names_with_sti_fallback - I18n.backend.store_translations 'en-US', :activerecord => {:models => {:topic => 'topic model'} } + I18n.backend.store_translations 'en', :activerecord => {:models => {:topic => 'topic model'} } assert_equal 'topic model', Reply.human_name end end diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index b2df98ca0a..f59e3f7001 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -9,7 +9,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase @old_load_path, @old_backend = I18n.load_path, I18n.backend I18n.load_path.clear I18n.backend = I18n::Backend::Simple.new - I18n.backend.store_translations('en-US', :activerecord => {:errors => {:messages => {:custom => nil}}}) + I18n.backend.store_translations('en', :activerecord => {:errors => {:messages => {:custom => nil}}}) end def teardown @@ -165,7 +165,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase def test_errors_full_messages_translates_human_attribute_name_for_model_attributes @topic.errors.instance_variable_set :@errors, { 'title' => ['empty'] } I18n.expects(:translate).with(:"topic.title", :default => ['Title'], :scope => [:activerecord, :attributes], :count => 1).returns('Title') - @topic.errors.full_messages :locale => 'en-US' + @topic.errors.full_messages :locale => 'en' end end @@ -429,8 +429,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_confirmation_of w/o mocha def test_validates_confirmation_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:confirmation => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:confirmation => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:confirmation => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:confirmation => 'global message'}}} Topic.validates_confirmation_of :title @topic.title_confirmation = 'foo' @@ -439,7 +439,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_confirmation_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:confirmation => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:confirmation => 'global message'}}} Topic.validates_confirmation_of :title @topic.title_confirmation = 'foo' @@ -450,8 +450,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_acceptance_of w/o mocha def test_validates_acceptance_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:accepted => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:accepted => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:accepted => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:accepted => 'global message'}}} Topic.validates_acceptance_of :title, :allow_nil => false @topic.valid? @@ -459,7 +459,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_acceptance_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:accepted => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:accepted => 'global message'}}} Topic.validates_acceptance_of :title, :allow_nil => false @topic.valid? @@ -469,8 +469,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_presence_of w/o mocha def test_validates_presence_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:blank => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:blank => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:blank => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:blank => 'global message'}}} Topic.validates_presence_of :title @topic.valid? @@ -478,7 +478,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_presence_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:blank => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:blank => 'global message'}}} Topic.validates_presence_of :title @topic.valid? @@ -488,8 +488,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_length_of :within w/o mocha def test_validates_length_of_within_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:too_short => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:too_short => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:too_short => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:too_short => 'global message'}}} Topic.validates_length_of :title, :within => 3..5 @topic.valid? @@ -497,7 +497,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_length_of_within_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:too_short => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:too_short => 'global message'}}} Topic.validates_length_of :title, :within => 3..5 @topic.valid? @@ -507,8 +507,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_length_of :is w/o mocha def test_validates_length_of_within_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 @topic.valid? @@ -516,7 +516,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_length_of_within_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 @topic.valid? @@ -526,8 +526,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_uniqueness_of w/o mocha def test_validates_length_of_within_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 @topic.valid? @@ -535,7 +535,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_length_of_within_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 @topic.valid? @@ -546,8 +546,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_format_of w/o mocha def test_validates_format_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:invalid => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:invalid => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/ @topic.valid? @@ -555,7 +555,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_format_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/ @topic.valid? @@ -565,8 +565,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_inclusion_of w/o mocha def test_validates_inclusion_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:inclusion => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:inclusion => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:inclusion => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:inclusion => 'global message'}}} Topic.validates_inclusion_of :title, :in => %w(a b c) @topic.valid? @@ -574,7 +574,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_inclusion_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:inclusion => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:inclusion => 'global message'}}} Topic.validates_inclusion_of :title, :in => %w(a b c) @topic.valid? @@ -584,8 +584,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_exclusion_of w/o mocha def test_validates_exclusion_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:exclusion => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:exclusion => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:exclusion => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:exclusion => 'global message'}}} Topic.validates_exclusion_of :title, :in => %w(a b c) @topic.title = 'a' @@ -594,7 +594,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_exclusion_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:exclusion => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:exclusion => 'global message'}}} Topic.validates_exclusion_of :title, :in => %w(a b c) @topic.title = 'a' @@ -605,8 +605,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_numericality_of without :only_integer w/o mocha def test_validates_numericality_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} Topic.validates_numericality_of :title @topic.title = 'a' @@ -615,7 +615,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_numericality_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true @topic.title = 'a' @@ -626,8 +626,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_numericality_of with :only_integer w/o mocha def test_validates_numericality_of_only_integer_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true @topic.title = 'a' @@ -636,7 +636,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_numericality_of_only_integer_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true @topic.title = 'a' @@ -647,8 +647,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_numericality_of :odd w/o mocha def test_validates_numericality_of_odd_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:odd => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:odd => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:odd => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:odd => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true, :odd => true @topic.title = 0 @@ -657,7 +657,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_numericality_of_odd_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:odd => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:odd => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true, :odd => true @topic.title = 0 @@ -668,8 +668,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_numericality_of :less_than w/o mocha def test_validates_numericality_of_less_than_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:less_than => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:less_than => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:less_than => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:less_than => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0 @topic.title = 1 @@ -678,7 +678,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_numericality_of_less_than_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:less_than => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:less_than => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0 @topic.title = 1 @@ -690,8 +690,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_associated w/o mocha def test_validates_associated_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:replies => {:invalid => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:replies => {:invalid => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} Topic.validates_associated :replies replied_topic.valid? @@ -699,7 +699,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_associated_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} Topic.validates_associated :replies replied_topic.valid? @@ -707,7 +707,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validations_with_message_symbol_must_translate - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom_error => "I am a custom error"}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:custom_error => "I am a custom error"}}} Topic.validates_presence_of :title, :message => :custom_error @topic.title = nil @topic.valid? @@ -715,7 +715,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_with_message_symbol_must_translate_per_attribute - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}}} Topic.validates_presence_of :title, :message => :custom_error @topic.title = nil @topic.valid? @@ -723,7 +723,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_with_message_symbol_must_translate_per_model - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:custom_error => "I am a custom error"}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:custom_error => "I am a custom error"}}}} Topic.validates_presence_of :title, :message => :custom_error @topic.title = nil @topic.valid? @@ -743,7 +743,7 @@ class ActiveRecordValidationsGenerateMessageI18nTests < Test::Unit::TestCase def setup reset_callbacks Topic @topic = Topic.new - I18n.backend.store_translations :'en-US', { + I18n.backend.store_translations :'en', { :activerecord => { :errors => { :messages => { -- cgit v1.2.3 From 51a19ae2bf33e66b23ff5c91bf584b2efa9e669f Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 19 Nov 2008 14:12:38 +0100 Subject: Assume that the next version is going to be 2.3 for now --- activerecord/Rakefile | 2 +- activerecord/lib/active_record/version.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord') diff --git a/activerecord/Rakefile b/activerecord/Rakefile index 49c51ecb0c..f47674d5b7 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.1' + PKG_BUILD) + s.add_dependency('activesupport', '= 2.3.0' + PKG_BUILD) s.files.delete FIXTURES_ROOT + "/fixture_database.sqlite" s.files.delete FIXTURES_ROOT + "/fixture_database_2.sqlite" diff --git a/activerecord/lib/active_record/version.rb b/activerecord/lib/active_record/version.rb index 3c5a9b7df8..6ac4bdc905 100644 --- a/activerecord/lib/active_record/version.rb +++ b/activerecord/lib/active_record/version.rb @@ -1,8 +1,8 @@ module ActiveRecord module VERSION #:nodoc: MAJOR = 2 - MINOR = 2 - TINY = 1 + MINOR = 3 + TINY = 0 STRING = [MAJOR, MINOR, TINY].join('.') end -- cgit v1.2.3 From 9c3c42d8ea489ff50fceebe0d37c86f9291fece5 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Wed, 19 Nov 2008 21:18:07 +0530 Subject: Remove reset! as a connection#checkout callback --- activerecord/lib/active_record/connection_adapters/abstract_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index f8fa969dc3..092d8f2268 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -31,7 +31,7 @@ module ActiveRecord include QueryCache include ActiveSupport::Callbacks define_callbacks :checkout, :checkin - checkout :reset! + @@row_even = true def initialize(connection, logger = nil) #:nodoc: -- cgit v1.2.3 From 8e4624be9e55cd0865b307be70a1e6287ec033ca Mon Sep 17 00:00:00 2001 From: Ken Collins Date: Wed, 19 Nov 2008 11:09:44 -0500 Subject: Remove SQL Server cases from tests for latest adapter work to pass rails expected behavior. Signed-off-by: Michael Koziarski --- activerecord/lib/active_record/test_case.rb | 6 +- activerecord/test/cases/adapter_test_sqlserver.rb | 95 ---------------------- activerecord/test/cases/associations/eager_test.rb | 2 +- activerecord/test/cases/base_test.rb | 13 ++- activerecord/test/cases/binary_test.rb | 6 +- activerecord/test/cases/defaults_test.rb | 2 +- activerecord/test/cases/inheritance_test.rb | 4 +- activerecord/test/cases/locking_test.rb | 4 +- activerecord/test/cases/migration_test.rb | 10 +-- .../test/cases/table_name_test_sqlserver.rb | 23 ------ .../test/schema/sqlserver_specific_schema.rb | 5 -- 11 files changed, 17 insertions(+), 153 deletions(-) delete mode 100644 activerecord/test/cases/adapter_test_sqlserver.rb delete mode 100644 activerecord/test/cases/table_name_test_sqlserver.rb delete mode 100644 activerecord/test/schema/sqlserver_specific_schema.rb (limited to 'activerecord') diff --git a/activerecord/lib/active_record/test_case.rb b/activerecord/lib/active_record/test_case.rb index d5f43f56e6..149b93203e 100644 --- a/activerecord/lib/active_record/test_case.rb +++ b/activerecord/lib/active_record/test_case.rb @@ -3,11 +3,9 @@ require "active_support/test_case" module ActiveRecord class TestCase < ActiveSupport::TestCase #:nodoc: def assert_date_from_db(expected, actual, message = nil) - # SQL Server doesn't have a separate column type just for dates, + # SybaseAdapter doesn't have a separate column type just for dates, # so the time is in the string and incorrectly formatted - if current_adapter?(:SQLServerAdapter) - assert_equal expected.strftime("%Y/%m/%d 00:00:00"), actual.strftime("%Y/%m/%d 00:00:00") - elsif current_adapter?(:SybaseAdapter) + if current_adapter?(:SybaseAdapter) assert_equal expected.to_s, actual.to_date.to_s, message else assert_equal expected.to_s, actual.to_s, message diff --git a/activerecord/test/cases/adapter_test_sqlserver.rb b/activerecord/test/cases/adapter_test_sqlserver.rb deleted file mode 100644 index ea270fb7ee..0000000000 --- a/activerecord/test/cases/adapter_test_sqlserver.rb +++ /dev/null @@ -1,95 +0,0 @@ -require "cases/helper" -require 'models/default' -require 'models/post' -require 'models/task' - -class SqlServerAdapterTest < ActiveRecord::TestCase - class TableWithRealColumn < ActiveRecord::Base; end - - fixtures :posts, :tasks - - def setup - @connection = ActiveRecord::Base.connection - end - - def teardown - @connection.execute("SET LANGUAGE us_english") rescue nil - end - - def test_real_column_has_float_type - assert_equal :float, TableWithRealColumn.columns_hash["real_number"].type - end - - # SQL Server 2000 has a bug where some unambiguous date formats are not - # correctly identified if the session language is set to german - def test_date_insertion_when_language_is_german - @connection.execute("SET LANGUAGE deutsch") - - assert_nothing_raised do - Task.create(:starting => Time.utc(2000, 1, 31, 5, 42, 0), :ending => Date.new(2006, 12, 31)) - end - end - - def test_indexes_with_descending_order - # Make sure we have an index with descending order - @connection.execute "CREATE INDEX idx_credit_limit ON accounts (credit_limit DESC)" rescue nil - assert_equal ["credit_limit"], @connection.indexes('accounts').first.columns - ensure - @connection.execute "DROP INDEX accounts.idx_credit_limit" - end - - def test_execute_without_block_closes_statement - assert_all_statements_used_are_closed do - @connection.execute("SELECT 1") - end - end - - def test_execute_with_block_closes_statement - assert_all_statements_used_are_closed do - @connection.execute("SELECT 1") do |sth| - assert !sth.finished?, "Statement should still be alive within block" - end - end - end - - def test_insert_with_identity_closes_statement - assert_all_statements_used_are_closed do - @connection.insert("INSERT INTO accounts ([id], [firm_id],[credit_limit]) values (999, 1, 50)") - end - end - - def test_insert_without_identity_closes_statement - assert_all_statements_used_are_closed do - @connection.insert("INSERT INTO accounts ([firm_id],[credit_limit]) values (1, 50)") - end - end - - def test_active_closes_statement - assert_all_statements_used_are_closed do - @connection.active? - end - end - - def assert_all_statements_used_are_closed(&block) - existing_handles = [] - ObjectSpace.each_object(DBI::StatementHandle) {|handle| existing_handles << handle} - GC.disable - - yield - - used_handles = [] - ObjectSpace.each_object(DBI::StatementHandle) {|handle| used_handles << handle unless existing_handles.include? handle} - - assert_block "No statements were used within given block" do - used_handles.size > 0 - end - - ObjectSpace.each_object(DBI::StatementHandle) do |handle| - assert_block "Statement should have been closed within given block" do - handle.finished? - end - end - ensure - GC.enable - end -end diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 5f43975d5a..a4f1f65f9a 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -667,7 +667,7 @@ class EagerAssociationTest < ActiveRecord::TestCase end def test_count_with_include - if current_adapter?(:SQLServerAdapter, :SybaseAdapter) + if current_adapter?(:SybaseAdapter) assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "len(comments.body) > 15") elsif current_adapter?(:OpenBaseAdapter) assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(FETCHBLOB(comments.body)) > 15") diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index da9f2742d8..5f54931d00 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -428,9 +428,6 @@ class BasicsTest < ActiveRecord::TestCase end def test_preserving_date_objects - # SQL Server doesn't have a separate column type just for dates, so all are returned as time - return true if current_adapter?(:SQLServerAdapter) - if current_adapter?(:SybaseAdapter, :OracleAdapter) # Sybase ctlib does not (yet?) support the date type; use datetime instead. # Oracle treats all dates/times as Time. @@ -777,8 +774,8 @@ class BasicsTest < ActiveRecord::TestCase end end - # Oracle, SQLServer, and Sybase do not have a TIME datatype. - unless current_adapter?(:SQLServerAdapter, :OracleAdapter, :SybaseAdapter) + # Oracle, and Sybase do not have a TIME datatype. + unless current_adapter?(:OracleAdapter, :SybaseAdapter) def test_utc_as_time_zone Topic.default_timezone = :utc attributes = { "bonus_time" => "5:42:00AM" } @@ -1157,8 +1154,8 @@ class BasicsTest < ActiveRecord::TestCase end def test_attributes_on_dummy_time - # Oracle, SQL Server, and Sybase do not have a TIME datatype. - return true if current_adapter?(:SQLServerAdapter, :OracleAdapter, :SybaseAdapter) + # Oracle, and Sybase do not have a TIME datatype. + return true if current_adapter?(:OracleAdapter, :SybaseAdapter) attributes = { "bonus_time" => "5:42:00AM" @@ -1874,7 +1871,7 @@ class BasicsTest < ActiveRecord::TestCase assert_equal "integer", xml.elements["//parent-id"].attributes['type'] assert_equal "true", xml.elements["//parent-id"].attributes['nil'] - if current_adapter?(:SybaseAdapter, :SQLServerAdapter, :OracleAdapter) + if current_adapter?(:SybaseAdapter, :OracleAdapter) assert_equal last_read_in_current_timezone, xml.elements["//last-read"].text assert_equal "datetime" , xml.elements["//last-read"].attributes['type'] else diff --git a/activerecord/test/cases/binary_test.rb b/activerecord/test/cases/binary_test.rb index 7131532c05..8545ba97cc 100644 --- a/activerecord/test/cases/binary_test.rb +++ b/activerecord/test/cases/binary_test.rb @@ -1,13 +1,9 @@ require "cases/helper" -# Without using prepared statements, it makes no sense to test -# BLOB data with SQL Server, because the length of a statement is -# limited to 8KB. -# # Without using prepared statements, it makes no sense to test # BLOB data with DB2 or Firebird, because the length of a statement # is limited to 32KB. -unless current_adapter?(:SQLServerAdapter, :SybaseAdapter, :DB2Adapter, :FirebirdAdapter) +unless current_adapter?(:SybaseAdapter, :DB2Adapter, :FirebirdAdapter) require 'models/binary' class BinaryTest < ActiveRecord::TestCase diff --git a/activerecord/test/cases/defaults_test.rb b/activerecord/test/cases/defaults_test.rb index ee84cb8af8..38e4853dc0 100644 --- a/activerecord/test/cases/defaults_test.rb +++ b/activerecord/test/cases/defaults_test.rb @@ -78,7 +78,7 @@ class DefaultTest < ActiveRecord::TestCase end end - if current_adapter?(:PostgreSQLAdapter, :SQLServerAdapter, :FirebirdAdapter, :OpenBaseAdapter, :OracleAdapter) + if current_adapter?(:PostgreSQLAdapter, :FirebirdAdapter, :OpenBaseAdapter, :OracleAdapter) def test_default_integers default = Default.new assert_instance_of Fixnum, default.positive_integer diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index a39415618d..3f59eb9706 100644 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -59,13 +59,13 @@ class InheritanceTest < ActiveRecord::TestCase def test_a_bad_type_column #SQLServer need to turn Identity Insert On before manually inserting into the Identity column - if current_adapter?(:SQLServerAdapter, :SybaseAdapter) + if current_adapter?(:SybaseAdapter) Company.connection.execute "SET IDENTITY_INSERT companies ON" end Company.connection.insert "INSERT INTO companies (id, #{QUOTED_TYPE}, name) VALUES(100, 'bad_class!', 'Not happening')" #We then need to turn it back Off before continuing. - if current_adapter?(:SQLServerAdapter, :SybaseAdapter) + if current_adapter?(:SybaseAdapter) Company.connection.execute "SET IDENTITY_INSERT companies OFF" end assert_raises(ActiveRecord::SubclassNotFound) { Company.find(100) } diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb index 0a14b1d906..077cac7747 100644 --- a/activerecord/test/cases/locking_test.rb +++ b/activerecord/test/cases/locking_test.rb @@ -200,9 +200,9 @@ end # blocks, so separate script called by Kernel#system is needed. # (See exec vs. async_exec in the PostgreSQL adapter.) -# TODO: The SQL Server, Sybase, and OpenBase adapters currently have no support for pessimistic locking +# TODO: The Sybase, and OpenBase adapters currently have no support for pessimistic locking -unless current_adapter?(:SQLServerAdapter, :SybaseAdapter, :OpenBaseAdapter) +unless current_adapter?(:SybaseAdapter, :OpenBaseAdapter) class PessimisticLockingTest < ActiveRecord::TestCase self.use_transactional_fixtures = false fixtures :people, :readers diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index ac44dd7ffe..2ec3d40332 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -271,9 +271,9 @@ if ActiveRecord::Base.connection.supports_migrations? Person.connection.drop_table table_name rescue nil end - # SQL Server, Sybase, and SQLite3 will not allow you to add a NOT NULL + # Sybase, and SQLite3 will not allow you to add a NOT NULL # column to a table without a default value. - unless current_adapter?(:SQLServerAdapter, :SybaseAdapter, :SQLiteAdapter) + unless current_adapter?(:SybaseAdapter, :SQLiteAdapter) def test_add_column_not_null_without_default Person.connection.create_table :testings do |t| t.column :foo, :string @@ -410,7 +410,7 @@ if ActiveRecord::Base.connection.supports_migrations? assert_equal Fixnum, bob.age.class assert_equal Time, bob.birthday.class - if current_adapter?(:SQLServerAdapter, :OracleAdapter, :SybaseAdapter) + if current_adapter?(:OracleAdapter, :SybaseAdapter) # Sybase, and Oracle don't differentiate between date/time assert_equal Time, bob.favorite_day.class else @@ -851,10 +851,6 @@ if ActiveRecord::Base.connection.supports_migrations? # - SQLite3 stores a float, in violation of SQL assert_kind_of BigDecimal, b.value_of_e assert_equal BigDecimal("2.71828182845905"), b.value_of_e - elsif current_adapter?(:SQLServer) - # - SQL Server rounds instead of truncating - assert_kind_of Fixnum, b.value_of_e - assert_equal 3, b.value_of_e else # - SQL standard is an integer assert_kind_of Fixnum, b.value_of_e diff --git a/activerecord/test/cases/table_name_test_sqlserver.rb b/activerecord/test/cases/table_name_test_sqlserver.rb deleted file mode 100644 index fbf38a130e..0000000000 --- a/activerecord/test/cases/table_name_test_sqlserver.rb +++ /dev/null @@ -1,23 +0,0 @@ -require "cases/helper" -require 'active_record/schema' - -if ActiveRecord::Base.connection.supports_migrations? - class Order < ActiveRecord::Base - self.table_name = '[order]' - end - - class TableNameTest < ActiveRecord::TestCase - self.use_transactional_fixtures = false - - # Ensures Model.columns works when using SQLServer escape characters. - # Enables legacy schemas using SQL reserved words as table names. - # Should work with table names with spaces as well ('table name'). - def test_escaped_table_name - assert_nothing_raised do - ActiveRecord::Base.connection.select_all 'SELECT * FROM [order]' - end - assert_equal '[order]', Order.table_name - assert_equal 5, Order.columns.length - end - end -end diff --git a/activerecord/test/schema/sqlserver_specific_schema.rb b/activerecord/test/schema/sqlserver_specific_schema.rb deleted file mode 100644 index cd8aca2fe5..0000000000 --- a/activerecord/test/schema/sqlserver_specific_schema.rb +++ /dev/null @@ -1,5 +0,0 @@ -ActiveRecord::Schema.define do - create_table :table_with_real_columns, :force => true do |t| - t.column :real_number, :real - end -end \ No newline at end of file -- cgit v1.2.3 From db6b3a1f2c785fe3e32a96c386de24366c0bf0a1 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 23 Nov 2008 13:43:59 +0100 Subject: Docfix [#1444 state:committed] --- activerecord/lib/active_record/base.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index cff5fd7a42..6692235a78 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2322,7 +2322,7 @@ module ActiveRecord #:nodoc: # construct an URI with the user object's 'id' in it: # # user = User.find_by_name('Phusion') - # user_path(path) # => "/users/1" + # user_path(user) # => "/users/1" # # You can override +to_param+ in your model to make +users_path+ construct # an URI using the user's name instead of the user's id: @@ -2334,7 +2334,7 @@ module ActiveRecord #:nodoc: # end # # user = User.find_by_name('Phusion') - # user_path(path) # => "/users/Phusion" + # user_path(user) # => "/users/Phusion" def to_param # We can't use alias_method here, because method 'id' optimizes itself on the fly. (id = self.id) ? id.to_s : nil # Be sure to stringify the id for routes -- cgit v1.2.3 From 7254d23764f7abe8023f3daeb07d99ea1c8e777a Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 24 Nov 2008 11:14:24 -0600 Subject: Autoload ActiveRecord files --- activerecord/lib/active_record.rb | 78 ++++++++++------------ activerecord/lib/active_record/associations.rb | 22 +++--- activerecord/lib/active_record/base.rb | 11 +++ .../connection_adapters/abstract_adapter.rb | 1 + activerecord/lib/active_record/serialization.rb | 2 +- activerecord/test/cases/ar_schema_test.rb | 1 - activerecord/test/cases/schema_dumper_test.rb | 1 - 7 files changed, 61 insertions(+), 55 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 7fdef17a45..584349659e 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -31,51 +31,45 @@ rescue LoadError end end -require 'active_record/base' -require 'active_record/named_scope' -require 'active_record/observer' -require 'active_record/query_cache' -require 'active_record/validations' -require 'active_record/callbacks' -require 'active_record/reflection' -require 'active_record/associations' -require 'active_record/association_preload' -require 'active_record/aggregations' -require 'active_record/transactions' -require 'active_record/timestamp' -require 'active_record/locking/optimistic' -require 'active_record/locking/pessimistic' -require 'active_record/migration' -require 'active_record/schema' -require 'active_record/calculations' -require 'active_record/serialization' -require 'active_record/attribute_methods' -require 'active_record/dirty' -require 'active_record/dynamic_finder_match' +module ActiveRecord + # TODO: Review explicit loads to see if they will automatically be handled by the initilizer. + def self.load_all! + [Base, DynamicFinderMatch, ConnectionAdapters::AbstractAdapter] + end -ActiveRecord::Base.class_eval do - extend ActiveRecord::QueryCache - include ActiveRecord::Validations - include ActiveRecord::Locking::Optimistic - include ActiveRecord::Locking::Pessimistic - include ActiveRecord::AttributeMethods - include ActiveRecord::Dirty - include ActiveRecord::Callbacks - include ActiveRecord::Observing - include ActiveRecord::Timestamp - include ActiveRecord::Associations - include ActiveRecord::NamedScope - include ActiveRecord::AssociationPreload - include ActiveRecord::Aggregations - include ActiveRecord::Transactions - include ActiveRecord::Reflection - include ActiveRecord::Calculations - include ActiveRecord::Serialization -end + autoload :Aggregations, 'active_record/aggregations' + autoload :AssociationPreload, 'active_record/association_preload' + autoload :Associations, 'active_record/associations' + autoload :AttributeMethods, 'active_record/attribute_methods' + autoload :Base, 'active_record/base' + autoload :Calculations, 'active_record/calculations' + autoload :Callbacks, 'active_record/callbacks' + autoload :Dirty, 'active_record/dirty' + autoload :DynamicFinderMatch, 'active_record/dynamic_finder_match' + autoload :Migration, 'active_record/migration' + autoload :NamedScope, 'active_record/named_scope' + autoload :Observing, 'active_record/observer' + autoload :QueryCache, 'active_record/query_cache' + autoload :Reflection, 'active_record/reflection' + autoload :Schema, 'active_record/schema' + autoload :SchemaDumper, 'active_record/schema_dumper' + autoload :Serialization, 'active_record/serialization' + autoload :TestCase, 'active_record/test_case' + autoload :Timestamp, 'active_record/timestamp' + autoload :Transactions, 'active_record/transactions' + autoload :Validations, 'active_record/validations' -require 'active_record/connection_adapters/abstract_adapter' + module Locking + autoload :Optimistic, 'active_record/locking/optimistic' + autoload :Pessimistic, 'active_record/locking/pessimistic' + end -require 'active_record/schema_dumper' + module ConnectionAdapters + autoload :AbstractAdapter, 'active_record/connection_adapters/abstract_adapter' + end +end require 'active_record/i18n_interpolation_deprecation' I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml' + +ActiveRecord.load_all! diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 7f7819115c..63e28a43ab 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1,13 +1,3 @@ -require 'active_record/associations/association_proxy' -require 'active_record/associations/association_collection' -require 'active_record/associations/belongs_to_association' -require 'active_record/associations/belongs_to_polymorphic_association' -require 'active_record/associations/has_one_association' -require 'active_record/associations/has_many_association' -require 'active_record/associations/has_many_through_association' -require 'active_record/associations/has_and_belongs_to_many_association' -require 'active_record/associations/has_one_through_association' - module ActiveRecord class HasManyThroughAssociationNotFoundError < ActiveRecordError #:nodoc: def initialize(owner_class_name, reflection) @@ -75,6 +65,18 @@ module ActiveRecord # See ActiveRecord::Associations::ClassMethods for documentation. module Associations # :nodoc: + # These classes will be loaded when associatoins are created. + # So there is no need to eager load them. + autoload :AssociationCollection, 'active_record/associations/association_collection' + autoload :AssociationProxy, 'active_record/associations/association_proxy' + autoload :BelongsToAssociation, 'active_record/associations/belongs_to_association' + autoload :BelongsToPolymorphicAssociation, 'active_record/associations/belongs_to_polymorphic_association' + autoload :HasAndBelongsToManyAssociation, 'active_record/associations/has_and_belongs_to_many_association' + autoload :HasManyAssociation, 'active_record/associations/has_many_association' + autoload :HasManyThroughAssociation, 'active_record/associations/has_many_through_association' + autoload :HasOneAssociation, 'active_record/associations/has_one_association' + autoload :HasOneThroughAssociation, 'active_record/associations/has_one_through_association' + def self.included(base) base.extend(ClassMethods) end diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 6692235a78..1e7cac8371 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2978,4 +2978,15 @@ module ActiveRecord #:nodoc: value end end + + Base.class_eval do + extend QueryCache + include Validations + include Locking::Optimistic, Locking::Pessimistic + include AttributeMethods + include Dirty + include Callbacks, Observing, Timestamp + include Associations, AssociationPreload, NamedScope + include Aggregations, Transactions, Reflection, Calculations, Serialization + end end diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 092d8f2268..cab77fc031 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -3,6 +3,7 @@ require 'date' require 'bigdecimal' require 'bigdecimal/util' +# TODO: Autoload these files require 'active_record/connection_adapters/abstract/schema_definitions' require 'active_record/connection_adapters/abstract/schema_statements' require 'active_record/connection_adapters/abstract/database_statements' diff --git a/activerecord/lib/active_record/serialization.rb b/activerecord/lib/active_record/serialization.rb index 332cda1e16..cef067de5f 100644 --- a/activerecord/lib/active_record/serialization.rb +++ b/activerecord/lib/active_record/serialization.rb @@ -95,4 +95,4 @@ module ActiveRecord #:nodoc: end require 'active_record/serializers/xml_serializer' -require 'active_record/serializers/json_serializer' \ No newline at end of file +require 'active_record/serializers/json_serializer' diff --git a/activerecord/test/cases/ar_schema_test.rb b/activerecord/test/cases/ar_schema_test.rb index 431dc7a141..4c1589d965 100644 --- a/activerecord/test/cases/ar_schema_test.rb +++ b/activerecord/test/cases/ar_schema_test.rb @@ -1,5 +1,4 @@ require "cases/helper" -require 'active_record/schema' if ActiveRecord::Base.connection.supports_migrations? diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index ee7e285a73..17e4c755ce 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -1,5 +1,4 @@ require "cases/helper" -require 'active_record/schema_dumper' require 'stringio' -- cgit v1.2.3 From 703fecb4fc81c3a975a53c9c4534f40193bd1ab9 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 24 Nov 2008 11:37:57 -0600 Subject: Add LAZY env flag for testing autoload/lazy load feature --- activerecord/lib/active_record.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 584349659e..612e2313ae 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -72,4 +72,4 @@ end require 'active_record/i18n_interpolation_deprecation' I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml' -ActiveRecord.load_all! +ActiveRecord.load_all! unless ENV['LAZY'] -- cgit v1.2.3 From fffb1da3f22852c722dbc4436ec7c924435d29a5 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 24 Nov 2008 11:52:29 -0600 Subject: require json lib when serialization is loaded --- activerecord/lib/active_record/serialization.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/serialization.rb b/activerecord/lib/active_record/serialization.rb index cef067de5f..870b4b2dd4 100644 --- a/activerecord/lib/active_record/serialization.rb +++ b/activerecord/lib/active_record/serialization.rb @@ -1,3 +1,5 @@ +require 'active_support/json' + module ActiveRecord #:nodoc: module Serialization class Serializer #:nodoc: -- cgit v1.2.3 From 21901e9345daff5abb3acb0af108f5f2134bee32 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 24 Nov 2008 12:02:00 -0600 Subject: fixtures depends on dependencies --- activerecord/lib/active_record/fixtures.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 9a82ff2ed4..aefb8d4a10 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -1,6 +1,7 @@ require 'erb' require 'yaml' require 'csv' +require 'active_support/dependencies' require 'active_support/test_case' if RUBY_VERSION < '1.9' -- cgit v1.2.3 From d6b923adbdfc9a4df20132f741bbfb43db12113c Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 24 Nov 2008 12:09:49 -0600 Subject: get activerecord tests passing with lazy loading --- activerecord/lib/active_record.rb | 3 +++ activerecord/lib/active_record/base.rb | 3 +++ 2 files changed, 6 insertions(+) (limited to 'activerecord') diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 612e2313ae..3293206d43 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -37,6 +37,9 @@ module ActiveRecord [Base, DynamicFinderMatch, ConnectionAdapters::AbstractAdapter] end + autoload :ActiveRecordError, 'active_record/base' + autoload :ConnectionNotEstablished, 'active_record/base' + autoload :Aggregations, 'active_record/aggregations' autoload :AssociationPreload, 'active_record/association_preload' autoload :Associations, 'active_record/associations' diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 1e7cac8371..9e4514375f 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2990,3 +2990,6 @@ module ActiveRecord #:nodoc: include Aggregations, Transactions, Reflection, Calculations, Serialization end end + +# TODO: Remove this and make it work with LAZY flag +require 'active_record/connection_adapters/abstract_adapter' -- cgit v1.2.3 From 835be0cbedd56bb4af71f1104d311d0e425e7abf Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 24 Nov 2008 12:30:04 -0600 Subject: missed ActiveRecord::Migrator --- activerecord/lib/active_record.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord') diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 3293206d43..55f06ed832 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -50,6 +50,7 @@ module ActiveRecord autoload :Dirty, 'active_record/dirty' autoload :DynamicFinderMatch, 'active_record/dynamic_finder_match' autoload :Migration, 'active_record/migration' + autoload :Migrator, 'active_record/migration' autoload :NamedScope, 'active_record/named_scope' autoload :Observing, 'active_record/observer' autoload :QueryCache, 'active_record/query_cache' -- cgit v1.2.3 From 104f3a57768602289299b3be0fab5b1ed21d7653 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 24 Nov 2008 18:43:04 -0800 Subject: Add config.preload_frameworks to load all frameworks at startup. Default to false so Rails autoloads itself as it's used. --- activerecord/lib/active_record.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 55f06ed832..348e5b94af 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -75,5 +75,3 @@ end require 'active_record/i18n_interpolation_deprecation' I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml' - -ActiveRecord.load_all! unless ENV['LAZY'] -- cgit v1.2.3 From ad93212f79e98535aa504dfdf6175e8035616abe Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 25 Nov 2008 23:50:57 -0800 Subject: Rename use_transactional_fixtures? so it doesn't collide with the superclass_delegating_accessor's query method --- activerecord/lib/active_record/fixtures.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index aefb8d4a10..129306d335 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -913,7 +913,7 @@ module ActiveRecord end end - def use_transactional_fixtures? + def run_in_transaction? use_transactional_fixtures && !self.class.uses_transaction?(method_name) end @@ -929,7 +929,7 @@ module ActiveRecord @@already_loaded_fixtures ||= {} # Load fixtures once and begin transaction. - if use_transactional_fixtures? + if run_in_transaction? if @@already_loaded_fixtures[self.class] @loaded_fixtures = @@already_loaded_fixtures[self.class] else @@ -952,12 +952,12 @@ module ActiveRecord def teardown_fixtures return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank? - unless use_transactional_fixtures? + unless run_in_transaction? Fixtures.reset_cache end # Rollback changes if a transaction is active. - if use_transactional_fixtures? && ActiveRecord::Base.connection.open_transactions != 0 + if run_in_transaction? && ActiveRecord::Base.connection.open_transactions != 0 ActiveRecord::Base.connection.rollback_db_transaction ActiveRecord::Base.connection.decrement_open_transactions end -- cgit v1.2.3 From 9a4d557713acb0fc8e80f61af18094034aca029a Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 26 Nov 2008 15:21:12 +0100 Subject: Ensure hash conditions on referenced tables are considered when eager loading with limit/offset. [#1404 state:resolved] Signed-off-by: Pratik Naik --- activerecord/lib/active_record/associations.rb | 1 + activerecord/test/cases/associations/eager_test.rb | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 63e28a43ab..0546b76c63 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1733,6 +1733,7 @@ module ActiveRecord case cond when nil then all when Array then all << cond.first + when Hash then all << cond.keys else all << cond end end diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index a4f1f65f9a..3c8408d14b 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -385,12 +385,28 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal count, posts.size end - def test_eager_with_has_many_and_limit_ond_high_offset + def test_eager_with_has_many_and_limit_and_high_offset posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ]) assert_equal 0, posts.size end - def test_count_eager_with_has_many_and_limit_ond_high_offset + def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_array_conditions + assert_queries(1) do + posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, + :conditions => [ "authors.name = ? and comments.body = ?", 'David', 'go crazy' ]) + assert_equal 0, posts.size + end + end + + def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_hash_conditions + assert_queries(1) do + posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, + :conditions => { 'authors.name' => 'David', 'comments.body' => 'go crazy' }) + assert_equal 0, posts.size + end + end + + def test_count_eager_with_has_many_and_limit_and_high_offset posts = Post.count(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ]) assert_equal 0, posts end -- cgit v1.2.3 From 0c4ba90aa1ea6a8d386c724a55a31e63a13c46ab Mon Sep 17 00:00:00 2001 From: Foliosus Date: Tue, 18 Nov 2008 09:32:48 -0800 Subject: Removed extra 'as' in :joins clause for habtm preloading Signed-off-by: Michael Koziarski [#1405 state:committed] --- 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 69300e5ce5..99c3ce5e62 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -185,7 +185,7 @@ module ActiveRecord associated_records = reflection.klass.find(:all, :conditions => [conditions, ids], :include => options[:include], - :joins => "INNER JOIN #{connection.quote_table_name options[:join_table]} as t0 ON #{reflection.klass.quoted_table_name}.#{reflection.klass.primary_key} = t0.#{reflection.association_foreign_key}", + :joins => "INNER JOIN #{connection.quote_table_name options[:join_table]} t0 ON #{reflection.klass.quoted_table_name}.#{reflection.klass.primary_key} = t0.#{reflection.association_foreign_key}", :select => "#{options[:select] || table_name+'.*'}, t0.#{reflection.primary_key_name} as the_parent_record_id", :order => options[:order]) -- cgit v1.2.3 From 97403ad5fdfcdfb2110c6f8fd0ebf43b7afc4859 Mon Sep 17 00:00:00 2001 From: miloops Date: Fri, 21 Nov 2008 19:20:33 -0300 Subject: Add :having option to find, to use in combination with grouped finds. Also added to has_many and has_and_belongs_to_many associations. Signed-off-by: Michael Koziarski [#1028 state:committed] --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/associations.rb | 12 ++++++++---- .../lib/active_record/associations/association_proxy.rb | 1 + activerecord/lib/active_record/base.rb | 9 ++++++--- .../has_and_belongs_to_many_associations_test.rb | 5 +++++ .../test/cases/associations/has_many_associations_test.rb | 5 +++++ activerecord/test/cases/finder_test.rb | 7 +++++++ activerecord/test/models/author.rb | 1 + activerecord/test/models/category.rb | 1 + activerecord/test/models/project.rb | 1 + 10 files changed, 37 insertions(+), 7 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index c1d7297260..cca70f1fb7 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *2.3.0/3.0* +* Add :having as a key to find and the relevant associations. [miloops] + * Added default_scope to Base #1381 [Paweł Kondzior]. Example: class Person < ActiveRecord::Base diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 0546b76c63..3fbbea43ed 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -724,6 +724,8 @@ module ActiveRecord # Specify second-order associations that should be eager loaded when the collection is loaded. # [:group] # An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause. + # [:having] + # Combined with +:group+ this can be used to filter the records that a GROUP BY returns. Uses the HAVING SQL-clause. # [:limit] # An integer determining the limit on the number of rows that should be returned. # [:offset] @@ -1181,6 +1183,8 @@ module ActiveRecord # Specify second-order associations that should be eager loaded when the collection is loaded. # [:group] # An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause. + # [:having] + # Combined with +:group+ this can be used to filter the records that a GROUP BY returns. Uses the HAVING SQL-clause. # [:limit] # An integer determining the limit on the number of rows that should be returned. # [:offset] @@ -1553,7 +1557,7 @@ module ActiveRecord @@valid_keys_for_has_many_association = [ :class_name, :table_name, :foreign_key, :primary_key, :dependent, - :select, :conditions, :include, :order, :group, :limit, :offset, + :select, :conditions, :include, :order, :group, :having, :limit, :offset, :as, :through, :source, :source_type, :uniq, :finder_sql, :counter_sql, @@ -1609,7 +1613,7 @@ module ActiveRecord mattr_accessor :valid_keys_for_has_and_belongs_to_many_association @@valid_keys_for_has_and_belongs_to_many_association = [ :class_name, :table_name, :join_table, :foreign_key, :association_foreign_key, - :select, :conditions, :include, :order, :group, :limit, :offset, + :select, :conditions, :include, :order, :group, :having, :limit, :offset, :uniq, :finder_sql, :counter_sql, :delete_sql, :insert_sql, :before_add, :after_add, :before_remove, :after_remove, @@ -1658,7 +1662,7 @@ module ActiveRecord add_conditions!(sql, options[:conditions], scope) add_limited_ids_condition!(sql, options, join_dependency) if !using_limitable_reflections?(join_dependency.reflections) && ((scope && scope[:limit]) || options[:limit]) - add_group!(sql, options[:group], scope) + add_group!(sql, options[:group], options[:having], scope) add_order!(sql, options[:order], scope) add_limit!(sql, options, scope) if using_limitable_reflections?(join_dependency.reflections) add_lock!(sql, options, scope) @@ -1714,7 +1718,7 @@ module ActiveRecord end add_conditions!(sql, options[:conditions], scope) - add_group!(sql, options[:group], scope) + add_group!(sql, options[:group], options[:having], scope) if order && is_distinct connection.add_order_by_for_association_limiting!(sql, :order => order) diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index d1a79df6e6..75ec4fbb2e 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -188,6 +188,7 @@ module ActiveRecord def merge_options_from_reflection!(options) options.reverse_merge!( :group => @reflection.options[:group], + :having => @reflection.options[:having], :limit => @reflection.options[:limit], :offset => @reflection.options[:offset], :joins => @reflection.options[:joins], diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 9e4514375f..8f8ed241d5 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -521,6 +521,7 @@ module ActiveRecord #:nodoc: # * :conditions - An SQL fragment like "administrator = 1", [ "user_name = ?", username ], or ["user_name = :user_name", { :user_name => user_name }]. See conditions in the intro. # * :order - An SQL fragment like "created_at DESC, name". # * :group - An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause. + # * :having - Combined with +:group+ this can be used to filter the records that a GROUP BY returns. Uses the HAVING SQL-clause. # * :limit - An integer determining the limit on the number of rows that should be returned. # * :offset - An integer determining the offset from where the rows should be fetched. So at 5, it would skip rows 0 through 4. # * :joins - Either an SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id" (rarely needed) @@ -1632,7 +1633,7 @@ module ActiveRecord #:nodoc: add_joins!(sql, options[:joins], scope) add_conditions!(sql, options[:conditions], scope) - add_group!(sql, options[:group], scope) + add_group!(sql, options[:group], options[:having], scope) add_order!(sql, options[:order], scope) add_limit!(sql, options, scope) add_lock!(sql, options, scope) @@ -1688,13 +1689,15 @@ module ActiveRecord #:nodoc: end end - def add_group!(sql, group, scope = :auto) + def add_group!(sql, group, having, scope = :auto) if group sql << " GROUP BY #{group}" + sql << " HAVING #{having}" if having else scope = scope(:find) if :auto == scope if scope && (scoped_group = scope[:group]) sql << " GROUP BY #{scoped_group}" + sql << " HAVING #{scoped_having}" if (scoped_having = scope[:having]) end end end @@ -2259,7 +2262,7 @@ module ActiveRecord #:nodoc: end VALID_FIND_OPTIONS = [ :conditions, :include, :joins, :limit, :offset, - :order, :select, :readonly, :group, :from, :lock ] + :order, :select, :readonly, :group, :having, :from, :lock ] def validate_find_options(options) #:nodoc: options.assert_valid_keys(VALID_FIND_OPTIONS) diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index b5bedf3704..2f08e09d43 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -658,6 +658,11 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal 1, categories(:technology).posts_gruoped_by_title.size end + def test_find_scoped_grouped_having + assert_equal 2, projects(:active_record).well_payed_salary_groups.size + assert projects(:active_record).well_payed_salary_groups.all? { |g| g.salary > 10000 } + end + def test_get_ids assert_equal projects(:active_record, :action_controller).map(&:id).sort, developers(:david).project_ids.sort assert_equal [projects(:active_record).id], developers(:jamis).project_ids diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 59784e1bcb..816ceb6855 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -255,6 +255,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 2, companies(:first_firm).clients_grouped_by_name.length end + def test_find_scoped_grouped_having + assert_equal 1, authors(:david).popular_grouped_posts.length + assert_equal 0, authors(:mary).popular_grouped_posts.length + end + def test_adding force_signal37_to_load_all_clients_of_firm natural = Client.new("name" => "Natural Company") diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 153880afbd..d4d770b04e 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -175,6 +175,13 @@ class FinderTest < ActiveRecord::TestCase assert_equal 4, developers.map(&:salary).uniq.size end + def test_find_with_group_and_having + developers = Developer.find(:all, :group => "salary", :having => "sum(salary) > 10000", :select => "salary") + assert_equal 3, developers.size + assert_equal 3, developers.map(&:salary).uniq.size + assert developers.all? { |developer| developer.salary > 10000 } + end + def test_find_with_entire_select_statement topics = Topic.find_by_sql "SELECT * FROM topics WHERE author_name = 'Mary'" diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index e5b19ff9e4..4ffac4fe32 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -1,6 +1,7 @@ class Author < ActiveRecord::Base has_many :posts has_many :posts_with_comments, :include => :comments, :class_name => "Post" + has_many :popular_grouped_posts, :include => :comments, :class_name => "Post", :group => "type", :having => "SUM(comments_count) > 1", :select => "type" has_many :posts_with_comments_sorted_by_comment_id, :include => :comments, :class_name => "Post", :order => 'comments.id' has_many :posts_sorted_by_id_limited, :class_name => "Post", :order => 'posts.id', :limit => 1 has_many :posts_with_categories, :include => :categories, :class_name => "Post" diff --git a/activerecord/test/models/category.rb b/activerecord/test/models/category.rb index 4e9d247a4e..5efce6aaa6 100644 --- a/activerecord/test/models/category.rb +++ b/activerecord/test/models/category.rb @@ -14,6 +14,7 @@ class Category < ActiveRecord::Base :class_name => 'Post', :conditions => { :title => 'Yet Another Testing Title' } + has_and_belongs_to_many :popular_grouped_posts, :class_name => "Post", :group => "posts.type", :having => "sum(comments.post_id) > 2", :include => :comments has_and_belongs_to_many :posts_gruoped_by_title, :class_name => "Post", :group => "title", :select => "title" def self.what_are_you diff --git a/activerecord/test/models/project.rb b/activerecord/test/models/project.rb index 44c692b5e7..550d4ae23c 100644 --- a/activerecord/test/models/project.rb +++ b/activerecord/test/models/project.rb @@ -13,6 +13,7 @@ class Project < ActiveRecord::Base :after_add => Proc.new {|o, r| o.developers_log << "after_adding#{r.id || ''}"}, :before_remove => Proc.new {|o, r| o.developers_log << "before_removing#{r.id}"}, :after_remove => Proc.new {|o, r| o.developers_log << "after_removing#{r.id}"} + has_and_belongs_to_many :well_payed_salary_groups, :class_name => "Developer", :group => "salary", :having => "SUM(salary) > 10000", :select => "SUM(salary) as salary" attr_accessor :developers_log -- cgit v1.2.3