public
Description: Natural-looking Finder Queries for ActiveRecord
Homepage: http://www.thoughtbot.com/projects/squirrel
Clone URL: git://github.com/thoughtbot/squirrel.git
Added capacity for scoped to take a Squirrel block and do what's right.
jyurek (author)
Fri Jun 20 13:54:27 -0700 2008
commit  23afa9bc674a1dc89ec981992de4a03ff3efffe4
tree    4b01b8fbb28304840f84d6189c6c266b9c10fefc
parent  4a6efb445a22686a5d4d60fbb16ab96418c420a1
...
3
4
5
 
 
 
 
 
 
6
7
8
...
3
4
5
6
7
8
9
10
11
12
13
14
0
@@ -3,6 +3,12 @@ class << ActiveRecord::Base
0
   include Squirrel::Hook
0
 end
0
 
0
+if defined?(ActiveRecord::NamedScope::Scope)
0
+ class ActiveRecord::NamedScope::Scope
0
+ include Squirrel::NamedScopeHook
0
+ end
0
+end
0
+
0
 [ ActiveRecord::Associations::HasManyAssociation,
0
   ActiveRecord::Associations::HasAndBelongsToManyAssociation,
0
   ActiveRecord::Associations::HasManyThroughAssociation
...
17
18
19
 
 
 
 
 
 
 
 
 
20
21
22
23
24
 
 
 
 
 
 
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
27
28
...
64
65
66
67
68
69
70
71
72
 
73
74
 
75
76
77
78
79
80
 
 
 
 
 
 
 
 
 
 
81
82
83
...
17
18
19
20
21
22
23
24
25
26
27
28
29
 
 
 
 
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
...
89
90
91
 
 
 
 
 
 
92
93
 
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
0
@@ -17,12 +17,37 @@ module Squirrel
0
       end
0
     end
0
 
0
+ def scoped_with_squirrel *args, &blk
0
+ if blk
0
+ query = Query.new(self, &blk)
0
+ self.scoped(query.to_find_parameters)
0
+ else
0
+ scoped_without_squirrel(*args)
0
+ end
0
+ end
0
+
0
     def self.included base
0
- return if base.instance_methods.include? 'find_without_squirrel'
0
- base.class_eval do
0
- alias_method :find_without_squirrel, :find
0
- alias_method :find, :find_with_squirrel
0
+ if ! base.instance_methods.include?('find_without_squirrel') &&
0
+ base.instance_methods.include?('find')
0
+ base.class_eval do
0
+ alias_method :find_without_squirrel, :find
0
+ alias_method :find, :find_with_squirrel
0
+ end
0
       end
0
+ if ! base.instance_methods.include?('scoped_without_squirrel') &&
0
+ base.instance_methods.include?('scoped')
0
+ base.class_eval do
0
+ alias_method :scoped_without_squirrel, :scoped
0
+ alias_method :scoped, :scoped_with_squirrel
0
+ end
0
+ end
0
+ end
0
+ end
0
+
0
+ module NamedScopeHook
0
+ def scoped *args, &blk
0
+ args = blk ? [Query.new(self, &blk).to_find_parameters] : args
0
+ scopes[:scoped].call(self, *args)
0
     end
0
   end
0
 
0
@@ -64,20 +89,25 @@ module Squirrel
0
         pagination = opts.delete(:paginate) || {}
0
         model.send(:with_scope, :find => opts) do
0
           @conditions.paginate(pagination) unless pagination.empty?
0
- find_parameters = { :conditions => to_find_conditions,
0
- :include => to_find_include,
0
- :order => to_find_order,
0
- :limit => to_find_limit,
0
- :offset => to_find_offset }
0
- results = model.find args[0], find_parameters
0
+ results = model.find args[0], to_find_parameters
0
           if @conditions.paginate?
0
- paginate_result_set results, find_parameters
0
+ paginate_result_set results, to_find_parameters
0
           end
0
         end
0
         results
0
       end
0
     end
0
 
0
+ def to_find_parameters
0
+ find_parameters = {}
0
+ find_parameters[:conditions] = to_find_conditions unless to_find_conditions.blank?
0
+ find_parameters[:include ] = to_find_include unless to_find_include.blank?
0
+ find_parameters[:order ] = to_find_order unless to_find_order.blank?
0
+ find_parameters[:limit ] = to_find_limit unless to_find_limit.blank?
0
+ find_parameters[:offset ] = to_find_offset unless to_find_offset.blank?
0
+ find_parameters
0
+ end
0
+
0
     # Delegates the to_find_conditions call to the root ConditionGroup
0
     def to_find_conditions
0
       @conditions.to_find_conditions
...
272
273
274
 
 
 
 
 
 
 
 
 
 
275
...
272
273
274
275
276
277
278
279
280
281
282
283
284
285
0
@@ -272,4 +272,14 @@ class SquirrelTest < Test::Unit::TestCase
0
 
0
     assert_equal [Post.find(7)], query.execute(:all)
0
   end
0
+
0
+ def test_scopes_work_like_find_does
0
+ assert_equal( {:conditions => User.find(:query){ id == 2 }.to_find_conditions},
0
+ User.scoped{ id == 2 }.proxy_options)
0
+ end
0
+
0
+ def test_scopes_can_be_nested
0
+ assert_equal(User.find(1),
0
+ User.scoped{ name =~ "Jon%" }.scoped{ id < 3 }.first)
0
+ end
0
 end

Comments

    No one has commented yet.