From 81d619ea0d7e936bdf5a643c8731e4b3f3746b8e Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 17 Sep 2007 21:19:44 +0000 Subject: Associations macros accept extension blocks alongside modules. Closes #9346. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7504 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 3 +++ activerecord/lib/active_record/associations.rb | 12 ++++++------ activerecord/test/associations/extension_test.rb | 7 ++++++- activerecord/test/fixtures/developer.rb | 10 ++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 3badae0ca1..df02908386 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,8 @@ *SVN* +* Associations macros accept extension blocks alongside modules. #9346 [Josh +Peek] + * Speed up and simplify query caching. [Jeremy Kemper] * connection.select_rows 'sql' returns an array (rows) of arrays (field values). #2329 [Michael Schuerig] diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 8cb6bd02ac..a508a9c94d 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1165,7 +1165,7 @@ module ActiveRecord :extend ) - options[:extend] = create_extension_module(association_id, extension) if block_given? + options[:extend] = create_extension_modules(association_id, extension, options[:extend]) if block_given? create_reflection(:has_many, association_id, options, self) end @@ -1203,7 +1203,7 @@ module ActiveRecord :extend ) - options[:extend] = create_extension_module(association_id, extension) if block_given? + options[:extend] = create_extension_modules(association_id, extension, options[:extend]) if block_given? reflection = create_reflection(:has_and_belongs_to_many, association_id, options, self) @@ -1346,14 +1346,14 @@ module ActiveRecord sql =~ /where/i ? " AND " : "WHERE " end - def create_extension_module(association_id, extension) + def create_extension_modules(association_id, block_extension, extensions) extension_module_name = "#{self.to_s}#{association_id.to_s.camelize}AssociationExtension" silence_warnings do - Object.const_set(extension_module_name, Module.new(&extension)) + Object.const_set(extension_module_name, Module.new(&block_extension)) end - - extension_module_name.constantize + + Array(extensions).push(extension_module_name.constantize) end class JoinDependency # :nodoc: diff --git a/activerecord/test/associations/extension_test.rb b/activerecord/test/associations/extension_test.rb index e80a2b9fb9..67a9ee02ad 100644 --- a/activerecord/test/associations/extension_test.rb +++ b/activerecord/test/associations/extension_test.rb @@ -23,7 +23,12 @@ class AssociationsExtensionsTest < Test::Unit::TestCase assert_equal projects(:action_controller), developers(:david).projects_extended_by_name_twice.find_most_recent assert_equal projects(:active_record), developers(:david).projects_extended_by_name_twice.find_least_recent end - + + def test_named_extension_and_block_on_habtm + assert_equal projects(:action_controller), developers(:david).projects_extended_by_name_and_block.find_most_recent + assert_equal projects(:active_record), developers(:david).projects_extended_by_name_and_block.find_least_recent + end + def test_marshalling_extensions david = developers(:david) assert_equal projects(:action_controller), david.projects.find_most_recent diff --git a/activerecord/test/fixtures/developer.rb b/activerecord/test/fixtures/developer.rb index 75fb307b19..c868cddecd 100644 --- a/activerecord/test/fixtures/developer.rb +++ b/activerecord/test/fixtures/developer.rb @@ -29,6 +29,16 @@ class Developer < ActiveRecord::Base :association_foreign_key => "project_id", :extend => [DeveloperProjectsAssociationExtension, DeveloperProjectsAssociationExtension2] + has_and_belongs_to_many :projects_extended_by_name_and_block, + :class_name => "Project", + :join_table => "developers_projects", + :association_foreign_key => "project_id", + :extend => DeveloperProjectsAssociationExtension do + def find_least_recent + find(:first, :order => "id ASC") + end + end + has_and_belongs_to_many :special_projects, :join_table => 'developers_projects', :association_foreign_key => 'project_id' validates_inclusion_of :salary, :in => 50000..200000 -- cgit v1.2.3