From 9cc16e26fb0052578e274e9680b2e1e98da38ba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Wro=CC=81bel?= Date: Thu, 20 Jan 2011 01:11:17 +0100 Subject: [PATCH] Allow default_scope to accept lambdas. Previous implementation was throwing exceptions when default_scope was called multiple times for the same model. This fix uses the old finder merging for non-lambda arguments and recursive lambdas when necessary. More info in ticket #1812 on lighthouse. --- activerecord/lib/active_record/base.rb | 15 ++++++++++++++- 1 files changed, 14 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 47dc2d4..c94c0c9 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1140,8 +1140,21 @@ MSG # Article.create.published # => true def default_scope(options = {}) reset_scoped_methods + default_scoping = self.default_scoping.dup - self.default_scoping = default_scoping << construct_finder_arel(options, default_scoping.pop) + previous = default_scoping.pop + + if previous.kind_of?(Proc) or options.kind_of?(Proc) + new_default_scope = lambda do + sane_options = options.kind_of?(Proc) ? options.call : options + sane_previous = previous.kind_of?(Proc) ? previous.call : previous + construct_finder_arel sane_options, sane_previous + end + else + new_default_scope = construct_finder_arel options, previous + end + + self.default_scoping = default_scoping << new_default_scope end def current_scoped_methods #:nodoc: -- 1.7.3.5