decisiv-grouped_scope 0.5.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +34 -0
- data/MIT-LICENSE +21 -0
- data/README.rdoc +98 -0
- data/Rakefile +43 -0
- data/init.rb +2 -0
- data/lib/grouped_scope/association_reflection.rb +54 -0
- data/lib/grouped_scope/class_methods.rb +32 -0
- data/lib/grouped_scope/core_ext.rb +21 -0
- data/lib/grouped_scope/errors.rb +12 -0
- data/lib/grouped_scope/grouping.rb +9 -0
- data/lib/grouped_scope/has_many_association.rb +30 -0
- data/lib/grouped_scope/has_many_through_association.rb +28 -0
- data/lib/grouped_scope/instance_methods.rb +10 -0
- data/lib/grouped_scope/self_grouping.rb +78 -0
- data/lib/grouped_scope.rb +15 -0
- data/test/factories.rb +46 -0
- data/test/grouped_scope/association_reflection_test.rb +83 -0
- data/test/grouped_scope/class_methods_test.rb +51 -0
- data/test/grouped_scope/has_many_association_test.rb +157 -0
- data/test/grouped_scope/has_many_through_association_test.rb +53 -0
- data/test/grouped_scope/self_grouping_test.rb +147 -0
- data/test/helper.rb +108 -0
- data/test/lib/boot.rb +32 -0
- data/test/lib/core_ext.rb +20 -0
- data/test/lib/named_scope/core_ext.rb +82 -0
- data/test/lib/named_scope/named_scope.rb +168 -0
- data/test/lib/named_scope/named_scope_patch_1.2.rb +85 -0
- data/test/lib/named_scope/named_scope_patch_2.0.rb +55 -0
- data/test/lib/named_scope.rb +7 -0
- data/test/lib/test_case.rb +44 -0
- metadata +84 -0
@@ -0,0 +1,168 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module NamedScope
|
3
|
+
# All subclasses of ActiveRecord::Base have two named_scopes:
|
4
|
+
# * <tt>all</tt>, which is similar to a <tt>find(:all)</tt> query, and
|
5
|
+
# * <tt>scoped</tt>, which allows for the creation of anonymous scopes, on the fly: <tt>Shirt.scoped(:conditions => {:color => 'red'}).scoped(:include => :washing_instructions)</tt>
|
6
|
+
#
|
7
|
+
# These anonymous scopes tend to be useful when procedurally generating complex queries, where passing
|
8
|
+
# intermediate values (scopes) around as first-class objects is convenient.
|
9
|
+
def self.included(base)
|
10
|
+
base.class_eval do
|
11
|
+
extend ClassMethods
|
12
|
+
named_scope :scoped, lambda { |scope| scope }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def scopes
|
18
|
+
read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {})
|
19
|
+
end
|
20
|
+
|
21
|
+
# Adds a class method for retrieving and querying objects. A scope represents a narrowing of a database query,
|
22
|
+
# such as <tt>:conditions => {:color => :red}, :select => 'shirts.*', :include => :washing_instructions</tt>.
|
23
|
+
#
|
24
|
+
# class Shirt < ActiveRecord::Base
|
25
|
+
# named_scope :red, :conditions => {:color => 'red'}
|
26
|
+
# named_scope :dry_clean_only, :joins => :washing_instructions, :conditions => ['washing_instructions.dry_clean_only = ?', true]
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# 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>,
|
30
|
+
# in effect, represents the query <tt>Shirt.find(:all, :conditions => {:color => 'red'})</tt>.
|
31
|
+
#
|
32
|
+
# Unlike Shirt.find(...), however, the object returned by <tt>Shirt.red</tt> is not an Array; it resembles the association object
|
33
|
+
# constructed by a <tt>has_many</tt> declaration. For instance, you can invoke <tt>Shirt.red.find(:first)</tt>, <tt>Shirt.red.count</tt>,
|
34
|
+
# <tt>Shirt.red.find(:all, :conditions => {:size => 'small'})</tt>. Also, just
|
35
|
+
# as with the association objects, name scopes acts like an Array, implementing Enumerable; <tt>Shirt.red.each(&block)</tt>,
|
36
|
+
# <tt>Shirt.red.first</tt>, and <tt>Shirt.red.inject(memo, &block)</tt> all behave as if Shirt.red really were an Array.
|
37
|
+
#
|
38
|
+
# 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.
|
39
|
+
# Nested finds and calculations also work with these compositions: <tt>Shirt.red.dry_clean_only.count</tt> returns the number of garments
|
40
|
+
# for which these criteria obtain. Similarly with <tt>Shirt.red.dry_clean_only.average(:thread_count)</tt>.
|
41
|
+
#
|
42
|
+
# All scopes are available as class methods on the ActiveRecord::Base descendent upon which the scopes were defined. But they are also available to
|
43
|
+
# <tt>has_many</tt> associations. If,
|
44
|
+
#
|
45
|
+
# class Person < ActiveRecord::Base
|
46
|
+
# has_many :shirts
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# then <tt>elton.shirts.red.dry_clean_only</tt> will return all of Elton's red, dry clean
|
50
|
+
# only shirts.
|
51
|
+
#
|
52
|
+
# Named scopes can also be procedural.
|
53
|
+
#
|
54
|
+
# class Shirt < ActiveRecord::Base
|
55
|
+
# named_scope :colored, lambda { |color|
|
56
|
+
# { :conditions => { :color => color } }
|
57
|
+
# }
|
58
|
+
# end
|
59
|
+
#
|
60
|
+
# In this example, <tt>Shirt.colored('puce')</tt> finds all puce shirts.
|
61
|
+
#
|
62
|
+
# Named scopes can also have extensions, just as with <tt>has_many</tt> declarations:
|
63
|
+
#
|
64
|
+
# class Shirt < ActiveRecord::Base
|
65
|
+
# named_scope :red, :conditions => {:color => 'red'} do
|
66
|
+
# def dom_id
|
67
|
+
# 'red_shirts'
|
68
|
+
# end
|
69
|
+
# end
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
#
|
73
|
+
# For testing complex named scopes, you can examine the scoping options using the
|
74
|
+
# <tt>proxy_options</tt> method on the proxy itself.
|
75
|
+
#
|
76
|
+
# class Shirt < ActiveRecord::Base
|
77
|
+
# named_scope :colored, lambda { |color|
|
78
|
+
# { :conditions => { :color => color } }
|
79
|
+
# }
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# expected_options = { :conditions => { :colored => 'red' } }
|
83
|
+
# assert_equal expected_options, Shirt.colored('red').proxy_options
|
84
|
+
def named_scope(name, options = {}, &block)
|
85
|
+
name = name.to_sym
|
86
|
+
scopes[name] = lambda do |parent_scope, *args|
|
87
|
+
Scope.new(parent_scope, case options
|
88
|
+
when Hash
|
89
|
+
options
|
90
|
+
when Proc
|
91
|
+
options.call(*args)
|
92
|
+
end, &block)
|
93
|
+
end
|
94
|
+
(class << self; self end).instance_eval do
|
95
|
+
define_method name do |*args|
|
96
|
+
scopes[name].call(self, *args)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
class Scope
|
103
|
+
attr_reader :proxy_scope, :proxy_options
|
104
|
+
|
105
|
+
[].methods.each do |m|
|
106
|
+
unless m =~ /(^__|^nil\?|^send|^object_id$|class|extend|^find$|count|sum|average|maximum|minimum|paginate|first|last|empty\?|respond_to\?)/
|
107
|
+
delegate m, :to => :proxy_found
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
delegate :scopes, :with_scope, :to => :proxy_scope
|
112
|
+
|
113
|
+
def initialize(proxy_scope, options, &block)
|
114
|
+
[options[:extend]].flatten.each { |extension| extend extension } if options[:extend]
|
115
|
+
extend Module.new(&block) if block_given?
|
116
|
+
@proxy_scope, @proxy_options = proxy_scope, options.except(:extend)
|
117
|
+
end
|
118
|
+
|
119
|
+
def reload
|
120
|
+
load_found; self
|
121
|
+
end
|
122
|
+
|
123
|
+
def first(*args)
|
124
|
+
if args.first.kind_of?(Integer) || (@found && !args.first.kind_of?(Hash))
|
125
|
+
proxy_found.first(*args)
|
126
|
+
else
|
127
|
+
find(:first, *args)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def last(*args)
|
132
|
+
if args.first.kind_of?(Integer) || (@found && !args.first.kind_of?(Hash))
|
133
|
+
proxy_found.last(*args)
|
134
|
+
else
|
135
|
+
find(:last, *args)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def empty?
|
140
|
+
@found ? @found.empty? : count.zero?
|
141
|
+
end
|
142
|
+
|
143
|
+
def respond_to?(method, include_private = false)
|
144
|
+
super || @proxy_scope.respond_to?(method, include_private)
|
145
|
+
end
|
146
|
+
|
147
|
+
protected
|
148
|
+
def proxy_found
|
149
|
+
@found || load_found
|
150
|
+
end
|
151
|
+
|
152
|
+
private
|
153
|
+
def method_missing(method, *args, &block)
|
154
|
+
if scopes.include?(method)
|
155
|
+
scopes[method].call(self, *args)
|
156
|
+
else
|
157
|
+
with_scope :find => proxy_options do
|
158
|
+
proxy_scope.send(method, *args, &block)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def load_found
|
164
|
+
@found = find(:all)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
|
2
|
+
ActiveRecord::Associations::AssociationProxy.class_eval do
|
3
|
+
protected
|
4
|
+
def with_scope(*args, &block)
|
5
|
+
@reflection.klass.send :with_scope, *args, &block
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class ActiveRecord::Base
|
10
|
+
class << self
|
11
|
+
def find(*args)
|
12
|
+
options = extract_options_from_args!(args)
|
13
|
+
validate_find_options(options)
|
14
|
+
set_readonly_option!(options)
|
15
|
+
case args.first
|
16
|
+
when :first then find_initial(options)
|
17
|
+
when :last then find_last(options)
|
18
|
+
when :all then find_every(options)
|
19
|
+
else find_from_ids(args, options)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
private
|
23
|
+
def attribute_condition_with_named_scope(argument)
|
24
|
+
case argument
|
25
|
+
when ActiveRecord::Associations::AssociationCollection, ActiveRecord::NamedScope::Scope then "IN (?)"
|
26
|
+
else attribute_condition_without_named_scope(argument)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
alias_method_chain :attribute_condition, :named_scope
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
ActiveRecord::Associations::HasManyAssociation.class_eval do
|
34
|
+
protected
|
35
|
+
def method_missing(method, *args, &block)
|
36
|
+
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
|
37
|
+
super
|
38
|
+
elsif @reflection.klass.scopes.include?(method)
|
39
|
+
@reflection.klass.scopes[method].call(self, *args)
|
40
|
+
else
|
41
|
+
create_scoping = {}
|
42
|
+
set_belongs_to_association_for(create_scoping)
|
43
|
+
|
44
|
+
@reflection.klass.with_scope(
|
45
|
+
:create => create_scoping,
|
46
|
+
:find => {
|
47
|
+
:conditions => @finder_sql,
|
48
|
+
:joins => @join_sql,
|
49
|
+
:readonly => false
|
50
|
+
}
|
51
|
+
) do
|
52
|
+
@reflection.klass.send(method, *args, &block)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
ActiveRecord::Associations::HasManyThroughAssociation.class_eval do
|
59
|
+
protected
|
60
|
+
def method_missing(method, *args, &block)
|
61
|
+
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
|
62
|
+
super
|
63
|
+
elsif @reflection.klass.scopes.include?(method)
|
64
|
+
@reflection.klass.scopes[method].call(self, *args)
|
65
|
+
else
|
66
|
+
@reflection.klass.with_scope(construct_scope) { @reflection.klass.send(method, *args, &block) }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do
|
72
|
+
protected
|
73
|
+
def method_missing(method, *args, &block)
|
74
|
+
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
|
75
|
+
super
|
76
|
+
elsif @reflection.klass.scopes.include?(method)
|
77
|
+
@reflection.klass.scopes[method].call(self, *args)
|
78
|
+
else
|
79
|
+
@reflection.klass.with_scope(:find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false }) do
|
80
|
+
@reflection.klass.send(method, *args, &block)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
@@ -0,0 +1,55 @@
|
|
1
|
+
|
2
|
+
ActiveRecord::Associations::AssociationProxy.class_eval do
|
3
|
+
protected
|
4
|
+
def with_scope(*args, &block)
|
5
|
+
@reflection.klass.send :with_scope, *args, &block
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class ActiveRecord::Base
|
10
|
+
class << self
|
11
|
+
def find(*args)
|
12
|
+
options = args.extract_options!
|
13
|
+
validate_find_options(options)
|
14
|
+
set_readonly_option!(options)
|
15
|
+
case args.first
|
16
|
+
when :first then find_initial(options)
|
17
|
+
when :last then find_last(options)
|
18
|
+
when :all then find_every(options)
|
19
|
+
else find_from_ids(args, options)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
private
|
23
|
+
def attribute_condition_with_named_scope(argument)
|
24
|
+
case argument
|
25
|
+
when ActiveRecord::NamedScope::Scope then "IN (?)"
|
26
|
+
else attribute_condition_without_named_scope(argument)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
alias_method_chain :attribute_condition, :named_scope
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
ActiveRecord::Associations::AssociationCollection.class_eval do
|
34
|
+
protected
|
35
|
+
def method_missing(method, *args)
|
36
|
+
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
|
37
|
+
if block_given?
|
38
|
+
super { |*block_args| yield(*block_args) }
|
39
|
+
else
|
40
|
+
super
|
41
|
+
end
|
42
|
+
elsif @reflection.klass.scopes.include?(method)
|
43
|
+
@reflection.klass.scopes[method].call(self, *args)
|
44
|
+
else
|
45
|
+
with_scope(construct_scope) do
|
46
|
+
if block_given?
|
47
|
+
@reflection.klass.send(method, *args) { |*block_args| yield(*block_args) }
|
48
|
+
else
|
49
|
+
@reflection.klass.send(method, *args)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
@@ -0,0 +1,7 @@
|
|
1
|
+
unless defined? ActiveRecord::NamedScope
|
2
|
+
require "named_scope/core_ext"
|
3
|
+
require "named_scope/named_scope"
|
4
|
+
require "named_scope/named_scope_patch_#{ActiveRecord::Base.respond_to?(:find_first) ? '1.2' : '2.0'}"
|
5
|
+
ActiveRecord::Base.send :include, ActiveRecord::NamedScope
|
6
|
+
end
|
7
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
|
2
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__)+'/../debug.log')
|
3
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
|
4
|
+
ActiveRecord::Base.connection.class.class_eval do
|
5
|
+
IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/]
|
6
|
+
def execute_with_query_record(sql, name = nil, &block)
|
7
|
+
$queries_executed ||= []
|
8
|
+
$queries_executed << sql unless IGNORED_SQL.any? { |r| sql =~ r }
|
9
|
+
execute_without_query_record(sql, name, &block)
|
10
|
+
end
|
11
|
+
alias_method_chain :execute, :query_record
|
12
|
+
end
|
13
|
+
|
14
|
+
module GroupedScope
|
15
|
+
class TestCase < Test::Unit::TestCase
|
16
|
+
|
17
|
+
def test_truth ; end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def assert_sql(*patterns_to_match)
|
22
|
+
$queries_executed = []
|
23
|
+
yield
|
24
|
+
ensure
|
25
|
+
failed_patterns = []
|
26
|
+
patterns_to_match.each do |pattern|
|
27
|
+
failed_patterns << pattern unless $queries_executed.any?{ |sql| pattern === sql }
|
28
|
+
end
|
29
|
+
assert failed_patterns.empty?, "Query pattern(s) #{failed_patterns.map(&:inspect).join(', ')} not found in:\n#{$queries_executed.inspect}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def assert_queries(num = 1)
|
33
|
+
$queries_executed = []
|
34
|
+
yield
|
35
|
+
ensure
|
36
|
+
assert_equal num, $queries_executed.size, "#{$queries_executed.size} instead of #{num} queries were executed."
|
37
|
+
end
|
38
|
+
|
39
|
+
def assert_no_queries(&block)
|
40
|
+
assert_queries(0, &block)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: decisiv-grouped_scope
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ken Collins
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-09 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Extends has_many associations to group scope.
|
17
|
+
email: ken@metaskills.net
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
- CHANGELOG
|
25
|
+
- MIT-LICENSE
|
26
|
+
files:
|
27
|
+
- CHANGELOG
|
28
|
+
- MIT-LICENSE
|
29
|
+
- Rakefile
|
30
|
+
- README.rdoc
|
31
|
+
- init.rb
|
32
|
+
- lib/grouped_scope.rb
|
33
|
+
- lib/grouped_scope/association_reflection.rb
|
34
|
+
- lib/grouped_scope/class_methods.rb
|
35
|
+
- lib/grouped_scope/core_ext.rb
|
36
|
+
- lib/grouped_scope/errors.rb
|
37
|
+
- lib/grouped_scope/grouping.rb
|
38
|
+
- lib/grouped_scope/has_many_association.rb
|
39
|
+
- lib/grouped_scope/has_many_through_association.rb
|
40
|
+
- lib/grouped_scope/instance_methods.rb
|
41
|
+
- lib/grouped_scope/self_grouping.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/metaskills/grouped_scope/
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- --main
|
47
|
+
- README.rdoc
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.2.0
|
66
|
+
signing_key:
|
67
|
+
specification_version: 2
|
68
|
+
summary: Extends has_many associations to group scope.
|
69
|
+
test_files:
|
70
|
+
- test/factories.rb
|
71
|
+
- test/grouped_scope/association_reflection_test.rb
|
72
|
+
- test/grouped_scope/class_methods_test.rb
|
73
|
+
- test/grouped_scope/has_many_association_test.rb
|
74
|
+
- test/grouped_scope/has_many_through_association_test.rb
|
75
|
+
- test/grouped_scope/self_grouping_test.rb
|
76
|
+
- test/helper.rb
|
77
|
+
- test/lib/boot.rb
|
78
|
+
- test/lib/core_ext.rb
|
79
|
+
- test/lib/named_scope.rb
|
80
|
+
- test/lib/named_scope/core_ext.rb
|
81
|
+
- test/lib/named_scope/named_scope.rb
|
82
|
+
- test/lib/named_scope/named_scope_patch_1.2.rb
|
83
|
+
- test/lib/named_scope/named_scope_patch_2.0.rb
|
84
|
+
- test/lib/test_case.rb
|