From 019cd51a3f36ec7631bf1b63c069e62a3b5185d4 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Mon, 18 Apr 2011 23:35:22 +0100 Subject: Bring back support for passing a callable object to the default_scope macro. You can also just use a block. --- activerecord/lib/active_record/base.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'activerecord/lib/active_record/base.rb') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 5b316c17be..9a01d793f9 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1196,6 +1196,15 @@ MSG # Article.new.published # => true # Article.create.published # => true # + # You can also use default_scope with a block, in order to have it lazily evaluated: + # + # class Article < ActiveRecord::Base + # default_scope { where(:published_at => Time.now - 1.week) } + # end + # + # (You can also pass any object which responds to call to the default_scope + # macro, and it will be called when building the default scope.) + # # If you need to do more complex things with a default scope, you can alternatively # define it as a class method: # @@ -1233,6 +1242,7 @@ end WARN end + scope = Proc.new if block_given? self.default_scopes = default_scopes.dup << scope end @@ -1245,6 +1255,8 @@ end default_scopes.inject(relation) do |default_scope, scope| if scope.is_a?(Hash) default_scope.apply_finder_options(scope) + elsif !scope.is_a?(Relation) && scope.respond_to?(:call) + default_scope.merge(scope.call) else default_scope.merge(scope) end -- cgit v1.2.3