From 220fa226832648ed6ec0b1357d5084ad1d9bfa7f Mon Sep 17 00:00:00 2001 From: Ben Sandofsky Date: Mon, 16 Jun 2008 02:28:46 -0700 Subject: [PATCH] Support binding lambdas to activerecord conditions. --- activerecord/lib/active_record/base.rb | 4 ++-- activerecord/test/cases/finder_test.rb | 5 +++++ activerecord/test/models/company.rb | 8 ++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 1c16d5d..c992f84 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2085,10 +2085,10 @@ module ActiveRecord #:nodoc: if value.respond_to?(:empty?) && value.empty? connection.quote(nil) else - value.map { |v| connection.quote(v) }.join(',') + value.map { |v| connection.quote(v.kind_of?(Proc) ? v.call : v) }.join(',') end else - connection.quote(value) + connection.quote(value.kind_of?(Proc) ? value.call : value) end end diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 80936d5..ec6b018 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -358,6 +358,11 @@ class FinderTest < ActiveRecord::TestCase assert_kind_of Time, Topic.find(:first, :conditions => ["id = :id", { :id => 1 }]).written_on end + def test_binding_lambdas + assert_equal Company.find(1), Company.find_incrementing(:first) + assert_equal Company.find(2), Company.find_incrementing(:first) + end + def test_bind_enumerable quoted_abc = %(#{ActiveRecord::Base.connection.quote('a')},#{ActiveRecord::Base.connection.quote('b')},#{ActiveRecord::Base.connection.quote('c')}) diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index 9fa810a..064db97 100755 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -13,6 +13,14 @@ class Company < AbstractCompany def arbitrary_method "I am Jack's profound disappointment" end + + @@count = 0 + def self.find_incrementing(*args) + with_scope(:find => {:conditions => ['companies.id = ?', lambda { @@count = @@count + 1; @@count }]}) do + find(*args) + end + end + end module Namespaced -- 1.5.5.3