Browse files

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
  • Loading branch information...
1 parent 6b1901d commit 81d619ea0d7e936bdf5a643c8731e4b3f3746b8e @jeremy jeremy committed Sep 17, 2007
View
3 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]
View
12 activerecord/lib/active_record/associations.rb
@@ -1165,7 +1165,7 @@ def create_has_many_reflection(association_id, options, &extension)
: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 @@ def create_has_and_belongs_to_many_reflection(association_id, options, &extensio
: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 @@ def condition_word(sql)
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:
View
7 activerecord/test/associations/extension_test.rb
@@ -23,7 +23,12 @@ def test_named_two_extensions_on_habtm
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
View
10 activerecord/test/fixtures/developer.rb
@@ -29,6 +29,16 @@ def find_most_recent
: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

0 comments on commit 81d619e

Please sign in to comment.