1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge the has_finder gem, renamed as 'named_scope'. Closes #11404 [nkallen]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9084 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Rick Olson 2008-03-24 02:50:02 +00:00
parent 5bf4cbbdb7
commit 081ddb6f24
20 changed files with 369 additions and 39 deletions

View file

@ -1,5 +1,19 @@
*SVN*
* Merge the has_finder gem, renamed as 'named_scope'. #11404 [nkallen]
class Article < ActiveRecord::Base
has_finder :published, :conditions => {:published => true}
has_finder :popular, :conditions => ...
end
Article.published.paginate(:page => 1)
Article.published.popular.count
Article.popular.find(:first)
Article.popular.find(:all, :conditions => {...})
See http://pivots.pivotallabs.com/users/nick/blog/articles/284-hasfinder-it-s-now-easier-than-ever-to-create-complex-re-usable-sql-queries
* Add has_one :through support. #4756 [thechrisoshow]
* Migrations: create_table supports primary_key_prefix_type. #10314 [student, thechrisoshow]

View file

@ -37,6 +37,7 @@ unless defined? ActiveSupport
end
require 'active_record/base'
require 'active_record/named_scope'
require 'active_record/observer'
require 'active_record/query_cache'
require 'active_record/validations'
@ -64,6 +65,7 @@ ActiveRecord::Base.class_eval do
include ActiveRecord::Observing
include ActiveRecord::Timestamp
include ActiveRecord::Associations
include ActiveRecord::NamedScope
include ActiveRecord::AssociationPreload
include ActiveRecord::Aggregations
include ActiveRecord::Transactions

View file

@ -1133,7 +1133,7 @@ module ActiveRecord
end
end
end
def add_multiple_associated_save_callbacks(association_name)
method_name = "validate_associated_records_for_#{association_name}".to_sym
ivar = "@#{association_name}"

View file

@ -41,7 +41,7 @@ module ActiveRecord
delete(@target)
reset_target!
end
# Calculate sum using SQL, not Enumerable
def sum(*args)
if block_given?
@ -168,8 +168,10 @@ module ActiveRecord
else
super
end
else
@reflection.klass.send(:with_scope, construct_scope) do
elsif @reflection.klass.scopes.include?(method)
@reflection.klass.scopes[method].call(self, *args)
else
with_scope(construct_scope) do
if block_given?
@reflection.klass.send(method, *args) { |*block_args| yield(*block_args) }
else

View file

@ -119,6 +119,10 @@ module ActiveRecord
)
end
def with_scope(*args, &block)
@reflection.klass.send :with_scope, *args, &block
end
private
def method_missing(method, *args)
if load_target

View file

@ -167,7 +167,10 @@ module ActiveRecord
def construct_scope
create_scoping = {}
set_belongs_to_association_for(create_scoping)
{ :find => { :conditions => @finder_sql, :readonly => false, :order => @reflection.options[:order], :limit => @reflection.options[:limit] }, :create => create_scoping }
{
:find => { :conditions => @finder_sql, :readonly => false, :order => @reflection.options[:order], :limit => @reflection.options[:limit] },
:create => create_scoping
}
end
end
end

View file

@ -139,8 +139,10 @@ module ActiveRecord
else
super
end
elsif @reflection.klass.scopes.include?(method)
@reflection.klass.scopes[method].call(self, *args)
else
@reflection.klass.send(:with_scope, construct_scope) do
with_scope construct_scope do
if block_given?
@reflection.klass.send(method, *args) { |*block_args| yield(*block_args) }
else

View file

