From b5775c2b3efb3ae5ef9074d26f6fc3e302a4f6f0 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 21 Jun 2009 14:35:14 +0100 Subject: Add expiry support File cache store [#1693 state:resolved] [Roman Shterenzon, Pratik Naik] --- activesupport/lib/active_support/cache.rb | 12 ++++++++++-- activesupport/lib/active_support/cache/file_store.rb | 14 +++++++++++++- activesupport/lib/active_support/cache/mem_cache_store.rb | 4 ---- activesupport/test/caching_test.rb | 10 ++++++++++ 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index feb6b1f2cf..192a82e74f 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -129,8 +129,8 @@ module ActiveSupport # # For example, MemCacheStore's #write method supports the +:expires_in+ # option, which tells the memcached server to automatically expire the - # cache item after a certain period. We can use this option with #fetch - # too: + # cache item after a certain period. This options is also supported by + # FileStore's #read method. We can use this option with #fetch too: # # cache = ActiveSupport::Cache::MemCacheStore.new # cache.fetch("foo", :force => true, :expires_in => 5.seconds) do @@ -169,6 +169,10 @@ module ActiveSupport # You may also specify additional options via the +options+ argument. # The specific cache store implementation will decide what to do with # +options+. + # + # For example, FileStore supports the +:expires_in+ option, which + # makes the method return nil for cache items older than the specified + # period. def read(key, options = nil) log("read", key, options) end @@ -223,6 +227,10 @@ module ActiveSupport end private + def expires_in(options) + (options && options[:expires_in]) || 0 + end + def log(operation, key, options) logger.debug("Cache #{operation}: #{key}#{options ? " (#{options.inspect})" : ""}") if logger && !@silence && !@logger_off end diff --git a/activesupport/lib/active_support/cache/file_store.rb b/activesupport/lib/active_support/cache/file_store.rb index 3217350d58..75eed5ed94 100644 --- a/activesupport/lib/active_support/cache/file_store.rb +++ b/activesupport/lib/active_support/cache/file_store.rb @@ -10,11 +10,23 @@ module ActiveSupport @cache_path = cache_path end + # Reads a value from the cache. + # + # Possible options: + # - +:expires_in+ - the number of seconds that this value may stay in + # the cache. def read(name, options = nil) super - File.open(real_file_path(name), 'rb') { |f| Marshal.load(f) } rescue nil + + file_name = real_file_path(name) + expires = expires_in(options) + + if File.exist?(file_name) && (expires <= 0 || Time.now - File.mtime(file_name) < expires) + File.open(file_name, 'rb') { |f| Marshal.load(f) } + end end + # Writes a value to the cache. def write(name, value, options = nil) super ensure_cache_path(File.dirname(real_file_path(name))) diff --git a/activesupport/lib/active_support/cache/mem_cache_store.rb b/activesupport/lib/active_support/cache/mem_cache_store.rb index 38b3409ca6..954d0f5423 100644 --- a/activesupport/lib/active_support/cache/mem_cache_store.rb +++ b/activesupport/lib/active_support/cache/mem_cache_store.rb @@ -130,10 +130,6 @@ module ActiveSupport end private - def expires_in(options) - (options && options[:expires_in]) || 0 - end - def raw?(options) options && options[:raw] end diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index 51d04d9388..e6e2205708 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -146,6 +146,16 @@ class FileStoreTest < ActiveSupport::TestCase end include CacheStoreBehavior + + def test_expires_in + @cache.write('foo', 'bar') + cache_read = lambda { @cache.read('foo', :expires_in => 2) } + assert_equal 'bar', cache_read.call + sleep(1) + assert_equal 'bar', cache_read.call + sleep(1) + assert_nil cache_read.call + end end class MemoryStoreTest < ActiveSupport::TestCase -- cgit v1.2.3 From 66eb05821b1cb522f497c874e7708fe705fb8356 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 21 Jun 2009 14:51:43 +0100 Subject: Use stubbing instead of sleep() in File store cache tests --- activesupport/test/caching_test.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index e6e2205708..c2a03818e1 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -148,12 +148,18 @@ class FileStoreTest < ActiveSupport::TestCase include CacheStoreBehavior def test_expires_in + time = Time.local(2008, 4, 24) + Time.stubs(:now).returns(time) + File.stubs(:mtime).returns(time) + @cache.write('foo', 'bar') - cache_read = lambda { @cache.read('foo', :expires_in => 2) } + cache_read = lambda { @cache.read('foo', :expires_in => 1.minute) } assert_equal 'bar', cache_read.call - sleep(1) + + Time.stubs(:now).returns(time + 30.seconds) assert_equal 'bar', cache_read.call - sleep(1) + + Time.stubs(:now).returns(time + 2.minutes) assert_nil cache_read.call end end -- cgit v1.2.3 From 9cb8c812f2a23ab5653a7888740a014a02c97c18 Mon Sep 17 00:00:00 2001 From: Darragh Curran Date: Sun, 21 Jun 2009 17:05:43 +0100 Subject: Add content_for?(:name) helper to check if content_for(:name) is present [#1311 state:resolved] Signed-off-by: Pratik Naik --- .../lib/action_view/helpers/capture_helper.rb | 22 ++++++++++++++++++++++ actionpack/test/template/capture_helper_test.rb | 15 +++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 actionpack/test/template/capture_helper_test.rb diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb index a8b5a9dbb9..c90acc5ac2 100644 --- a/actionpack/lib/action_view/helpers/capture_helper.rb +++ b/actionpack/lib/action_view/helpers/capture_helper.rb @@ -117,6 +117,28 @@ module ActionView @_content_for[name] end + # content_for? simply checks whether any content has been captured yet using content_for + # Useful to render parts of your layout differently based on what is in your views. + # + # ==== Examples + # + # Perhaps you will use different css in you layout if no content_for :right_column + # + # <%# This is the layout %> + # + # + # My Website + # <%= yield :script %> + # + # + # <%= yield %> + # <%= yield :right_col %> + # + # + def content_for?(name) + @_content_for[name].present? + end + # Use an alternate output buffer for the duration of the block. # Defaults to a new empty string. def with_output_buffer(buf = nil) #:nodoc: diff --git a/actionpack/test/template/capture_helper_test.rb b/actionpack/test/template/capture_helper_test.rb new file mode 100644 index 0000000000..2017a18806 --- /dev/null +++ b/actionpack/test/template/capture_helper_test.rb @@ -0,0 +1,15 @@ +require 'abstract_unit' + +class CaptureHelperTest < ActionView::TestCase + def setup + super + @_content_for = Hash.new {|h,k| h[k] = "" } + end + + def test_content_for + assert ! content_for?(:title) + content_for :title, 'title' + assert content_for?(:title) + assert ! content_for?(:something_else) + end +end -- cgit v1.2.3 From 83c1934003740ed01c618a8943457a0df53e2adb Mon Sep 17 00:00:00 2001 From: mattbauer Date: Sun, 21 Jun 2009 17:35:04 +0100 Subject: Ensure hm:t#create respects source associations hash conditions [#2090 state:resolved] Signed-off-by: Pratik Naik --- .../lib/active_record/associations/through_association_scope.rb | 7 +++++++ .../test/cases/associations/has_many_through_associations_test.rb | 6 ++++++ activerecord/test/models/post.rb | 2 ++ activerecord/test/schema/schema.rb | 1 + 4 files changed, 16 insertions(+) diff --git a/activerecord/lib/active_record/associations/through_association_scope.rb b/activerecord/lib/active_record/associations/through_association_scope.rb index 7661c50039..8e7ce33814 100644 --- a/activerecord/lib/active_record/associations/through_association_scope.rb +++ b/activerecord/lib/active_record/associations/through_association_scope.rb @@ -94,10 +94,17 @@ module ActiveRecord def construct_join_attributes(associate) # TODO: revist this to allow it for deletion, supposing dependent option is supported raise ActiveRecord::HasManyThroughCantAssociateThroughHasManyReflection.new(@owner, @reflection) if @reflection.source_reflection.macro == :has_many + join_attributes = construct_owner_attributes(@reflection.through_reflection).merge(@reflection.source_reflection.primary_key_name => associate.id) + if @reflection.options[:source_type] join_attributes.merge!(@reflection.source_reflection.options[:foreign_type] => associate.class.base_class.name.to_s) end + + if @reflection.through_reflection.options[:conditions].is_a?(Hash) + join_attributes.merge!(@reflection.through_reflection.options[:conditions]) + end + join_attributes end diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 4254badef2..7a4712d7c8 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -157,6 +157,12 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert_equal peeps + 1, posts(:thinking).people.count end + def test_associate_with_create_with_through_having_conditions + impatient_people = posts(:thinking).impatient_people.count + posts(:thinking).impatient_people.create!(:first_name => 'foo') + assert_equal impatient_people + 1, posts(:thinking).impatient_people.count + end + def test_associate_with_create_exclamation_and_no_options peeps = posts(:thinking).people.count posts(:thinking).people.create!(:first_name => 'foo') diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 374e536a5b..7392515ec7 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -69,6 +69,8 @@ class Post < ActiveRecord::Base :after_add => lambda {|owner, reader| log(:added, :after, reader.first_name) }, :before_remove => lambda {|owner, reader| log(:removed, :before, reader.first_name) }, :after_remove => lambda {|owner, reader| log(:removed, :after, reader.first_name) } + has_many :skimmers, :class_name => 'Reader', :conditions => { :skimmer => true } + has_many :impatient_people, :through => :skimmers, :source => :person def self.top(limit) ranked_by_comments.limit(limit) diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index b2aaccb352..d2d6d1f4b3 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -364,6 +364,7 @@ ActiveRecord::Schema.define do create_table :readers, :force => true do |t| t.integer :post_id, :null => false t.integer :person_id, :null => false + t.boolean :skimmer, :default => false end create_table :shape_expressions, :force => true do |t| -- cgit v1.2.3 From 21cd4c0e93fc6ac5497ada787d286c07f627e5ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=3D=3Futf-8=3Fq=3FMislav=3D20Marohni=3DC4=3D87=3F=3D?= Date: Sun, 21 Jun 2009 17:53:07 +0100 Subject: Fix polymorphic_path doesn't modify options hash [#2099 state:resolved] Signed-off-by: Pratik Naik --- .../action_controller/routing/generation/polymorphic_routes.rb | 3 +-- actionpack/test/activerecord/polymorphic_routes_test.rb | 9 ++++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/actionpack/lib/action_controller/routing/generation/polymorphic_routes.rb b/actionpack/lib/action_controller/routing/generation/polymorphic_routes.rb index d9b614c237..c6f7de17bd 100644 --- a/actionpack/lib/action_controller/routing/generation/polymorphic_routes.rb +++ b/actionpack/lib/action_controller/routing/generation/polymorphic_routes.rb @@ -112,8 +112,7 @@ module ActionController # Returns the path component of a URL for the given record. It uses # polymorphic_url with :routing_type => :path. def polymorphic_path(record_or_hash_or_array, options = {}) - options[:routing_type] = :path - polymorphic_url(record_or_hash_or_array, options) + polymorphic_url(record_or_hash_or_array, options.merge(:routing_type => :path)) end %w(edit new).each do |action| diff --git a/actionpack/test/activerecord/polymorphic_routes_test.rb b/actionpack/test/activerecord/polymorphic_routes_test.rb index b9f5be2361..2036d1eeb5 100644 --- a/actionpack/test/activerecord/polymorphic_routes_test.rb +++ b/actionpack/test/activerecord/polymorphic_routes_test.rb @@ -234,10 +234,13 @@ class PolymorphicRoutesTest < ActionController::TestCase with_admin_test_routes do @project.save @task.save + + options = {} object_array = [:admin, @project, @task] - assert_no_difference 'object_array.size' do - polymorphic_url(object_array) - end + original_args = [object_array.dup, options.dup] + + assert_no_difference('object_array.size') { polymorphic_path(object_array, options) } + assert_equal original_args, [object_array, options] end end -- cgit v1.2.3 From b26c2c11ab47dbd4d1e26806d1e861edc7d697e0 Mon Sep 17 00:00:00 2001 From: Brian Hogan Date: Thu, 19 Mar 2009 21:24:18 -0500 Subject: Ensure table names are quoted while renaming for sqlite3 adapter [#2272 state:resolved] Signed-off-by: Pratik Naik --- .../connection_adapters/sqlite_adapter.rb | 2 +- activerecord/test/cases/migration_test.rb | 26 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index c9d0c9574f..d96c6d6b51 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -245,7 +245,7 @@ module ActiveRecord end def rename_table(name, new_name) - execute "ALTER TABLE #{name} RENAME TO #{new_name}" + execute "ALTER TABLE #{quote_table_name(name)} RENAME TO #{quote_table_name(new_name)}" end # See: http://www.sqlite.org/lang_altertable.html diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index 16861f21b1..9ff95ae842 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -635,6 +635,32 @@ if ActiveRecord::Base.connection.supports_migrations? end end + if current_adapter?(:SQLiteAdapter) + def test_rename_table_for_sqlite_should_work_with_reserved_words + begin + assert_nothing_raised do + ActiveRecord::Base.connection.rename_table :references, :old_references + ActiveRecord::Base.connection.create_table :octopuses do |t| + t.column :url, :string + end + end + + assert_nothing_raised { ActiveRecord::Base.connection.rename_table :octopuses, :references } + + # Using explicit id in insert for compatibility across all databases + con = ActiveRecord::Base.connection + assert_nothing_raised do + con.execute "INSERT INTO 'references' (#{con.quote_column_name('id')}, #{con.quote_column_name('url')}) VALUES (1, 'http://rubyonrails.com')" + end + assert_equal 'http://rubyonrails.com', ActiveRecord::Base.connection.select_value("SELECT url FROM 'references' WHERE id=1") + + ensure + ActiveRecord::Base.connection.drop_table :references + ActiveRecord::Base.connection.rename_table :old_references, :references + end + end + end + def test_rename_table begin ActiveRecord::Base.connection.create_table :octopuses do |t| -- cgit v1.2.3 From eb30e4ca40370b416566f1bbaf0204bb379883e4 Mon Sep 17 00:00:00 2001 From: Joseph Wilk Date: Thu, 12 Mar 2009 13:21:22 +0000 Subject: Fixed a bug where create_table could not be called without a block [#2221 state:resolved] Signed-off-by: Pratik Naik --- .../connection_adapters/abstract/schema_statements.rb | 2 +- activerecord/test/cases/migration_test.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index ff63ea3a2e..f44cd0bd5a 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -101,7 +101,7 @@ module ActiveRecord table_definition = TableDefinition.new(self) table_definition.primary_key(options[:primary_key] || Base.get_primary_key(table_name)) unless options[:id] == false - yield table_definition + yield table_definition if block_given? if options[:force] && table_exists?(table_name) drop_table(table_name, options) diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index 9ff95ae842..215b5a427a 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -293,6 +293,13 @@ if ActiveRecord::Base.connection.supports_migrations? Person.connection.drop_table table_name rescue nil end + def test_create_table_without_a_block + table_name = :testings + Person.connection.create_table table_name + ensure + Person.connection.drop_table table_name rescue nil + end + # Sybase, and SQLite3 will not allow you to add a NOT NULL # column to a table without a default value. unless current_adapter?(:SybaseAdapter, :SQLiteAdapter) -- cgit v1.2.3 From 4851ca9e13a4317342df02ae25b1929340523f7a Mon Sep 17 00:00:00 2001 From: Patrick Joyce Date: Sun, 21 Jun 2009 22:07:23 +0100 Subject: Generate proper :counter_sql from :finder_sql when there is a newline character immediately following 'SELECT' [#2118 state:resolved] Signed-off-by: Pratik Naik --- .../active_record/associations/association_collection.rb | 14 +++++++++++++- .../associations/has_and_belongs_to_many_association.rb | 10 +--------- .../lib/active_record/associations/has_many_association.rb | 10 +--------- .../associations/has_many_through_association.rb | 10 +--------- .../has_and_belongs_to_many_associations_test.rb | 5 +++++ .../test/cases/associations/has_many_associations_test.rb | 5 +++++ activerecord/test/cases/base_test.rb | 6 +++--- activerecord/test/cases/calculations_test.rb | 4 ++-- activerecord/test/cases/finder_test.rb | 4 ++-- activerecord/test/cases/inheritance_test.rb | 4 ++-- activerecord/test/cases/reflection_test.rb | 4 ++-- activerecord/test/fixtures/companies.yml | 8 ++++++++ activerecord/test/models/company.rb | 4 ++++ activerecord/test/models/project.rb | 6 ++++++ 14 files changed, 55 insertions(+), 39 deletions(-) diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index e12f6be35d..84edaec15e 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -351,7 +351,19 @@ module ActiveRecord protected def construct_find_options!(options) end - + + def construct_counter_sql + if @reflection.options[:counter_sql] + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + elsif @reflection.options[:finder_sql] + # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ + @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT\b(\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + else + @counter_sql = @finder_sql + end + end + def load_target if !@owner.new_record? || foreign_key_present begin diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index af9ce3dfb2..fd23e59e82 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -85,15 +85,7 @@ module ActiveRecord @join_sql = "INNER JOIN #{@owner.connection.quote_table_name @reflection.options[:join_table]} ON #{@reflection.quoted_table_name}.#{@reflection.klass.primary_key} = #{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.association_foreign_key}" - if @reflection.options[:counter_sql] - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - elsif @reflection.options[:finder_sql] - # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ - @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - else - @counter_sql = @finder_sql - end + construct_counter_sql end def construct_scope diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 73dd50dd07..e4b631bc54 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -97,15 +97,7 @@ module ActiveRecord @finder_sql << " AND (#{conditions})" if conditions end - if @reflection.options[:counter_sql] - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - elsif @reflection.options[:finder_sql] - # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ - @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - else - @counter_sql = @finder_sql - end + construct_counter_sql end def construct_scope diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 67631f0120..e21ef90391 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -90,15 +90,7 @@ module ActiveRecord @finder_sql = construct_conditions end - if @reflection.options[:counter_sql] - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - elsif @reflection.options[:finder_sql] - # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ - @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - else - @counter_sql = @finder_sql - end + construct_counter_sql end def has_cached_counter? 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 8dc95806b9..9dbbd01aa1 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 @@ -803,6 +803,11 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal 1, developer.projects.count end + def test_count_with_finder_sql + assert_equal 3, projects(:active_record).developers_with_finder_sql.count + assert_equal 3, projects(:active_record).developers_with_multiline_finder_sql.count + end + def test_association_proxy_transaction_method_starts_transaction_in_association_class Post.expects(:transaction) Category.find(:first).posts.transaction do diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index d99424f9cd..15919e2289 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -163,6 +163,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 0, Firm.find(:first).no_clients_using_counter_sql.size end + def test_counting_using_finder_sql + assert_equal 2, Firm.find(4).clients_using_sql.count + assert_equal 2, Firm.find(4).clients_using_multiline_sql.count + end + def test_belongs_to_sanity c = Client.new assert_nil c.firm diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 96270118d8..0f3f6e41a5 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -599,9 +599,9 @@ class BasicsTest < ActiveRecord::TestCase end def test_destroy_many - assert_equal 3, Client.count - Client.destroy([2, 3]) - assert_equal 1, Client.count + assert_difference('Client.count', -2) do + Client.destroy([2, 3]) + end end def test_delete_many diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index fd455e0864..75f52dfa4a 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -203,7 +203,7 @@ class CalculationsTest < ActiveRecord::TestCase c = Company.count(:all, :group => "UPPER(#{QUOTED_TYPE})") assert_equal 2, c[nil] assert_equal 1, c['DEPENDENTFIRM'] - assert_equal 3, c['CLIENT'] + assert_equal 4, c['CLIENT'] assert_equal 2, c['FIRM'] end @@ -211,7 +211,7 @@ class CalculationsTest < ActiveRecord::TestCase c = Company.count(:all, :group => "UPPER(companies.#{QUOTED_TYPE})") assert_equal 2, c[nil] assert_equal 1, c['DEPENDENTFIRM'] - assert_equal 3, c['CLIENT'] + assert_equal 4, c['CLIENT'] assert_equal 2, c['FIRM'] end diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 037b67ec84..d8f5695a0f 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -1044,8 +1044,8 @@ class FinderTest < ActiveRecord::TestCase end def test_select_values - assert_equal ["1","2","3","4","5","6","7","8","9"], Company.connection.select_values("SELECT id FROM companies ORDER BY id").map! { |i| i.to_s } - assert_equal ["37signals","Summit","Microsoft", "Flamboyant Software", "Ex Nihilo", "RailsCore", "Leetsoft", "Jadedpixel", "Odegy"], Company.connection.select_values("SELECT name FROM companies ORDER BY id") + assert_equal ["1","2","3","4","5","6","7","8","9", "10"], Company.connection.select_values("SELECT id FROM companies ORDER BY id").map! { |i| i.to_s } + assert_equal ["37signals","Summit","Microsoft", "Flamboyant Software", "Ex Nihilo", "RailsCore", "Leetsoft", "Jadedpixel", "Odegy", "Ex Nihilo Part Deux"], Company.connection.select_values("SELECT name FROM companies ORDER BY id") end def test_select_rows diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index eae5a60829..167d3abad9 100644 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -112,9 +112,9 @@ class InheritanceTest < ActiveRecord::TestCase end def test_inheritance_condition - assert_equal 9, Company.count + assert_equal 10, Company.count assert_equal 2, Firm.count - assert_equal 3, Client.count + assert_equal 4, Client.count end def test_alt_inheritance_condition diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb index db64bbb806..9275c9af89 100644 --- a/activerecord/test/cases/reflection_test.rb +++ b/activerecord/test/cases/reflection_test.rb @@ -170,8 +170,8 @@ class ReflectionTest < ActiveRecord::TestCase def test_reflection_of_all_associations # FIXME these assertions bust a lot - assert_equal 28, Firm.reflect_on_all_associations.size - assert_equal 21, Firm.reflect_on_all_associations(:has_many).size + assert_equal 29, Firm.reflect_on_all_associations.size + assert_equal 22, Firm.reflect_on_all_associations(:has_many).size assert_equal 7, Firm.reflect_on_all_associations(:has_one).size assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size end diff --git a/activerecord/test/fixtures/companies.yml b/activerecord/test/fixtures/companies.yml index e7691fde46..9ad68fbe11 100644 --- a/activerecord/test/fixtures/companies.yml +++ b/activerecord/test/fixtures/companies.yml @@ -35,6 +35,14 @@ another_client: name: Ex Nihilo ruby_type: Client +a_third_client: + id: 10 + type: Client + firm_id: 4 + client_of: 4 + name: Ex Nihilo Part Deux + ruby_type: Client + rails_core: id: 6 name: RailsCore diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index 4c2514b0b3..840527ddeb 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -48,6 +48,10 @@ class Firm < Company has_many :clients_with_interpolated_conditions, :class_name => "Client", :conditions => 'rating > #{rating}' has_many :clients_like_ms_with_hash_conditions, :conditions => { :name => 'Microsoft' }, :class_name => "Client", :order => "id" has_many :clients_using_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}' + has_many :clients_using_multiline_sql, :class_name => "Client", :finder_sql => ' + SELECT + companies.* + FROM companies WHERE companies.client_of = #{id}' has_many :clients_using_counter_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}', :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = #{id}' diff --git a/activerecord/test/models/project.rb b/activerecord/test/models/project.rb index f25b2ddf77..422b12dc83 100644 --- a/activerecord/test/models/project.rb +++ b/activerecord/test/models/project.rb @@ -8,6 +8,12 @@ class Project < ActiveRecord::Base has_and_belongs_to_many :developers_named_david_with_hash_conditions, :class_name => "Developer", :conditions => { :name => 'David' }, :uniq => true has_and_belongs_to_many :salaried_developers, :class_name => "Developer", :conditions => "salary > 0" has_and_belongs_to_many :developers_with_finder_sql, :class_name => "Developer", :finder_sql => 'SELECT t.*, j.* FROM developers_projects j, developers t WHERE t.id = j.developer_id AND j.project_id = #{id} ORDER BY t.id' + has_and_belongs_to_many :developers_with_multiline_finder_sql, :class_name => "Developer", :finder_sql => ' + SELECT + t.*, j.* + FROM + developers_projects j, + developers t WHERE t.id = j.developer_id AND j.project_id = #{id} ORDER BY t.id' has_and_belongs_to_many :developers_by_sql, :class_name => "Developer", :delete_sql => "DELETE FROM developers_projects WHERE project_id = \#{id} AND developer_id = \#{record.id}" has_and_belongs_to_many :developers_with_callbacks, :class_name => "Developer", :before_add => Proc.new {|o, r| o.developers_log << "before_adding#{r.id || ''}"}, :after_add => Proc.new {|o, r| o.developers_log << "after_adding#{r.id || ''}"}, -- cgit v1.2.3 From 80f1f863cd0f9cba89079511282de5710a2e1832 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Mon, 22 Jun 2009 12:04:02 -0700 Subject: Revert "Generate proper :counter_sql from :finder_sql when there is a newline character immediately following 'SELECT' [#2118 state:resolved]" This reverts commit 4851ca9e13a4317342df02ae25b1929340523f7a. The tests do not pass for postgresql. --- .../active_record/associations/association_collection.rb | 14 +------------- .../associations/has_and_belongs_to_many_association.rb | 10 +++++++++- .../lib/active_record/associations/has_many_association.rb | 10 +++++++++- .../associations/has_many_through_association.rb | 10 +++++++++- .../has_and_belongs_to_many_associations_test.rb | 5 ----- .../test/cases/associations/has_many_associations_test.rb | 5 ----- activerecord/test/cases/base_test.rb | 6 +++--- activerecord/test/cases/calculations_test.rb | 4 ++-- activerecord/test/cases/finder_test.rb | 4 ++-- activerecord/test/cases/inheritance_test.rb | 4 ++-- activerecord/test/cases/reflection_test.rb | 4 ++-- activerecord/test/fixtures/companies.yml | 8 -------- activerecord/test/models/company.rb | 4 ---- activerecord/test/models/project.rb | 6 ------ 14 files changed, 39 insertions(+), 55 deletions(-) diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 84edaec15e..e12f6be35d 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -351,19 +351,7 @@ module ActiveRecord protected def construct_find_options!(options) end - - def construct_counter_sql - if @reflection.options[:counter_sql] - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - elsif @reflection.options[:finder_sql] - # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ - @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT\b(\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } - @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) - else - @counter_sql = @finder_sql - end - end - + def load_target if !@owner.new_record? || foreign_key_present begin diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index fd23e59e82..af9ce3dfb2 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -85,7 +85,15 @@ module ActiveRecord @join_sql = "INNER JOIN #{@owner.connection.quote_table_name @reflection.options[:join_table]} ON #{@reflection.quoted_table_name}.#{@reflection.klass.primary_key} = #{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.association_foreign_key}" - construct_counter_sql + if @reflection.options[:counter_sql] + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + elsif @reflection.options[:finder_sql] + # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ + @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + else + @counter_sql = @finder_sql + end end def construct_scope diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index e4b631bc54..73dd50dd07 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -97,7 +97,15 @@ module ActiveRecord @finder_sql << " AND (#{conditions})" if conditions end - construct_counter_sql + if @reflection.options[:counter_sql] + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + elsif @reflection.options[:finder_sql] + # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ + @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + else + @counter_sql = @finder_sql + end end def construct_scope diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index e21ef90391..67631f0120 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -90,7 +90,15 @@ module ActiveRecord @finder_sql = construct_conditions end - construct_counter_sql + if @reflection.options[:counter_sql] + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + elsif @reflection.options[:finder_sql] + # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ + @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + else + @counter_sql = @finder_sql + end end def has_cached_counter? 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 9dbbd01aa1..8dc95806b9 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 @@ -803,11 +803,6 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal 1, developer.projects.count end - def test_count_with_finder_sql - assert_equal 3, projects(:active_record).developers_with_finder_sql.count - assert_equal 3, projects(:active_record).developers_with_multiline_finder_sql.count - end - def test_association_proxy_transaction_method_starts_transaction_in_association_class Post.expects(:transaction) Category.find(:first).posts.transaction do diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 15919e2289..d99424f9cd 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -163,11 +163,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 0, Firm.find(:first).no_clients_using_counter_sql.size end - def test_counting_using_finder_sql - assert_equal 2, Firm.find(4).clients_using_sql.count - assert_equal 2, Firm.find(4).clients_using_multiline_sql.count - end - def test_belongs_to_sanity c = Client.new assert_nil c.firm diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 0f3f6e41a5..96270118d8 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -599,9 +599,9 @@ class BasicsTest < ActiveRecord::TestCase end def test_destroy_many - assert_difference('Client.count', -2) do - Client.destroy([2, 3]) - end + assert_equal 3, Client.count + Client.destroy([2, 3]) + assert_equal 1, Client.count end def test_delete_many diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 75f52dfa4a..fd455e0864 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -203,7 +203,7 @@ class CalculationsTest < ActiveRecord::TestCase c = Company.count(:all, :group => "UPPER(#{QUOTED_TYPE})") assert_equal 2, c[nil] assert_equal 1, c['DEPENDENTFIRM'] - assert_equal 4, c['CLIENT'] + assert_equal 3, c['CLIENT'] assert_equal 2, c['FIRM'] end @@ -211,7 +211,7 @@ class CalculationsTest < ActiveRecord::TestCase c = Company.count(:all, :group => "UPPER(companies.#{QUOTED_TYPE})") assert_equal 2, c[nil] assert_equal 1, c['DEPENDENTFIRM'] - assert_equal 4, c['CLIENT'] + assert_equal 3, c['CLIENT'] assert_equal 2, c['FIRM'] end diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index d8f5695a0f..037b67ec84 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -1044,8 +1044,8 @@ class FinderTest < ActiveRecord::TestCase end def test_select_values - assert_equal ["1","2","3","4","5","6","7","8","9", "10"], Company.connection.select_values("SELECT id FROM companies ORDER BY id").map! { |i| i.to_s } - assert_equal ["37signals","Summit","Microsoft", "Flamboyant Software", "Ex Nihilo", "RailsCore", "Leetsoft", "Jadedpixel", "Odegy", "Ex Nihilo Part Deux"], Company.connection.select_values("SELECT name FROM companies ORDER BY id") + assert_equal ["1","2","3","4","5","6","7","8","9"], Company.connection.select_values("SELECT id FROM companies ORDER BY id").map! { |i| i.to_s } + assert_equal ["37signals","Summit","Microsoft", "Flamboyant Software", "Ex Nihilo", "RailsCore", "Leetsoft", "Jadedpixel", "Odegy"], Company.connection.select_values("SELECT name FROM companies ORDER BY id") end def test_select_rows diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index 167d3abad9..eae5a60829 100644 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -112,9 +112,9 @@ class InheritanceTest < ActiveRecord::TestCase end def test_inheritance_condition - assert_equal 10, Company.count + assert_equal 9, Company.count assert_equal 2, Firm.count - assert_equal 4, Client.count + assert_equal 3, Client.count end def test_alt_inheritance_condition diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb index 9275c9af89..db64bbb806 100644 --- a/activerecord/test/cases/reflection_test.rb +++ b/activerecord/test/cases/reflection_test.rb @@ -170,8 +170,8 @@ class ReflectionTest < ActiveRecord::TestCase def test_reflection_of_all_associations # FIXME these assertions bust a lot - assert_equal 29, Firm.reflect_on_all_associations.size - assert_equal 22, Firm.reflect_on_all_associations(:has_many).size + assert_equal 28, Firm.reflect_on_all_associations.size + assert_equal 21, Firm.reflect_on_all_associations(:has_many).size assert_equal 7, Firm.reflect_on_all_associations(:has_one).size assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size end diff --git a/activerecord/test/fixtures/companies.yml b/activerecord/test/fixtures/companies.yml index 9ad68fbe11..e7691fde46 100644 --- a/activerecord/test/fixtures/companies.yml +++ b/activerecord/test/fixtures/companies.yml @@ -35,14 +35,6 @@ another_client: name: Ex Nihilo ruby_type: Client -a_third_client: - id: 10 - type: Client - firm_id: 4 - client_of: 4 - name: Ex Nihilo Part Deux - ruby_type: Client - rails_core: id: 6 name: RailsCore diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index 840527ddeb..4c2514b0b3 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -48,10 +48,6 @@ class Firm < Company has_many :clients_with_interpolated_conditions, :class_name => "Client", :conditions => 'rating > #{rating}' has_many :clients_like_ms_with_hash_conditions, :conditions => { :name => 'Microsoft' }, :class_name => "Client", :order => "id" has_many :clients_using_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}' - has_many :clients_using_multiline_sql, :class_name => "Client", :finder_sql => ' - SELECT - companies.* - FROM companies WHERE companies.client_of = #{id}' has_many :clients_using_counter_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}', :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = #{id}' diff --git a/activerecord/test/models/project.rb b/activerecord/test/models/project.rb index 422b12dc83..f25b2ddf77 100644 --- a/activerecord/test/models/project.rb +++ b/activerecord/test/models/project.rb @@ -8,12 +8,6 @@ class Project < ActiveRecord::Base has_and_belongs_to_many :developers_named_david_with_hash_conditions, :class_name => "Developer", :conditions => { :name => 'David' }, :uniq => true has_and_belongs_to_many :salaried_developers, :class_name => "Developer", :conditions => "salary > 0" has_and_belongs_to_many :developers_with_finder_sql, :class_name => "Developer", :finder_sql => 'SELECT t.*, j.* FROM developers_projects j, developers t WHERE t.id = j.developer_id AND j.project_id = #{id} ORDER BY t.id' - has_and_belongs_to_many :developers_with_multiline_finder_sql, :class_name => "Developer", :finder_sql => ' - SELECT - t.*, j.* - FROM - developers_projects j, - developers t WHERE t.id = j.developer_id AND j.project_id = #{id} ORDER BY t.id' has_and_belongs_to_many :developers_by_sql, :class_name => "Developer", :delete_sql => "DELETE FROM developers_projects WHERE project_id = \#{id} AND developer_id = \#{record.id}" has_and_belongs_to_many :developers_with_callbacks, :class_name => "Developer", :before_add => Proc.new {|o, r| o.developers_log << "before_adding#{r.id || ''}"}, :after_add => Proc.new {|o, r| o.developers_log << "after_adding#{r.id || ''}"}, -- cgit v1.2.3