Skip to content
Browse files

Undeprecate the :extend option

Suggested by @dhh.

It doesn't affect the generated SQL, so seems reasonable to continue to
allow it as an association option.
  • Loading branch information...
1 parent 5f759ff commit 5937bd02dee112646469848d7fe8a8bfcef5b4c1 @jonleighton jonleighton committed Jan 18, 2013
View
1 activerecord/CHANGELOG.md
@@ -985,7 +985,6 @@
* `:conditions` becomes `:where`.
* `:include` becomes `:includes`.
- * `:extend` becomes `:extending`.
The code to implement the deprecated features has been moved out to
the `activerecord-deprecated_finders` gem. This gem is a dependency
View
2 activerecord/activerecord.gemspec
@@ -25,5 +25,5 @@ Gem::Specification.new do |s|
s.add_dependency 'activemodel', version
s.add_dependency 'arel', '~> 3.0.2'
- s.add_dependency 'activerecord-deprecated_finders', '0.0.1'
+ s.add_dependency 'activerecord-deprecated_finders', '0.0.2'
end
View
1 activerecord/lib/active_record/associations/association_scope.rb
@@ -16,6 +16,7 @@ def initialize(association)
def scope
scope = klass.unscoped
scope.merge! eval_scope(klass, reflection.scope) if reflection.scope
+ scope.extending! Array(options[:extend])
add_constraints(scope)
end
View
3 activerecord/lib/active_record/associations/builder/collection_association.rb
@@ -6,7 +6,8 @@ class CollectionAssociation < Association #:nodoc:
CALLBACKS = [:before_add, :after_add, :before_remove, :after_remove]
def valid_options
- super + [:table_name, :finder_sql, :counter_sql, :before_add, :after_add, :before_remove, :after_remove]
+ super + [:table_name, :finder_sql, :counter_sql, :before_add,
+ :after_add, :before_remove, :after_remove, :extend]
end
attr_reader :block_extension, :extension_module
View
12 activerecord/test/cases/associations/has_many_associations_test.rb
@@ -1717,4 +1717,16 @@ def test_collection_association_with_private_kernel_method
author = authors(:mary)
assert !author.first_posts.exists?
end
+
+ test "association with extend option" do
+ post = posts(:welcome)
+ assert_equal "lifo", post.comments_with_extend.author
+ assert_equal "hello", post.comments_with_extend.greeting
+ end
+
+ test "association with extend option with multiple extensions" do
+ post = posts(:welcome)
+ assert_equal "lifo", post.comments_with_extend_2.author
+ assert_equal "hello", post.comments_with_extend_2.greeting
+ end
end
View
14 activerecord/test/models/post.rb
@@ -5,6 +5,12 @@ def author
end
end
+ module NamedExtension2
+ def greeting
+ "hello"
+ end
+ end
+
scope :containing_the_letter_a, -> { where("body LIKE '%a%'") }
scope :ranked_by_comments, -> { order("comments_count DESC") }
@@ -43,6 +49,14 @@ def the_association
end
end
+ has_many :comments_with_extend, extend: NamedExtension, class_name: "Comment", foreign_key: "post_id" do
+ def greeting
+ "hello"
+ end
+ end
+
+ has_many :comments_with_extend_2, extend: [NamedExtension, NamedExtension2], class_name: "Comment", foreign_key: "post_id"
+
has_many :author_favorites, :through => :author
has_many :author_categorizations, :through => :author, :source => :categorizations
has_many :author_addresses, :through => :author

1 comment on commit 5937bd0

@schneems
Ruby on Rails member

I don't think the :extending or :extend keys for has_many are documented anywhere.

Here's a brief:

The extend and extending keys accept a module or array of modules that will be applied to the association returned by the has_many association.

#   module NamedExtension
#     def greeting
#       "hello"
#     end
#   end
# 
#   class Account < ActiveRecord::Base
#     has_many :comments_with_extend, extend: NamedExtension
#   end
#
#   person = Account.first.people.find_or_create_by_name("David Heinemeier Hansson")
#   person.comments_with_extend.greeting # => "hello"
Please sign in to comment.
Something went wrong with that request. Please try again.