From 256b363eeecf6d0fb896aabd3fc619e200a5062c Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 17 Apr 2011 20:47:52 +0100 Subject: Revert "Deprecate defining scopes with a callable (lambda, proc, etc) via the scope class method. Just define a class method yourself instead." This reverts commit f0e198bfa1e3f9689e0cde1d194a44027fc90b3c. Conflicts: activerecord/test/models/post.rb --- activerecord/lib/active_record/named_scope.rb | 62 +-------------------------- 1 file changed, 2 insertions(+), 60 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index f1df04950b..60840e6958 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -51,14 +51,6 @@ module ActiveRecord # The above calls to scope define class methods Shirt.red and Shirt.dry_clean_only. Shirt.red, # in effect, represents the query Shirt.where(:color => 'red'). # - # Note that this is simply 'syntactic sugar' for defining an actual class method: - # - # class Shirt < ActiveRecord::Base - # def self.red - # where(:color => 'red') - # end - # end - # # Unlike Shirt.find(...), however, the object returned by Shirt.red is not an Array; it # resembles the association object constructed by a has_many declaration. For instance, # you can invoke Shirt.red.first, Shirt.red.count, Shirt.red.where(:size => 'small'). @@ -82,34 +74,14 @@ module ActiveRecord # then elton.shirts.red.dry_clean_only will return all of Elton's red, dry clean # only shirts. # - # If you need to pass parameters to a scope, define it as a normal method: + # Named \scopes can also be procedural: # # class Shirt < ActiveRecord::Base - # def self.colored(color) - # where(:color => color) - # end + # scope :colored, lambda {|color| where(:color => color) } # end # # In this example, Shirt.colored('puce') finds all puce shirts. # - # Note that scopes defined with \scope will be evaluated when they are defined, rather than - # when they are used. For example, the following would be incorrect: - # - # class Post < ActiveRecord::Base - # scope :recent, where('published_at >= ?', Time.now - 1.week) - # end - # - # The example above would be 'frozen' to the Time.now value when the Post - # class was defined, and so the resultant SQL query would always be the same. The correct - # way to do this would be via a class method, which will re-evaluate the scope each time - # it is called: - # - # class Post < ActiveRecord::Base - # def self.recent - # where('published_at >= ?', Time.now - 1.week) - # end - # end - # # Named \scopes can also have extensions, just as with has_many declarations: # # class Shirt < ActiveRecord::Base @@ -120,18 +92,6 @@ module ActiveRecord # end # end # - # The above could also be written as a class method like so: - # - # class Shirt < ActiveRecord::Base - # def self.red - # where(:color => 'red').extending do - # def dom_id - # 'red_shirts' - # end - # end - # end - # end - # # Scopes can also be used while creating/building a record. # # class Article < ActiveRecord::Base @@ -168,24 +128,6 @@ module ActiveRecord valid_scope_name?(name) extension = Module.new(&Proc.new) if block_given? - if !scope_options.is_a?(Relation) && scope_options.respond_to?(:call) - ActiveSupport::Deprecation.warn <<-WARN -Passing a proc (or other object that responds to #call) to scope is deprecated. If you need your scope to be lazily evaluated, or takes parameters, please define it as a normal class method instead. For example, change this: - -class Post < ActiveRecord::Base - scope :unpublished, lambda { where('published_at > ?', Time.now) } -end - -To this: - -class Post < ActiveRecord::Base - def self.unpublished - where('published_at > ?', Time.now) - end -end - WARN - end - scope_proc = lambda do |*args| options = scope_options.respond_to?(:call) ? scope_options.call(*args) : scope_options options = scoped.apply_finder_options(options) if options.is_a?(Hash) -- cgit v1.2.3