diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-04-08 23:54:54 +0100 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-04-12 19:46:05 -0700 |
commit | f0e198bfa1e3f9689e0cde1d194a44027fc90b3c (patch) | |
tree | 2da93eee8e63c088350971c04b80cd673f1b5333 /activerecord/test | |
parent | 788bd30859f3f21184defd240c3d32f179515225 (diff) | |
download | rails-f0e198bfa1e3f9689e0cde1d194a44027fc90b3c.tar.gz rails-f0e198bfa1e3f9689e0cde1d194a44027fc90b3c.tar.bz2 rails-f0e198bfa1e3f9689e0cde1d194a44027fc90b3c.zip |
Deprecate defining scopes with a callable (lambda, proc, etc) via the scope class method. Just define a class method yourself instead.
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/named_scope_test.rb | 6 | ||||
-rw-r--r-- | activerecord/test/models/comment.rb | 5 | ||||
-rw-r--r-- | activerecord/test/models/post.rb | 22 | ||||
-rw-r--r-- | activerecord/test/models/topic.rb | 26 |
4 files changed, 38 insertions, 21 deletions
diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 8fd1fc2577..2880fdc651 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -471,6 +471,12 @@ class NamedScopeTest < ActiveRecord::TestCase require "models/without_table" end end + + def test_scopes_with_callables_are_deprecated + assert_deprecated do + Post.scope :WE_SO_EXCITED, lambda { |partyingpartyingpartying, yeah| fun!.fun!.fun! } + end + end end class DynamicScopeMatchTest < ActiveRecord::TestCase diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb index 2a4c37089a..3bd7db7834 100644 --- a/activerecord/test/models/comment.rb +++ b/activerecord/test/models/comment.rb @@ -1,5 +1,8 @@ class Comment < ActiveRecord::Base - scope :limit_by, lambda {|l| limit(l) } + def self.limit_by(l) + limit(l) + end + scope :containing_the_letter_e, :conditions => "comments.body LIKE '%e%'" scope :not_again, where("comments.body NOT LIKE '%again%'") scope :for_first_post, :conditions => { :post_id => 1 } diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 632f83e5d4..7055380d85 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -7,12 +7,15 @@ class Post < ActiveRecord::Base scope :containing_the_letter_a, where("body LIKE '%a%'") scope :ranked_by_comments, order("comments_count DESC") - scope :limit_by, lambda {|l| limit(l) } - scope :with_authors_at_address, lambda { |address| { - :conditions => [ 'authors.author_address_id = ?', address.id ], - :joins => 'JOIN authors ON authors.id = posts.author_id' - } - } + + def self.limit_by(l) + limit(l) + end + + def self.with_authors_at_address(address) + where('authors.author_address_id = ?', address.id) + .joins('JOIN authors ON authors.id = posts.author_id') + end belongs_to :author do def greeting @@ -27,9 +30,10 @@ class Post < ActiveRecord::Base scope :with_special_comments, :joins => :comments, :conditions => {:comments => {:type => 'SpecialComment'} } scope :with_very_special_comments, joins(:comments).where(:comments => {:type => 'VerySpecialComment'}) - scope :with_post, lambda {|post_id| - { :joins => :comments, :conditions => {:comments => {:post_id => post_id} } } - } + + def self.with_post(post_id) + joins(:comments).where(:comments => { :post_id => post_id }) + end has_many :comments do def find_most_recent diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index 6440dbe8ab..60e750e6c4 100644 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -1,10 +1,20 @@ class Topic < ActiveRecord::Base scope :base - scope :written_before, lambda { |time| - if time - { :conditions => ['written_on < ?', time] } - end - } + + ActiveSupport::Deprecation.silence do + scope :written_before, lambda { |time| + if time + { :conditions => ['written_on < ?', time] } + end + } + + scope :with_object, Class.new(Struct.new(:klass)) { + def call + klass.where(:approved => true) + end + }.new(self) + end + scope :approved, :conditions => {:approved => true} scope :rejected, :conditions => {:approved => false} @@ -19,12 +29,6 @@ class Topic < ActiveRecord::Base end end - scope :with_object, Class.new(Struct.new(:klass)) { - def call - klass.where(:approved => true) - end - }.new(self) - module NamedExtension def two 2 |