Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add AssociationReflection#collection_association? which returns true …

…if it's for a has_many or has_and_belongs_to_many association.
  • Loading branch information...
commit f82adc7c5addb4f9bf6b9cb3f1fcf3fb47505e53 1 parent 1afa9fa
@alloy alloy authored
View
9 activerecord/lib/active_record/reflection.rb
@@ -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
View
9 activerecord/test/cases/reflection_test.rb
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.