From 9c7f4687591d425f3c7c2684540a0d7bdb289fb9 Mon Sep 17 00:00:00 2001 From: Elad Meidar Date: Sat, 8 Aug 2009 17:40:36 -0400 Subject: [PATCH] changed empty dentation method for asscoiations from has_? to ? --- activerecord/lib/active_record/reflection.rb | 7 +++++++ activerecord/test/cases/reflection_test.rb | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 2d4c1d5..edc9914 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -140,6 +140,13 @@ module ActiveRecord # Holds all the meta-data about an association as it was specified in the Active Record class. class AssociationReflection < MacroReflection #:nodoc: + def initialize(macro, name, options, active_record) + super(macro, name, options, active_record) + if active_record && name + active_record.send(:define_method, "#{name}?") { !self.send(name).blank? } + end + end + # Returns the target association's class: # # class Author < ActiveRecord::Base diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb index 30ec157..fcfef84 100644 --- a/activerecord/test/cases/reflection_test.rb +++ b/activerecord/test/cases/reflection_test.rb @@ -184,6 +184,30 @@ class ReflectionTest < ActiveRecord::TestCase assert_kind_of ActiveRecord::Reflection::ThroughReflection, Subscriber.reflect_on_association(:books) end + def test_belongs_to_association_defines_has_associated_object_method + Company.belongs_to :foo + company = Company.new + assert company.respond_to?(:foo?) + end + + def test_has_one_association_defines_has_associated_object_method + Company.has_one :foo + company = Company.new + assert company.respond_to?(:foo?) + end + + def test_has_many_association_defines_has_associated_object_method + Company.has_many :foos + company = Company.new + assert company.respond_to?(:foos?) + end + + def test_has_and_belongs_to_many_association_defines_has_associated_object_method + Company.has_and_belongs_to_many :foos + company = Company.new + assert company.respond_to?(:foos?) + end + private def assert_reflection(klass, association, options) assert reflection = klass.reflect_on_association(association) -- 1.6.0.2