Chris@909
|
1 # Taken from Rails 2.1
|
Chris@909
|
2 module CollectiveIdea #:nodoc:
|
Chris@909
|
3 module NamedScope #:nodoc:
|
Chris@909
|
4 # All subclasses of ActiveRecord::Base have two named_scopes:
|
Chris@909
|
5 # * <tt>all</tt>, which is similar to a <tt>find(:all)</tt> query, and
|
Chris@909
|
6 # * <tt>scoped</tt>, which allows for the creation of anonymous scopes, on the fly:
|
Chris@909
|
7 #
|
Chris@909
|
8 # Shirt.scoped(:conditions => {:color => 'red'}).scoped(:include => :washing_instructions)
|
Chris@909
|
9 #
|
Chris@909
|
10 # These anonymous scopes tend to be useful when procedurally generating complex queries, where passing
|
Chris@909
|
11 # intermediate values (scopes) around as first-class objects is convenient.
|
Chris@909
|
12 def self.included(base)
|
Chris@909
|
13 base.class_eval do
|
Chris@909
|
14 extend ClassMethods
|
Chris@909
|
15 named_scope :scoped, lambda { |scope| scope }
|
Chris@909
|
16 end
|
Chris@909
|
17 end
|
Chris@909
|
18
|
Chris@909
|
19 module ClassMethods #:nodoc:
|
Chris@909
|
20 def scopes
|
Chris@909
|
21 read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {})
|
Chris@909
|
22 end
|
Chris@909
|
23
|
Chris@909
|
24 # Adds a class method for retrieving and querying objects. A scope represents a narrowing of a database query,
|
Chris@909
|
25 # such as <tt>:conditions => {:color => :red}, :select => 'shirts.*', :include => :washing_instructions</tt>.
|
Chris@909
|
26 #
|
Chris@909
|
27 # class Shirt < ActiveRecord::Base
|
Chris@909
|
28 # named_scope :red, :conditions => {:color => 'red'}
|
Chris@909
|
29 # named_scope :dry_clean_only, :joins => :washing_instructions, :conditions => ['washing_instructions.dry_clean_only = ?', true]
|
Chris@909
|
30 # end
|
Chris@909
|
31 #
|
Chris@909
|
32 # 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>,
|
Chris@909
|
33 # in effect, represents the query <tt>Shirt.find(:all, :conditions => {:color => 'red'})</tt>.
|
Chris@909
|
34 #
|
Chris@909
|
35 # Unlike Shirt.find(...), however, the object returned by <tt>Shirt.red</tt> is not an Array; it resembles the association object
|
Chris@909
|
36 # constructed by a <tt>has_many</tt> declaration. For instance, you can invoke <tt>Shirt.red.find(:first)</tt>, <tt>Shirt.red.count</tt>,
|
Chris@909
|
37 # <tt>Shirt.red.find(:all, :conditions => {:size => 'small'})</tt>. Also, just
|
Chris@909
|
38 # as with the association objects, name scopes acts like an Array, implementing Enumerable; <tt>Shirt.red.each(&block)</tt>,
|
Chris@909
|
39 # <tt>Shirt.red.first</tt>, and <tt>Shirt.red.inject(memo, &block)</tt> all behave as if Shirt.red really were an Array.
|
Chris@909
|
40 #
|
Chris@909
|
41 # 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.
|
Chris@909
|
42 # Nested finds and calculations also work with these compositions: <tt>Shirt.red.dry_clean_only.count</tt> returns the number of garments
|
Chris@909
|
43 # for which these criteria obtain. Similarly with <tt>Shirt.red.dry_clean_only.average(:thread_count)</tt>.
|
Chris@909
|
44 #
|
Chris@909
|
45 # All scopes are available as class methods on the ActiveRecord descendent upon which the scopes were defined. But they are also available to
|
Chris@909
|
46 # <tt>has_many</tt> associations. If,
|
Chris@909
|
47 #
|
Chris@909
|
48 # class Person < ActiveRecord::Base
|
Chris@909
|
49 # has_many :shirts
|
Chris@909
|
50 # end
|
Chris@909
|
51 #
|
Chris@909
|
52 # then <tt>elton.shirts.red.dry_clean_only</tt> will return all of Elton's red, dry clean
|
Chris@909
|
53 # only shirts.
|
Chris@909
|
54 #
|
Chris@909
|
55 # Named scopes can also be procedural.
|
Chris@909
|
56 #
|
Chris@909
|
57 # class Shirt < ActiveRecord::Base
|
Chris@909
|
58 # named_scope :colored, lambda { |color|
|
Chris@909
|
59 # { :conditions => { :color => color } }
|
Chris@909
|
60 # }
|
Chris@909
|
61 # end
|
Chris@909
|
62 #
|
Chris@909
|
63 # In this example, <tt>Shirt.colored('puce')</tt> finds all puce shirts.
|
Chris@909
|
64 #
|
Chris@909
|
65 # Named scopes can also have extensions, just as with <tt>has_many</tt> declarations:
|
Chris@909
|
66 #
|
Chris@909
|
67 # class Shirt < ActiveRecord::Base
|
Chris@909
|
68 # named_scope :red, :conditions => {:color => 'red'} do
|
Chris@909
|
69 # def dom_id
|
Chris@909
|
70 # 'red_shirts'
|
Chris@909
|
71 # end
|
Chris@909
|
72 # end
|
Chris@909
|
73 # end
|
Chris@909
|
74 #
|
Chris@909
|
75 #
|
Chris@909
|
76 # For testing complex named scopes, you can examine the scoping options using the
|
Chris@909
|
77 # <tt>proxy_options</tt> method on the proxy itself.
|
Chris@909
|
78 #
|
Chris@909
|
79 # class Shirt < ActiveRecord::Base
|
Chris@909
|
80 # named_scope :colored, lambda { |color|
|
Chris@909
|
81 # { :conditions => { :color => color } }
|
Chris@909
|
82 # }
|
Chris@909
|
83 # end
|
Chris@909
|
84 #
|
Chris@909
|
85 # expected_options = { :conditions => { :colored => 'red' } }
|
Chris@909
|
86 # assert_equal expected_options, Shirt.colored('red').proxy_options
|
Chris@909
|
87 def named_scope(name, options = {}, &block)
|
Chris@909
|
88 scopes[name] = lambda do |parent_scope, *args|
|
Chris@909
|
89 Scope.new(parent_scope, case options
|
Chris@909
|
90 when Hash
|
Chris@909
|
91 options
|
Chris@909
|
92 when Proc
|
Chris@909
|
93 options.call(*args)
|
Chris@909
|
94 end, &block)
|
Chris@909
|
95 end
|
Chris@909
|
96 (class << self; self end).instance_eval do
|
Chris@909
|
97 define_method name do |*args|
|
Chris@909
|
98 scopes[name].call(self, *args)
|
Chris@909
|
99 end
|
Chris@909
|
100 end
|
Chris@909
|
101 end
|
Chris@909
|
102 end
|
Chris@909
|
103
|
Chris@909
|
104 class Scope #:nodoc:
|
Chris@909
|
105 attr_reader :proxy_scope, :proxy_options
|
Chris@909
|
106 [].methods.each { |m| delegate m, :to => :proxy_found unless m =~ /(^__|^nil\?|^send|class|extend|find|count|sum|average|maximum|minimum|paginate)/ }
|
Chris@909
|
107 delegate :scopes, :with_scope, :to => :proxy_scope
|
Chris@909
|
108
|
Chris@909
|
109 def initialize(proxy_scope, options, &block)
|
Chris@909
|
110 [options[:extend]].flatten.each { |extension| extend extension } if options[:extend]
|
Chris@909
|
111 extend Module.new(&block) if block_given?
|
Chris@909
|
112 @proxy_scope, @proxy_options = proxy_scope, options.except(:extend)
|
Chris@909
|
113 end
|
Chris@909
|
114
|
Chris@909
|
115 def reload
|
Chris@909
|
116 load_found; self
|
Chris@909
|
117 end
|
Chris@909
|
118
|
Chris@909
|
119 protected
|
Chris@909
|
120 def proxy_found
|
Chris@909
|
121 @found || load_found
|
Chris@909
|
122 end
|
Chris@909
|
123
|
Chris@909
|
124 private
|
Chris@909
|
125 def method_missing(method, *args, &block)
|
Chris@909
|
126 if scopes.include?(method)
|
Chris@909
|
127 scopes[method].call(self, *args)
|
Chris@909
|
128 else
|
Chris@909
|
129 with_scope :find => proxy_options do
|
Chris@909
|
130 proxy_scope.send(method, *args, &block)
|
Chris@909
|
131 end
|
Chris@909
|
132 end
|
Chris@909
|
133 end
|
Chris@909
|
134
|
Chris@909
|
135 def load_found
|
Chris@909
|
136 @found = find(:all)
|
Chris@909
|
137 end
|
Chris@909
|
138 end
|
Chris@909
|
139 end
|
Chris@909
|
140 end |