From 2a01d73218b908725c1059bacd13e1b3e3f7f44f Mon Sep 17 00:00:00 2001 From: Josh Susser Date: Mon, 28 Apr 2008 08:00:51 -0700 Subject: [PATCH] add has_(element)? method to models with has_many/through associations --- activerecord/lib/active_record/associations.rb | 4 ++++ .../associations/has_many_associations_test.rb | 16 ++++++++++++++++ .../has_many_through_associations_test.rb | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 7d27b06..541ce90 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1122,6 +1122,10 @@ module ActiveRecord define_method("#{reflection.name.to_s.singularize}_ids") do send(reflection.name).map(&:id) end + + define_method("has_#{reflection.name.to_s.singularize}?") do |element_of_collection| + !element_of_collection.nil? && send(reflection.name).find_by_id(element_of_collection.id) == element_of_collection + end end def collection_accessor_methods(reflection, association_proxy_class, writer = true) diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 7b97afe..51fb6e9 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -851,4 +851,20 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert ! firm.clients.include?(client) end + def test_has_element_when_included + assert authors(:david).has_post?(posts(:welcome)) + end + + def test_has_element_when_not_included + assert ! authors(:david).has_post?(posts(:eager_other)) + end + + def test_has_element_when_nil + assert ! authors(:david).has_post?(nil) + end + + def test_has_element_when_wrong_class_but_same_id_as_contained_element + assert ! authors(:david).has_post?( Author.find(posts(:welcome).id) ) + end + end \ No newline at end of file diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index a989910..b947e87 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -174,4 +174,20 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase post.people_with_callbacks.clear assert_equal (%w(Michael David Julian Roger) * 2).sort, log.last(8).collect(&:last).sort end + + def test_has_element_when_included + assert people(:michael).has_post?(posts(:welcome)) + end + + def test_has_element_when_not_included + assert ! people(:michael).has_post?(posts(:eager_other)) + end + + def test_has_element_when_nil + assert ! people(:michael).has_post?(nil) + end + + def test_has_element_when_wrong_class_but_same_id_as_contained_element + assert ! people(:michael).has_post?( Person.find(posts(:welcome).id) ) + end end -- 1.5.3.7