public
Description: Natural-looking Finder Queries for ActiveRecord
Homepage: http://www.thoughtbot.com/projects/squirrel
Clone URL: git://github.com/thoughtbot/squirrel.git
Removed the explicit branch and added support for using squirrel blocks on 
proxy associations.

git-svn-id: https://svn.thoughtbot.com/plugins/squirrel/trunk@409 
7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
jyurek (author)
Tue Mar 25 17:51:20 -0700 2008
commit  775d386ff34c79f89b4fb9b0317283dcf5dc1186
tree    d0125a6fc4cfffdbb75da5ddead02ce3a7e04b01
parent  b0f7ab674b237ff2c5b0935868299cbaccc9f569
...
1
2
 
 
 
 
 
 
 
 
 
 
...
1
 
2
3
4
5
6
7
8
9
10
11
0
@@ -1,2 +1,11 @@
0
 require File.dirname(__FILE__) + '/lib/squirrel.rb'
0
-ActiveRecord::Base.send :include, Squirrel::Hook
0
+class << ActiveRecord::Base
0
+ include Squirrel::Hook
0
+end
0
+
0
+[ ActiveRecord::Associations::HasManyAssociation,
0
+ ActiveRecord::Associations::HasAndBelongsToManyAssociation,
0
+ ActiveRecord::Associations::HasManyThroughAssociation
0
+].each do |association_class|
0
+ association_class.send(:include, Squirrel::Hook)
0
+end
...
7
8
9
 
 
 
 
 
 
 
 
 
 
10
11
12
13
14
15
16
17
18
19
20
 
21
22
23
...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
 
 
 
 
 
 
 
 
 
21
22
23
24
0
@@ -7,17 +7,18 @@ require 'extensions'
0
 module Squirrel
0
   # When included in AR::Base, it chains the #find method to allow for block execution.
0
   module Hook
0
+ def find_with_squirrel *args, &blk
0
+ args ||= [:all]
0
+ if blk || (args.last.is_a?(Hash) && args.last.has_key?(:paginate))
0
+ query = Query.new(self, &blk)
0
+ query.execute(*args)
0
+ else
0
+ find_without_squirrel(*args)
0
+ end
0
+ end
0
+
0
     def self.included base
0
- class << base
0
- def find_with_squirrel *args, &blk
0
- args ||= [:all]
0
- if blk || (args.last.is_a?(Hash) && args.last.has_key?(:paginate))
0
- query = Query.new(self, &blk)
0
- query.execute(*args)
0
- else
0
- find_without_squirrel(*args)
0
- end
0
- end
0
+ base.class_eval do
0
         alias_method :find_without_squirrel, :find
0
         alias_method :find, :find_with_squirrel
0
       end
...
1
2
 
 
3
4
5
6
7
8
9
...
 
 
1
2
3
4
5
 
6
7
8
0
@@ -1,9 +1,8 @@
0
-sqlite:
0
- adapter: sqlite
0
+sqlite3_file:
0
+ adapter: sqlite3
0
   dbfile: squirrel.sqlite.db
0
 sqlite3:
0
   adapter: sqlite3
0
- #dbfile: squirrel.sqlite3.db
0
   database: ":memory:"
0
 postgresql:
0
   adapter: postgresql
...
27
28
29
30
31
 
 
 
 
 
 
...
27
28
29
 
30
31
32
33
34
35
36
0
@@ -27,4 +27,9 @@ sixth:
0
   id: 6
0
   title: Yes! Blah!
0
   body: Blah for sure.
0
- user_id: 3
0
\ No newline at end of file
0
+ user_id: 3
0
+seventh:
0
+ id: 7
0
+ title: Ruby fills me with joy.
0
+ body: Really, Ruby is just fantastic.
0
+ user_id: 2
...
24
25
26
27
28
 
 
 
 
...
24
25
26
 
27
28
29
30
31
0
@@ -24,4 +24,7 @@ eigth:
0
   tag_id: 1
0
 ninth:
0
   post_id: 6
0
- tag_id: 3
0
\ No newline at end of file
0
+ tag_id: 3
0
+tenth:
0
+ post_id: 7
0
+ tag_id: 3
...
31
32
33
34
 
35
36
37
38
39
 
40
41
42
 
 
43
44
45
...
225
226
227
228
 
229
230
231
...
250
251
252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
...
31
32
33
 
34
35
36
37
38
 
39
40
41
 
42
43
44
45
46
...
226
227
228
 
229
230
231
232
...
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
0
@@ -31,15 +31,16 @@ class SquirrelTest < Test::Unit::TestCase
0
   def test_simple_operators
0
     # Sanity Check
0
     posts = Post.find(:all)
0
- assert_equal 6, posts.length
0
+ assert_equal 7, posts.length
0
     assert_equal 1, posts.first.id
0
     
0
     posts = Post.find(:all) { id == 1 }
0
     assert_equal 1, posts.length
0
- assert_equal 1, posts.first.id
0
+ assert posts.all?{|p| p.id == 1 }
0
 
0
     posts = Post.find(:all) { -id == 1 }
0
- assert_equal [2, 3, 4, 5, 6], posts.collect{|p| p.id }.sort
0
+ assert ! posts.empty?
0
+ assert ! posts.any?{|p| p.id == 1 }
0
 
0
     posts = Post.find(:all) { title =~ "%Rails%" }
0
     assert_equal 2, posts.length
0
@@ -225,7 +226,7 @@ class SquirrelTest < Test::Unit::TestCase
0
     assert_equal 2, posts.size
0
     assert_not_nil posts.pages
0
     assert_equal 1, posts.pages.first
0
- assert_equal 3, posts.pages.last
0
+ assert_equal 4, posts.pages.last
0
     assert_equal 3, posts.first.id
0
   end
0
   
0
@@ -250,4 +251,25 @@ class SquirrelTest < Test::Unit::TestCase
0
                   6],
0
                  query.to_find_conditions)
0
   end
0
+
0
+ def test_has_many_proxy_associations
0
+ user = User.find(2)
0
+ query = user.posts.find(:query){ body.contains? "Rails" }
0
+ assert_equal ["(posts.body LIKE ?)", "%Rails%"], query.to_find_conditions
0
+ assert_equal( {}, query.to_find_include )
0
+
0
+ assert_equal [Post.find(2), Post.find(5)], query.execute(:all)
0
+ end
0
+
0
+ def test_joins_inside_proxy_associations
0
+ user = User.find(2)
0
+ query = user.posts.find(:query){
0
+ body.contains? "Ruby"
0
+ tags.name == "Nothing"
0
+ }
0
+ assert_equal ["(posts.body LIKE ? AND (tags.name = ?))", "%Ruby%", "Nothing"], query.to_find_conditions
0
+ assert_equal( {:tags => {}}, query.to_find_include )
0
+
0
+ assert_equal [Post.find(7)], query.execute(:all)
0
+ end
0
 end

Comments

    No one has commented yet.