From 88ad068b592e987caa37421a383c7e4dd3c77868 Mon Sep 17 00:00:00 2001 From: =?utf-8?q?Jakub=20Ku=C5=BAma?= Date: Mon, 24 Nov 2008 23:49:20 +0100 Subject: [PATCH] In scope? --- activerecord/lib/active_record/named_scope.rb | 3 +++ activerecord/test/cases/named_scope_test.rb | 7 +++++++ 2 files changed, 10 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 83043c2..31664f9 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -96,6 +96,9 @@ module ActiveRecord scopes[name].call(self, *args) end end + define_method "in_#{name.to_s}?" do |*args| + self.class.send(name, *args).include?(self) + end end end diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 64e8997..e036140 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -90,6 +90,13 @@ class NamedScopeTest < ActiveRecord::TestCase assert_equal approved & replied, Topic.approved.replied end + def test_scope_inclusion_question + assert Topic.approved.first.in_approved? + assert Topic.replied.first.in_replied? + assert !Topic.find(:first, :conditions => {:approved => false}).in_approved? + assert !Topic.find(:first, :conditions => 'replies_count = 0').in_replied? + end + def test_procedural_scopes topics_written_before_the_third = Topic.find(:all, :conditions => ['written_on < ?', topics(:third).written_on]) topics_written_before_the_second = Topic.find(:all, :conditions => ['written_on < ?', topics(:second).written_on]) -- 1.5.6.3