public
Description: Natural-looking Finder Queries for ActiveRecord
Homepage: http://www.thoughtbot.com/projects/squirrel
Clone URL: git://github.com/thoughtbot/squirrel.git
Added first pass at a will_paginate compatability module.

git-svn-id: https://svn.thoughtbot.com/plugins/squirrel/trunk@408 
7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
jyurek (author)
Mon Mar 24 18:44:42 -0700 2008
commit  b0f7ab674b237ff2c5b0935868299cbaccc9f569
tree    5708d7acb8e495116ed227483de64be52c080db7
parent  437c20a1609acada75ac4e60aef18bd3a90b0556
...
20
21
22
 
 
 
 
 
23
24
25
...
28
29
30
 
 
 
 
 
 
...
20
21
22
23
24
25
26
27
28
29
30
...
33
34
35
36
37
38
39
40
41
0
@@ -20,6 +20,11 @@ Rake::TestTask.new(:test) do |t|
0
   t.verbose = true
0
 end
0
 
0
+desc "Clean the docs directory"
0
+task :clean_docs do
0
+ `rm -rf doc/`
0
+end
0
+
0
 desc 'Generate documentation for the squirrel plugin.'
0
 Rake::RDocTask.new(:rdoc) do |rdoc|
0
   rdoc.rdoc_dir = 'doc'
0
@@ -28,3 +33,9 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
0
   rdoc.rdoc_files.include('README')
0
   rdoc.rdoc_files.include('lib/**/*.rb')
0
 end
0
+
0
+desc 'Update documentation on website'
0
+task :sync_docs => [:clean_docs, :rdoc] do
0
+ `rsync -ave ssh doc/ dev@dev.thoughtbot.com:/home/dev/www/dev.thoughtbot.com/squirrel`
0
+end
0
+
...
1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
3
4
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
0
@@ -1,4 +1,51 @@
0
 module Squirrel
0
+ # The WillPagination module emulates the results that the will_paginate plugin returns
0
+ # from its #paginate methods. When it is used to extend a result set from Squirrel, it
0
+ # automtically pulls the result from the pagination that Squirrel performs. The methods
0
+ # added to that result set make it duck-equivalent to the WillPaginate::Collection
0
+ # class.
0
+ module WillPagination
0
+ def self.extended base
0
+ base.current_page = base.pages.current || 1
0
+ base.per_page = base.pages.per_page
0
+ base.total_entries = base.pages.total_results
0
+ end
0
+
0
+ attr_accessor :current_page, :per_page, :total_entries
0
+
0
+ # Returns the current_page + 1, or nil if there are no more.
0
+ def next_page
0
+ current_page < page_count ? (current_page + 1) : nil
0
+ end
0
+
0
+ # Returns the offset of the current page that is suitable for inserting into SQL.
0
+ def offset
0
+ (current_page - 1) * per_page
0
+ end
0
+
0
+ # Returns true if the current_page is greater than the total number of pages.
0
+ # Useful in case someone manually modifies the URL to put their own page number in.
0
+ def out_of_bounds?
0
+ current_page > page_count
0
+ end
0
+
0
+ # The number of pages in the result set.
0
+ def page_count
0
+ pages.last
0
+ end
0
+
0
+ # Returns the current_page - 1, or nil if this is the first page.
0
+ def previous_page
0
+ current_page > 1 ? (current_page - 1) : nil
0
+ end
0
+
0
+ # Sets the number of pages and entries.
0
+ def total_entries= total
0
+ @total_entries = total.to_i
0
+ @total_pages = (@total_entries / per_page.to_f).ceil
0
+ end
0
+ end
0
+
0
   # The Page class holds information about the current page of results.
0
   class Page
0
     attr_reader :offset, :limit, :page, :per_page
...
119
120
121
 
122
123
124
...
119
120
121
122
123
124
125
0
@@ -119,6 +119,7 @@ module Squirrel
0
                                                :limit => limit,
0
                                                :offset => offset) )
0
       set.instance_variable_set("@total_results", total_results)
0
+ set.extend( Squirrel::WillPagination )
0
     end
0
     
0
     # ConditionGroups are groups of Conditions, oddly enough. They most closely map to models
...
197
198
199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
201
202
...
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
0
@@ -197,6 +197,23 @@ class SquirrelTest < Test::Unit::TestCase
0
    assert_nil posts.pages.previous
0
    assert_equal (1..4), posts.pages.current_range
0
   end
0
+
0
+ def test_will_paginator_works_like_squirrel_paginator
0
+ assert posts = Post.find(:all) { id <=> (1..6); paginate :page => 2, :per_page => 4 }
0
+ [ :next_page,
0
+ :offset,
0
+ :out_of_bounds?,
0
+ :page_count,
0
+ :previous_page,
0
+ :replace,
0
+ :total_entries= ].each do |m|
0
+ assert posts.respond_to?(m)
0
+ end
0
+
0
+ assert_equal posts.pages.next, posts.next_page
0
+ assert_equal posts.pages.previous, posts.previous_page
0
+ assert_equal posts.total_results, posts.total_entries
0
+ end
0
   
0
   def test_passing_extra_conditions
0
     assert posts = Post.find(:all, :conditions => "id = 3"){ id <=> (1..6) }

Comments

    No one has commented yet.