@ -0,0 +1,128 @@
module ActiveRecord
module NamedScope
# All subclasses of ActiveRecord::Base have two named_scopes:
# * <tt>all</tt>, which is similar to a <tt>find(:all)</tt> query, and
# * <tt>scoped</tt>, which allows for the creation of anonymous scopes, on the fly:
#
# Shirt.scoped(:conditions => {:color => 'red'}).scoped(:include => :washing_instructions)
#
# These anonymous scopes tend to be useful when procedurally generating complex queries, where passing
# intermediate values (scopes) around as first-class objects is convenient.
def self.included(base)
base.class_eval do
extend ClassMethods
named_scope :all
named_scope :scoped, lambda { |scope| scope }
end
end
module ClassMethods
def scopes
read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {})
end
# Adds a class method for retrieving and querying objects. A scope represents a narrowing of a database query,
# 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]
# end
#
# The above calls to <tt>named_scope</tt> define class methods <tt>Shirt.red</tt> and <tt>Shirt.dry_clean_only</tt>. <tt>Shirt.red</tt>,
# in effect, represents the query <tt>Shirt.find(:all, :conditions => {:color => 'red'})</tt>.
#
# Unlike Shirt.find(...), however, the object returned by <tt>Shirt.red</tt> is not an Array; it resembles the association object
# constructed by a <tt>has_many</tt> declaration. For instance, you can invoke <tt>Shirt.red.find(:first)</tt>, <tt>Shirt.red.count</tt>,
# <tt>Shirt.red.find(:all, :conditions => {:size => 'small'})</tt>. Also, just
# as with the association objects, name scopes acts like an Array, implementing Enumerable; <tt>Shirt.red.each(&block)</tt>,
# <tt>Shirt.red.first</tt>, and <tt>Shirt.red.inject(memo, &block)</tt> all behave as if Shirt.red really were an Array.
#
# These named scopes are composable. For instance, <tt>Shirt.red.dry_clean_only</tt> will produce all shirts that are both red and dry clean only.
# Nested finds and calculations also work with these compositions: <tt>Shirt.red.dry_clean_only.count</tt> returns the number of garments
# for which these criteria obtain. Similarly with <tt>Shirt.red.dry_clean_only.average(:thread_count)</tt>.
#
# All scopes are available as class methods on the ActiveRecord descendent upon which the scopes were defined. But they are also available to
# <tt>has_many</tt> associations. If,
#
# class Person < ActiveRecord::Base
# has_many :shirts
# end
#
# then <tt>elton.shirts.red.dry_clean_only</tt> will return all of Elton's red, dry clean
# only shirts.
#
# Named scopes can also be procedural.
#
# class Shirt < ActiveRecord::Base
# named_scope :colored, lambda { |color|
# { :conditions => { :color => color } }
# }
# end
#
# In this example, <tt>Shirt.colored('puce')</tt> finds all puce shirts.
#
# 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
# def dom_id
# 'red_shirts'
# end
# end
# end
#
def named_scope(name, options = {}, &block)
scopes[name] = lambda do |parent_scope, *args|
Scope.new(parent_scope, case options
when Hash
options
when Proc
options.call(*args)
end, &block)
end
(class << self; self end).instance_eval do
define_method name do |*args|
scopes[name].call(self, *args)
end
end
end
end
class Scope
attr_reader :proxy_scope, :proxy_options
[].methods.each { |m| delegate m, :to => :proxy_found unless m =~ /(^__|^nil\?|^send|class|extend|find|count|sum|average|maximum|minimum|paginate)/ }
delegate :scopes, :with_scope, :to => :proxy_scope
def initialize(proxy_scope, options, &block)
[options[:extend]].flatten.each { |extension| extend extension } if options[:extend]
extend Module.new(&block) if block_given?
@proxy_scope, @proxy_options = proxy_scope, options.except(:extend)
end
def reload
load_found; self
end
protected
def proxy_found
@found || load_found
end
private
def method_missing(method, *args, &block)
if scopes.include?(method)
scopes[method].call(self, *args)
else
with_scope :find => proxy_options do
proxy_scope.send(method, *args, &block)
end
end
end
def load_found
@found = find(:all)
end
end
end
end

View file

@ -61,16 +61,17 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
def test_eager_association_loading_with_has_many_sti
topics = Topic.find(:all, :include => :replies, :order => 'topics.id')
assert_equal topics(:first, :second), topics
first, second, = topics(:first).replies.size, topics(:second).replies.size
assert_no_queries do
assert_equal 1, topics[0].replies.size
assert_equal 0, topics[1].replies.size
assert_equal first, topics[0].replies.size
assert_equal second, topics[1].replies.size
end
end
def test_eager_association_loading_with_belongs_to_sti
replies = Reply.find(:all, :include => :topic, :order => 'topics.id')
assert_equal [topics(:second)], replies
assert replies.include?(topics(:second))
assert !replies.include?(topics(:first))
assert_equal topics(:first), assert_no_queries { replies.first.topic }
end

View file

