Permalink
Browse files

Allow multiple association extensions with :extend option (closes #4666

) [Josh Susser]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4206 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 98634e7 commit 7e76740d2a5ee14b308c7e40f9c95354d19f6189 @technoweenie technoweenie committed Apr 11, 2006
View
@@ -1,3 +1,12 @@
+*SVN*
+
+* Allow multiple association extensions with :extend option (closes #4666) [Josh Susser]
+
+ class Account < ActiveRecord::Base
+ has_many :people, :extend => [FindOrCreateByNameExtension, FindRecentExtension]
+ end
+
+
*1.14.2* (April 9th, 2005)
* Fixed calculations for the Oracle Adapter (closes #4626) [Michael Schoen]
@@ -215,6 +215,14 @@ def clear_association_cache #:nodoc:
# has_many :people, :extend => FindOrCreateByNameExtension
# end
#
+ # If you need to use multiple named extension modules, you can specify an array of modules with the :extend option.
+ # In the case of name conflicts between methods in the modules, methods in modules later in the array supercede
+ # those earlier in the array. Example:
+ #
+ # class Account < ActiveRecord::Base
+ # has_many :people, :extend => [FindOrCreateByNameExtension, FindRecentExtension]
+ # end
+ #
# === Association Join Models
#
# Has Many associations can be configured with the :through option to use an explicit join model to retrieve the data. This
@@ -8,7 +8,7 @@ class AssociationProxy #:nodoc:
def initialize(owner, reflection)
@owner, @reflection = owner, reflection
- proxy_extend(reflection.options[:extend]) if reflection.options[:extend]
+ Array(reflection.options[:extend]).each { |ext| proxy_extend(ext) }
reset
end
@@ -18,6 +18,11 @@ def test_extension_on_habtm
def test_named_extension_on_habtm
assert_equal projects(:action_controller), developers(:david).projects_extended_by_name.find_most_recent
end
+
+ 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_marshalling_extensions
david = developers(:david)
@@ -4,6 +4,12 @@ def find_most_recent
end
end
+module DeveloperProjectsAssociationExtension2
+ def find_least_recent
+ find(:first, :order => "id ASC")
+ end
+end
+
class Developer < ActiveRecord::Base
has_and_belongs_to_many :projects do
def find_most_recent
@@ -17,6 +23,12 @@ def find_most_recent
:association_foreign_key => "project_id",
:extend => DeveloperProjectsAssociationExtension
+ has_and_belongs_to_many :projects_extended_by_name_twice,
+ :class_name => "Project",
+ :join_table => "developers_projects",
+ :association_foreign_key => "project_id",
+ :extend => [DeveloperProjectsAssociationExtension, DeveloperProjectsAssociationExtension2]
+
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 7e76740

Please sign in to comment.