mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Rename named_scope to scope
This commit is contained in:
parent
e1d507c7fb
commit
d60bb0a9e4
10 changed files with 46 additions and 35 deletions
|
@ -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]
|
||||
|
|
|
@ -38,11 +38,11 @@ module ActiveRecord
|
|||
# such as <tt>:conditions => {:color => :red}, :select => 'shirts.*', :include => :washing_instructions</tt>.
|
||||
#
|
||||
# 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 <tt>named_scope</tt> define class methods Shirt.red and Shirt.dry_clean_only. Shirt.red,
|
||||
# The above calls to <tt>scope</tt> define class methods Shirt.red and Shirt.dry_clean_only. Shirt.red,
|
||||
# in effect, represents the query <tt>Shirt.find(:all, :conditions => {:color => 'red'})</tt>.
|
||||
#
|
||||
# Unlike <tt>Shirt.find(...)</tt>, 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 <tt>has_many</tt> 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
|
|||
# <tt>proxy_options</tt> 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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 }
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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} } }
|
||||
}
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue