mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Rename named_scope to scope
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
*Edge*
|
*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]
|
* 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]
|
* Add Relation#except. [Pratik Naik]
|
||||||
|
|||||||
@@ -38,11 +38,11 @@ module ActiveRecord
|
|||||||
# such as <tt>:conditions => {:color => :red}, :select => 'shirts.*', :include => :washing_instructions</tt>.
|
# such as <tt>:conditions => {:color => :red}, :select => 'shirts.*', :include => :washing_instructions</tt>.
|
||||||
#
|
#
|
||||||
# class Shirt < ActiveRecord::Base
|
# class Shirt < ActiveRecord::Base
|
||||||
# named_scope :red, :conditions => {:color => 'red'}
|
# scope :red, :conditions => {:color => 'red'}
|
||||||
# named_scope :dry_clean_only, :joins => :washing_instructions, :conditions => ['washing_instructions.dry_clean_only = ?', true]
|
# scope :dry_clean_only, :joins => :washing_instructions, :conditions => ['washing_instructions.dry_clean_only = ?', true]
|
||||||
# end
|
# 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>.
|
# 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
|
# 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:
|
# Named \scopes can also be procedural:
|
||||||
#
|
#
|
||||||
# class Shirt < ActiveRecord::Base
|
# class Shirt < ActiveRecord::Base
|
||||||
# named_scope :colored, lambda { |color|
|
# scope :colored, lambda { |color|
|
||||||
# { :conditions => { :color => color } }
|
# { :conditions => { :color => color } }
|
||||||
# }
|
# }
|
||||||
# end
|
# end
|
||||||
@@ -78,7 +78,7 @@ module ActiveRecord
|
|||||||
# Named \scopes can also have extensions, just as with <tt>has_many</tt> declarations:
|
# Named \scopes can also have extensions, just as with <tt>has_many</tt> declarations:
|
||||||
#
|
#
|
||||||
# class Shirt < ActiveRecord::Base
|
# class Shirt < ActiveRecord::Base
|
||||||
# named_scope :red, :conditions => {:color => 'red'} do
|
# scope :red, :conditions => {:color => 'red'} do
|
||||||
# def dom_id
|
# def dom_id
|
||||||
# 'red_shirts'
|
# 'red_shirts'
|
||||||
# end
|
# end
|
||||||
@@ -90,14 +90,14 @@ module ActiveRecord
|
|||||||
# <tt>proxy_options</tt> method on the proxy itself.
|
# <tt>proxy_options</tt> method on the proxy itself.
|
||||||
#
|
#
|
||||||
# class Shirt < ActiveRecord::Base
|
# class Shirt < ActiveRecord::Base
|
||||||
# named_scope :colored, lambda { |color|
|
# scope :colored, lambda { |color|
|
||||||
# { :conditions => { :color => color } }
|
# { :conditions => { :color => color } }
|
||||||
# }
|
# }
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# expected_options = { :conditions => { :colored => 'red' } }
|
# expected_options = { :conditions => { :colored => 'red' } }
|
||||||
# assert_equal expected_options, Shirt.colored('red').proxy_options
|
# assert_equal expected_options, Shirt.colored('red').proxy_options
|
||||||
def named_scope(name, options = {}, &block)
|
def scope(name, options = {}, &block)
|
||||||
name = name.to_sym
|
name = name.to_sym
|
||||||
|
|
||||||
if !scopes[name] && respond_to?(name, true)
|
if !scopes[name] && respond_to?(name, true)
|
||||||
@@ -118,6 +118,11 @@ module ActiveRecord
|
|||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
class Scope < Relation
|
class Scope < Relation
|
||||||
|
|||||||
@@ -372,9 +372,13 @@ class NamedScopeTest < ActiveRecord::TestCase
|
|||||||
|
|
||||||
def test_named_scopes_with_reserved_names
|
def test_named_scopes_with_reserved_names
|
||||||
[:where, :with_scope].each do |protected_method|
|
[:where, :with_scope].each do |protected_method|
|
||||||
assert_raises(ArgumentError) { Topic.named_scope protected_method }
|
assert_raises(ArgumentError) { Topic.scope protected_method }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_deprecated_named_scope_method
|
||||||
|
assert_deprecated('named_scope has been deprecated') { Topic.named_scope :deprecated_named_scope }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class DynamicScopeMatchTest < ActiveRecord::TestCase
|
class DynamicScopeMatchTest < ActiveRecord::TestCase
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
class Comment < ActiveRecord::Base
|
class Comment < ActiveRecord::Base
|
||||||
named_scope :containing_the_letter_e, :conditions => "comments.body LIKE '%e%'"
|
scope :containing_the_letter_e, :conditions => "comments.body LIKE '%e%'"
|
||||||
named_scope :for_first_post, :conditions => { :post_id => 1 }
|
scope :for_first_post, :conditions => { :post_id => 1 }
|
||||||
named_scope :for_first_author,
|
scope :for_first_author,
|
||||||
:joins => :post,
|
:joins => :post,
|
||||||
:conditions => { "posts.author_id" => 1 }
|
:conditions => { "posts.author_id" => 1 }
|
||||||
|
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ class Developer < ActiveRecord::Base
|
|||||||
|
|
||||||
has_many :audit_logs
|
has_many :audit_logs
|
||||||
|
|
||||||
named_scope :jamises, :conditions => {:name => 'Jamis'}
|
scope :jamises, :conditions => {:name => 'Jamis'}
|
||||||
|
|
||||||
validates_inclusion_of :salary, :in => 50000..200000
|
validates_inclusion_of :salary, :in => 50000..200000
|
||||||
validates_length_of :name, :within => 3..20
|
validates_length_of :name, :within => 3..20
|
||||||
@@ -81,7 +81,7 @@ end
|
|||||||
class DeveloperOrderedBySalary < ActiveRecord::Base
|
class DeveloperOrderedBySalary < ActiveRecord::Base
|
||||||
self.table_name = 'developers'
|
self.table_name = 'developers'
|
||||||
default_scope :order => 'salary DESC'
|
default_scope :order => 'salary DESC'
|
||||||
named_scope :by_name, :order => 'name DESC'
|
scope :by_name, :order => 'name DESC'
|
||||||
|
|
||||||
def self.all_ordered_by_name
|
def self.all_ordered_by_name
|
||||||
with_scope(:find => { :order => 'name DESC' }) do
|
with_scope(:find => { :order => 'name DESC' }) do
|
||||||
|
|||||||
@@ -2,5 +2,5 @@ class Organization < ActiveRecord::Base
|
|||||||
has_many :member_details
|
has_many :member_details
|
||||||
has_many :members, :through => :member_details
|
has_many :members, :through => :member_details
|
||||||
|
|
||||||
named_scope :clubs, { :from => 'clubs' }
|
scope :clubs, { :from => 'clubs' }
|
||||||
end
|
end
|
||||||
@@ -12,6 +12,6 @@ class Person < ActiveRecord::Base
|
|||||||
has_many :agents, :class_name => 'Person', :foreign_key => 'primary_contact_id'
|
has_many :agents, :class_name => 'Person', :foreign_key => 'primary_contact_id'
|
||||||
belongs_to :number1_fan, :class_name => 'Person'
|
belongs_to :number1_fan, :class_name => 'Person'
|
||||||
|
|
||||||
named_scope :males, :conditions => { :gender => 'M' }
|
scope :males, :conditions => { :gender => 'M' }
|
||||||
named_scope :females, :conditions => { :gender => 'F' }
|
scope :females, :conditions => { :gender => 'F' }
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
class Post < ActiveRecord::Base
|
class Post < ActiveRecord::Base
|
||||||
named_scope :containing_the_letter_a, where("body LIKE '%a%'")
|
scope :containing_the_letter_a, where("body LIKE '%a%'")
|
||||||
named_scope :ranked_by_comments, order("comments_count DESC")
|
scope :ranked_by_comments, order("comments_count DESC")
|
||||||
named_scope :limit_by, lambda {|l| limit(l) }
|
scope :limit_by, lambda {|l| limit(l) }
|
||||||
named_scope :with_authors_at_address, lambda { |address| {
|
scope :with_authors_at_address, lambda { |address| {
|
||||||
:conditions => [ 'authors.author_address_id = ?', address.id ],
|
:conditions => [ 'authors.author_address_id = ?', address.id ],
|
||||||
:joins => 'JOIN authors ON authors.id = posts.author_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'
|
has_one :last_comment, :class_name => 'Comment', :order => 'id desc'
|
||||||
|
|
||||||
named_scope :with_special_comments, :joins => :comments, :conditions => {:comments => {:type => 'SpecialComment'} }
|
scope :with_special_comments, :joins => :comments, :conditions => {:comments => {:type => 'SpecialComment'} }
|
||||||
named_scope :with_very_special_comments, joins(:comments).where(:comments => {:type => 'VerySpecialComment'})
|
scope :with_very_special_comments, joins(:comments).where(:comments => {:type => 'VerySpecialComment'})
|
||||||
named_scope :with_post, lambda {|post_id|
|
scope :with_post, lambda {|post_id|
|
||||||
{ :joins => :comments, :conditions => {:comments => {:post_id => post_id} } }
|
{ :joins => :comments, :conditions => {:comments => {:post_id => post_id} } }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
require 'models/topic'
|
require 'models/topic'
|
||||||
|
|
||||||
class Reply < Topic
|
class Reply < Topic
|
||||||
named_scope :base
|
scope :base
|
||||||
|
|
||||||
belongs_to :topic, :foreign_key => "parent_id", :counter_cache => true
|
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"
|
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
|
class Topic < ActiveRecord::Base
|
||||||
named_scope :base
|
scope :base
|
||||||
named_scope :written_before, lambda { |time|
|
scope :written_before, lambda { |time|
|
||||||
if time
|
if time
|
||||||
{ :conditions => ['written_on < ?', time] }
|
{ :conditions => ['written_on < ?', time] }
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
named_scope :approved, :conditions => {:approved => true}
|
scope :approved, :conditions => {:approved => true}
|
||||||
named_scope :rejected, :conditions => {:approved => false}
|
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}}
|
scope :approved_as_hash_condition, :conditions => {:topics => {:approved => true}}
|
||||||
named_scope 'approved_as_string', :conditions => {:approved => true}
|
scope 'approved_as_string', :conditions => {:approved => true}
|
||||||
named_scope :replied, :conditions => ['replies_count > 0']
|
scope :replied, :conditions => ['replies_count > 0']
|
||||||
named_scope :anonymous_extension do
|
scope :anonymous_extension do
|
||||||
def one
|
def one
|
||||||
1
|
1
|
||||||
end
|
end
|
||||||
@@ -33,8 +33,8 @@ class Topic < ActiveRecord::Base
|
|||||||
2
|
2
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
named_scope :named_extension, :extend => NamedExtension
|
scope :named_extension, :extend => NamedExtension
|
||||||
named_scope :multiple_extensions, :extend => [MultipleExtensionTwo, MultipleExtensionOne]
|
scope :multiple_extensions, :extend => [MultipleExtensionTwo, MultipleExtensionOne]
|
||||||
|
|
||||||
has_many :replies, :dependent => :destroy, :foreign_key => "parent_id"
|
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"
|
has_many :replies_with_primary_key, :class_name => "Reply", :dependent => :destroy, :primary_key => "title", :foreign_key => "parent_title"
|
||||||
|
|||||||
Reference in New Issue
Block a user