diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 38bcf0c787..f6b2ef553e 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Rename named_scope to scope. [Pratik Naik] + * Changed ActiveRecord::Base.store_full_sti_class to be true by default reflecting the previously announced Rails 3 default [DHH] * Add Relation#except. [Pratik Naik] diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 16fde1ffb8..42fc6c5f28 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -38,11 +38,11 @@ module ActiveRecord # such as :conditions => {:color => :red}, :select => 'shirts.*', :include => :washing_instructions. # # class Shirt < ActiveRecord::Base - # named_scope :red, :conditions => {:color => 'red'} - # named_scope :dry_clean_only, :joins => :washing_instructions, :conditions => ['washing_instructions.dry_clean_only = ?', true] + # scope :red, :conditions => {:color => 'red'} + # scope :dry_clean_only, :joins => :washing_instructions, :conditions => ['washing_instructions.dry_clean_only = ?', true] # end # - # The above calls to named_scope define class methods Shirt.red and Shirt.dry_clean_only. Shirt.red, + # The above calls to scope define class methods Shirt.red and Shirt.dry_clean_only. Shirt.red, # in effect, represents the query Shirt.find(:all, :conditions => {:color => 'red'}). # # Unlike Shirt.find(...), however, the object returned by Shirt.red is not an Array; it resembles the association object @@ -68,7 +68,7 @@ module ActiveRecord # Named \scopes can also be procedural: # # class Shirt < ActiveRecord::Base - # named_scope :colored, lambda { |color| + # scope :colored, lambda { |color| # { :conditions => { :color => color } } # } # end @@ -78,7 +78,7 @@ module ActiveRecord # Named \scopes can also have extensions, just as with has_many declarations: # # class Shirt < ActiveRecord::Base - # named_scope :red, :conditions => {:color => 'red'} do + # scope :red, :conditions => {:color => 'red'} do # def dom_id # 'red_shirts' # end @@ -90,14 +90,14 @@ module ActiveRecord # proxy_options method on the proxy itself. # # class Shirt < ActiveRecord::Base - # named_scope :colored, lambda { |color| + # scope :colored, lambda { |color| # { :conditions => { :color => color } } # } # end # # expected_options = { :conditions => { :colored => 'red' } } # assert_equal expected_options, Shirt.colored('red').proxy_options - def named_scope(name, options = {}, &block) + def scope(name, options = {}, &block) name = name.to_sym if !scopes[name] && respond_to?(name, true) @@ -118,6 +118,11 @@ module ActiveRecord end end end + + def named_scope(*args, &block) + ActiveSupport::Deprecation.warn("Base#named_scope has been deprecated, please use Base.scope instead.", caller) + scope(*args, &block) + end end class Scope < Relation diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index ce1ac845cd..3e2bd58f9a 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -372,9 +372,13 @@ class NamedScopeTest < ActiveRecord::TestCase def test_named_scopes_with_reserved_names [:where, :with_scope].each do |protected_method| - assert_raises(ArgumentError) { Topic.named_scope protected_method } + assert_raises(ArgumentError) { Topic.scope protected_method } end end + + def test_deprecated_named_scope_method + assert_deprecated('named_scope has been deprecated') { Topic.named_scope :deprecated_named_scope } + end end class DynamicScopeMatchTest < ActiveRecord::TestCase diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb index 399dea9f12..a8a99f6dce 100644 --- a/activerecord/test/models/comment.rb +++ b/activerecord/test/models/comment.rb @@ -1,7 +1,7 @@ class Comment < ActiveRecord::Base - named_scope :containing_the_letter_e, :conditions => "comments.body LIKE '%e%'" - named_scope :for_first_post, :conditions => { :post_id => 1 } - named_scope :for_first_author, + scope :containing_the_letter_e, :conditions => "comments.body LIKE '%e%'" + scope :for_first_post, :conditions => { :post_id => 1 } + scope :for_first_author, :joins => :post, :conditions => { "posts.author_id" => 1 } diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index 058970336b..e7a1e110d7 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -43,7 +43,7 @@ class Developer < ActiveRecord::Base has_many :audit_logs - named_scope :jamises, :conditions => {:name => 'Jamis'} + scope :jamises, :conditions => {:name => 'Jamis'} validates_inclusion_of :salary, :in => 50000..200000 validates_length_of :name, :within => 3..20 @@ -81,7 +81,7 @@ end class DeveloperOrderedBySalary < ActiveRecord::Base self.table_name = 'developers' default_scope :order => 'salary DESC' - named_scope :by_name, :order => 'name DESC' + scope :by_name, :order => 'name DESC' def self.all_ordered_by_name with_scope(:find => { :order => 'name DESC' }) do diff --git a/activerecord/test/models/organization.rb b/activerecord/test/models/organization.rb index c85726169e..1da342a0bd 100644 --- a/activerecord/test/models/organization.rb +++ b/activerecord/test/models/organization.rb @@ -2,5 +2,5 @@ class Organization < ActiveRecord::Base has_many :member_details has_many :members, :through => :member_details - named_scope :clubs, { :from => 'clubs' } + scope :clubs, { :from => 'clubs' } end \ No newline at end of file diff --git a/activerecord/test/models/person.rb b/activerecord/test/models/person.rb index 57fa6418f1..2a73b1ee01 100644 --- a/activerecord/test/models/person.rb +++ b/activerecord/test/models/person.rb @@ -12,6 +12,6 @@ class Person < ActiveRecord::Base has_many :agents, :class_name => 'Person', :foreign_key => 'primary_contact_id' belongs_to :number1_fan, :class_name => 'Person' - named_scope :males, :conditions => { :gender => 'M' } - named_scope :females, :conditions => { :gender => 'F' } + scope :males, :conditions => { :gender => 'M' } + scope :females, :conditions => { :gender => 'F' } end diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index b5f9328139..939df7549b 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -1,8 +1,8 @@ class Post < ActiveRecord::Base - named_scope :containing_the_letter_a, where("body LIKE '%a%'") - named_scope :ranked_by_comments, order("comments_count DESC") - named_scope :limit_by, lambda {|l| limit(l) } - named_scope :with_authors_at_address, lambda { |address| { + 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' } @@ -19,9 +19,9 @@ class Post < ActiveRecord::Base has_one :last_comment, :class_name => 'Comment', :order => 'id desc' - named_scope :with_special_comments, :joins => :comments, :conditions => {:comments => {:type => 'SpecialComment'} } - named_scope :with_very_special_comments, joins(:comments).where(:comments => {:type => 'VerySpecialComment'}) - named_scope :with_post, lambda {|post_id| + 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} } } } diff --git a/activerecord/test/models/reply.rb b/activerecord/test/models/reply.rb index f1ba45b528..264a49b465 100644 --- a/activerecord/test/models/reply.rb +++ b/activerecord/test/models/reply.rb @@ -1,7 +1,7 @@ require 'models/topic' class Reply < Topic - named_scope :base + scope :base belongs_to :topic, :foreign_key => "parent_id", :counter_cache => true belongs_to :topic_with_primary_key, :class_name => "Topic", :primary_key => "title", :foreign_key => "parent_title", :counter_cache => "replies_count" diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index baca4972cb..91fc7c9416 100644 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -1,19 +1,19 @@ class Topic < ActiveRecord::Base - named_scope :base - named_scope :written_before, lambda { |time| + scope :base + scope :written_before, lambda { |time| if time { :conditions => ['written_on < ?', time] } end } - named_scope :approved, :conditions => {:approved => true} - named_scope :rejected, :conditions => {:approved => false} + scope :approved, :conditions => {:approved => true} + scope :rejected, :conditions => {:approved => false} - named_scope :by_lifo, :conditions => {:author_name => 'lifo'} + scope :by_lifo, :conditions => {:author_name => 'lifo'} - named_scope :approved_as_hash_condition, :conditions => {:topics => {:approved => true}} - named_scope 'approved_as_string', :conditions => {:approved => true} - named_scope :replied, :conditions => ['replies_count > 0'] - named_scope :anonymous_extension do + scope :approved_as_hash_condition, :conditions => {:topics => {:approved => true}} + scope 'approved_as_string', :conditions => {:approved => true} + scope :replied, :conditions => ['replies_count > 0'] + scope :anonymous_extension do def one 1 end @@ -33,8 +33,8 @@ class Topic < ActiveRecord::Base 2 end end - named_scope :named_extension, :extend => NamedExtension - named_scope :multiple_extensions, :extend => [MultipleExtensionTwo, MultipleExtensionOne] + scope :named_extension, :extend => NamedExtension + scope :multiple_extensions, :extend => [MultipleExtensionTwo, MultipleExtensionOne] has_many :replies, :dependent => :destroy, :foreign_key => "parent_id" has_many :replies_with_primary_key, :class_name => "Reply", :dependent => :destroy, :primary_key => "title", :foreign_key => "parent_title"