1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
Jeremy Kemper 2007-09-17 21:19:44 +00:00
parent 6b1901da8f
commit 81d619ea0d
4 changed files with 25 additions and 7 deletions

View file

@ -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 file

@ -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:

View file

@ -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

View file

@ -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