提交 f82adc7c 编写于 作者: E Eloy Duran

Add AssociationReflection#collection_association? which returns true if it's...

Add AssociationReflection#collection_association? which returns true if it's for a has_many or has_and_belongs_to_many association.
上级 1afa9fa5
......@@ -252,10 +252,17 @@ def polymorphic_inverse_of(associated_class)
end
end
# Returns whether or not this association reflection is for a collection
# association. Returns +true+ if the +macro+ is one of +has_many+ or
# +has_and_belongs_to_many+, +false+ otherwise.
def collection_association?
[:has_many, :has_and_belongs_to_many].include?(macro)
end
private
def derive_class_name
class_name = name.to_s.camelize
class_name = class_name.singularize if [ :has_many, :has_and_belongs_to_many ].include?(macro)
class_name = class_name.singularize if collection_association?
class_name
end
......
......@@ -4,6 +4,7 @@
require 'models/company'
require 'models/company_in_module'
require 'models/subscriber'
require 'models/ship'
require 'models/pirate'
require 'models/price_estimate'
......@@ -194,6 +195,14 @@ def test_has_many_through_reflection
assert_kind_of ActiveRecord::Reflection::ThroughReflection, Subscriber.reflect_on_association(:books)
end
def test_collection_association?
assert Pirate.reflect_on_association(:birds).collection_association?
assert Pirate.reflect_on_association(:parrots).collection_association?
assert !Pirate.reflect_on_association(:ship).collection_association?
assert !Ship.reflect_on_association(:pirate).collection_association?
end
private
def assert_reflection(klass, association, options)
assert reflection = klass.reflect_on_association(association)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册