@ -477,7 +477,7 @@ class BasicsTest < ActiveRecord::TestCase
def test_load
topics = Topic.find(:all, :order => 'id')
assert_equal(2, topics.size)
assert_equal(4, topics.size)
assert_equal(topics(:first).title, topics.first.title)
end
@ -549,10 +549,11 @@ class BasicsTest < ActiveRecord::TestCase
end
def test_destroy_all
assert_equal 2, Topic.count
Topic.destroy_all "author_name = 'Mary'"
assert_equal 1, Topic.count
original_count = Topic.count
topics_by_mary = Topic.count(:conditions => mary = "author_name = 'Mary'")
Topic.destroy_all mary
assert_equal original_count - topics_by_mary, Topic.count
end
def test_destroy_many
@ -562,8 +563,9 @@ class BasicsTest < ActiveRecord::TestCase
end
def test_delete_many
Topic.delete([1, 2])
assert_equal 0, Topic.count
original_count = Topic.count
Topic.delete(deleting = [1, 2])
assert_equal original_count - deleting.size, Topic.count
end
def test_boolean_attributes
@ -588,21 +590,21 @@ class BasicsTest < ActiveRecord::TestCase
end
def test_update_all
assert_equal 2, Topic.update_all("content = 'bulk updated!'")
assert_equal Topic.count, Topic.update_all("content = 'bulk updated!'")
assert_equal "bulk updated!", Topic.find(1).content
assert_equal "bulk updated!", Topic.find(2).content
assert_equal 2, Topic.update_all(['content = ?', 'bulk updated again!'])
assert_equal Topic.count, Topic.update_all(['content = ?', 'bulk updated again!'])
assert_equal "bulk updated again!", Topic.find(1).content
assert_equal "bulk updated again!", Topic.find(2).content
assert_equal 2, Topic.update_all(['content = ?', nil])
assert_equal Topic.count, Topic.update_all(['content = ?', nil])
assert_nil Topic.find(1).content
end
def test_update_all_with_hash
assert_not_nil Topic.find(1).last_read
assert_equal 2, Topic.update_all(:content => 'bulk updated with hash!', :last_read => nil)
assert_equal Topic.count, Topic.update_all(:content => 'bulk updated with hash!', :last_read => nil)
assert_equal "bulk updated with hash!", Topic.find(1).content
assert_equal "bulk updated with hash!", Topic.find(2).content
assert_nil Topic.find(1).last_read
@ -637,7 +639,9 @@ class BasicsTest < ActiveRecord::TestCase
end
def test_delete_all
assert_equal 2, Topic.delete_all
assert Topic.count > 0
assert_equal Topic.count, Topic.delete_all
end
def test_update_by_condition
@ -804,17 +808,17 @@ class BasicsTest < ActiveRecord::TestCase
def test_update_attributes!
reply = Reply.find(2)
assert_equal "The Second Topic's of the day", reply.title
assert_equal "The Second Topic of the day", reply.title
assert_equal "Have a nice day", reply.content
reply.update_attributes!("title" => "The Second Topic's of the day updated", "content" => "Have a nice evening")
reply.update_attributes!("title" => "The Second Topic of the day updated", "content" => "Have a nice evening")
reply.reload
assert_equal "The Second Topic's of the day updated", reply.title
assert_equal "The Second Topic of the day updated", reply.title
assert_equal "Have a nice evening", reply.content
reply.update_attributes!(:title => "The Second Topic's of the day", :content => "Have a nice day")
reply.update_attributes!(:title => "The Second Topic of the day", :content => "Have a nice day")
reply.reload
assert_equal "The Second Topic's of the day", reply.title
assert_equal "The Second Topic of the day", reply.title
assert_equal "Have a nice day", reply.content
assert_raise(ActiveRecord::RecordInvalid) { reply.update_attributes!(:title => nil, :content => "Have a nice evening") }
@ -1770,7 +1774,7 @@ class BasicsTest < ActiveRecord::TestCase
xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :include => :replies, :except => :replies_count)
assert_equal "<topic>", xml.first(7)
assert xml.include?(%(<replies type="array"><reply>))
assert xml.include?(%(<title>The Second Topic's of the day</title>))
assert xml.include?(%(<title>The Second Topic of the day</title>))
end
def test_array_to_xml_including_has_many_association

View file

@ -542,7 +542,7 @@ class FinderTest < ActiveRecord::TestCase
end
def test_find_all_by_array_attribute
assert_equal 2, Topic.find_all_by_title(["The First Topic", "The Second Topic's of the day"]).size
assert_equal 2, Topic.find_all_by_title(["The First Topic", "The Second Topic of the day"]).size
end
def test_find_all_by_boolean_attribute
@ -551,7 +551,7 @@ class FinderTest < ActiveRecord::TestCase
assert topics.include?(topics(:first))
topics = Topic.find_all_by_approved(true)
assert_equal 1, topics.size
assert_equal 3, topics.size
assert topics.include?(topics(:second))
end
@ -563,8 +563,8 @@ class FinderTest < ActiveRecord::TestCase
def test_find_all_by_nil_attribute
topics = Topic.find_all_by_last_read nil
assert_equal 1, topics.size
assert_nil topics[0].last_read
assert_equal 3, topics.size
assert topics.collect(&:last_read).all?(&:nil?)
end
def test_find_by_nil_and_not_nil_attributes

View file

@ -123,7 +123,7 @@ class FixturesTest < ActiveRecord::TestCase
end
def test_complete_instantiation
assert_equal 2, @topics.size
assert_equal 4, @topics.size
assert_equal "The First Topic", @first.title
end

View file

@ -68,9 +68,9 @@ class LifecycleTest < ActiveRecord::TestCase
fixtures :topics, :developers
def test_before_destroy
assert_equal 2, Topic.count
Topic.find(1).destroy
assert_equal 0, Topic.count
original_count = Topic.count
(topic_to_be_destroyed = Topic.find(1)).destroy
assert_equal original_count - (1 + topic_to_be_destroyed.replies.size), Topic.count
end
def test_after_save

View file

@ -0,0 +1,116 @@
require "cases/helper"
require 'models/post'
require 'models/topic'
require 'models/comment'
require 'models/reply'
require 'models/author'
class NamedScopeTest < ActiveRecord::TestCase
fixtures :posts, :authors, :topics
def test_implements_enumerable
assert !Topic.find(:all).empty?
assert_equal Topic.find(:all), Topic.all
assert_equal Topic.find(:all), Topic.all.collect
assert_equal Topic.find(:first), Topic.all.first
assert_equal Topic.find(:all), Topic.all.each { |i| i }
end
def test_found_items_are_cached
Topic.columns
all_posts = Topic.all
assert_queries(1) do
all_posts.collect
all_posts.collect
end
end
def test_reload_expires_cache_of_found_items
all_posts = Topic.all
all_posts.inspect
new_post = Topic.create!
assert !all_posts.include?(new_post)
assert all_posts.reload.include?(new_post)
end
def test_delegates_finds_and_calculations_to_the_base_class
assert !Topic.find(:all).empty?
assert_equal Topic.find(:all), Topic.all.find(:all)
assert_equal Topic.find(:first), Topic.all.find(:first)
assert_equal Topic.count, Topic.all.count
assert_equal Topic.average(:replies_count), Topic.all.average(:replies_count)
end
def test_subclasses_inherit_scopes
assert Topic.scopes.include?(:all)
assert Reply.scopes.include?(:all)
assert_equal Reply.find(:all), Reply.all
end
def test_scopes_with_options_limit_finds_to_those_matching_the_criteria_specified
assert !Topic.find(:all, :conditions => {:approved => true}).empty?
assert_equal Topic.find(:all, :conditions => {:approved => true}), Topic.approved
assert_equal Topic.count(:conditions => {:approved => true}), Topic.approved.count
end
def test_scopes_are_composable
assert_equal (approved = Topic.find(:all, :conditions => {:approved => true})), Topic.approved
assert_equal (replied = Topic.find(:all, :conditions => 'replies_count > 0')), Topic.replied
assert !(approved == replied)
assert !(approved & replied).empty?
assert_equal approved & replied, Topic.approved.replied
end
def test_procedural_scopes
topics_written_before_the_third = Topic.find(:all, :conditions => ['written_on < ?', topics(:third).written_on])
topics_written_before_the_second = Topic.find(:all, :conditions => ['written_on < ?', topics(:second).written_on])
assert_not_equal topics_written_before_the_second, topics_written_before_the_third
assert_equal topics_written_before_the_third, Topic.written_before(topics(:third).written_on)
assert_equal topics_written_before_the_second, Topic.written_before(topics(:second).written_on)
end
def test_extensions
assert_equal 1, Topic.anonymous_extension.one
assert_equal 2, Topic.named_extension.two
end
def test_multiple_extensions
assert_equal 2, Topic.multiple_extensions.extension_two
assert_equal 1, Topic.multiple_extensions.extension_one
end
def test_has_many_associations_have_access_to_named_scopes
assert_not_equal Post.containing_the_letter_a, authors(:david).posts
assert !Post.containing_the_letter_a.empty?
assert_equal authors(:david).posts & Post.containing_the_letter_a, authors(:david).posts.containing_the_letter_a
end
def test_has_many_through_associations_have_access_to_named_scopes
assert_not_equal Comment.containing_the_letter_e, authors(:david).posts
assert !Comment.containing_the_letter_e.empty?
assert_equal authors(:david).comments & Comment.containing_the_letter_e, authors(:david).comments.containing_the_letter_e
end
def test_active_records_have_scope_named__all__
assert !Topic.find(:all).empty?
assert_equal Topic.find(:all), Topic.all
end
def test_active_records_have_scope_named__scoped__
assert !Topic.find(:all, scope = {:conditions => "content LIKE '%Have%'"}).empty?
assert_equal Topic.find(:all, scope), Topic.scoped(scope)
end
end

View file

@ -428,7 +428,7 @@ class ValidationsTest < ActiveRecord::TestCase
assert_nil t2.errors.on(:title)
assert t2.errors.on(:parent_id)
t2.parent_id = 3
t2.parent_id = 4
assert t2.save, "Should now save t2 as unique"
t2.parent_id = nil

View file

@ -12,11 +12,31 @@ first:
second:
id: 2
title: The Second Topic's of the day
title: The Second Topic of the day
author_name: Mary
written_on: 2003-07-15t15:28:00.0099+01:00
written_on: 2004-07-15t15:28:00.0099+01:00
content: Have a nice day
approved: true
replies_count: 0
parent_id: 1
type: Reply
third:
id: 3
title: The Third Topic of the day
author_name: Nick
written_on: 2005-07-15t15:28:00.0099+01:00
content: I'm a troll
approved: true
replies_count: 1
fourth:
id: 4
title: The Fourth Topic of the day
author_name: Carl
written_on: 2006-07-15t15:28:00.0099+01:00
content: Why not?
approved: true
type: Reply
parent_id: 3

View file

@ -3,6 +3,7 @@ class Author < ActiveRecord::Base
has_many :posts_with_comments, :include => :comments, :class_name => "Post"
has_many :posts_with_categories, :include => :categories, :class_name => "Post"
has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post"
has_many :posts_containing_the_letter_a, :class_name => "Post"
has_many :posts_with_extension, :class_name => "Post" do #, :extend => ProxyTestExtension
def testing_proxy_owner
proxy_owner
@ -15,6 +16,7 @@ class Author < ActiveRecord::Base
end
end
has_many :comments, :through => :posts
has_many :comments_containing_the_letter_e, :through => :posts, :source => :comments
has_many :comments_desc, :through => :posts, :source => :comments, :order => 'comments.id DESC'
has_many :limited_comments, :through => :posts, :source => :comments, :limit => 1
has_many :funky_comments, :through => :posts, :source => :comments

View file

@ -1,4 +1,6 @@
class Comment < ActiveRecord::Base
named_scope :containing_the_letter_e, :conditions => "comments.body LIKE '%e%'"
belongs_to :post, :counter_cache => true
def self.what_are_you

View file

@ -1,4 +1,6 @@
class Post < ActiveRecord::Base
named_scope :containing_the_letter_a, :conditions => "body LIKE '%a%'"
belongs_to :author do
def greeting
"hello"

View file

@ -1,4 +1,32 @@
class Topic < ActiveRecord::Base
named_scope :written_before, lambda { |time|
{ :conditions => ['written_on < ?', time] }
}
named_scope :approved, :conditions => {:approved => true}
named_scope :replied, :conditions => ['replies_count > 0']
named_scope :anonymous_extension do
def one
1
end
end
module NamedExtension
def two
2
end
end
module MultipleExtensionOne
def extension_one
1
end
end
module MultipleExtensionTwo
def extension_two
2
end
end
named_scope :named_extension, :extend => NamedExtension
named_scope :multiple_extensions, :extend => [MultipleExtensionTwo, MultipleExtensionOne]
has_many :replies, :dependent => :destroy, :foreign_key => "parent_id"
serialize :content