genki-dm-pagination 0.2.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -21,3 +21,10 @@ USAGE:
21
21
  </ul>
22
22
  <%= paginate @posts %>
23
23
 
24
+ Paramaters:
25
+ :order = same paramaters as the :order in a normal DM call
26
+ (http://datamapper.org/doku.php?id=docs:finders&s[]=order#order)
27
+
28
+ :per_page = Number of items per page
29
+
30
+ :page = What page number
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'merb-core'
5
5
  require 'merb-core/tasks/merb'
6
6
 
7
7
  GEM_NAME = "dm-pagination"
8
- GEM_VERSION = "0.2.1"
8
+ GEM_VERSION = "0.3.1"
9
9
  AUTHOR = "Genki Takiuchi"
10
10
  EMAIL = "genki@s21g.com"
11
11
  HOMEPAGE = "http://blog.s21g.com/genki"
@@ -1,9 +1,12 @@
1
- require 'dm-pagination/pagination'
1
+ require 'dm-pagination/paginator'
2
2
 
3
3
  module DmPagination
4
4
  module Paginatable
5
5
  def paginate(options = {})
6
- Pagination.new(all, options)
6
+ paginator_type = options.delete(:paginator) ||
7
+ Merb::Plugins.config[:dm_pagination][:paginator]
8
+ paginator = Paginator.const_get(paginator_type.to_s.camel_case)
9
+ paginator.new(all, options)
7
10
  end
8
11
  end
9
12
  end
@@ -79,6 +79,7 @@ module DmPagination
79
79
  end
80
80
 
81
81
  def url(params)
82
+ @context.params.delete(:action) if @context.params[:action] == 'index'
82
83
  @context.url(@context.params.merge(params))
83
84
  end
84
85
  end
@@ -0,0 +1,23 @@
1
+ module DmPagination
2
+ module Paginator
3
+ class Solo < Base
4
+ def pages(window = 10, left = 5, right = 4)
5
+ num_of_pages = num_pages
6
+ return [] if num_of_pages <= 1
7
+ right_page = page + right
8
+ if right_page > num_of_pages
9
+ right_page = num_of_pages
10
+ elsif right_page < window
11
+ right_page = window
12
+ end
13
+ left_page = if num_of_pages - right_page < right
14
+ page - (window - (right_page - page + 1))
15
+ else
16
+ page - left
17
+ end
18
+ left_page = 1 if left_page <= 0
19
+ (left_page..right_page)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,12 @@
1
+ module DmPagination
2
+ module Paginator
3
+ class Trio < Base
4
+ def pages(window = 5, left = 2, right = 2)
5
+ (1..num_pages).inject([]) do |result, i|
6
+ i <= left || (num_pages - i) < right || (i-page).abs < window ?
7
+ result << i : (result.last.nil? ? result : result << nil)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,38 @@
1
+ module DmPagination
2
+ module Paginator
3
+ class Base
4
+ attr_reader :page, :num_pages
5
+
6
+ def initialize(collection, options)
7
+ @proxy_collection = collection
8
+ order = options[:order]
9
+
10
+ @page = (options[:page] || 1).to_i
11
+ @per_page = (options[:per_page] || 10).to_i
12
+ @num_pages = (@proxy_collection.count + @per_page - 1) / @per_page
13
+ @offset = (@page - 1)*@per_page
14
+
15
+ @collection = collection.all(:offset => @offset, :limit => @per_page)
16
+ @collection = @collection.all(:order => order) if order
17
+ end
18
+
19
+ def count
20
+ [0, [@proxy_collection.count - @offset, @per_page].min].max
21
+ end
22
+
23
+ def respond_to?(*args, &block)
24
+ super || @collection.send(:respond_to?, *args, &block)
25
+ end
26
+
27
+ def method_missing(method, *args, &block)
28
+ @collection.send(method, *args, &block)
29
+ end
30
+
31
+ def to_json
32
+ @collection.to_json
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ Dir[File.join(File.dirname(__FILE__), %w(paginator *))].each{|i| require(i)}
data/lib/dm-pagination.rb CHANGED
@@ -5,7 +5,8 @@ if defined?(Merb::Plugins)
5
5
  Merb::Plugins.config[:dm_pagination] = {
6
6
  :prev_label => '&laquo; Prev',
7
7
  :next_label => 'Next &raquo;',
8
- :truncate => '...'
8
+ :truncate => '...',
9
+ :paginator => :trio,
9
10
  }
10
11
 
11
12
  Merb::BootLoader.before_app_loads do
@@ -1,5 +1,4 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
-
3
2
  DataMapper.setup(:default, "sqlite3::memory:")
4
3
 
5
4
  Post.auto_migrate!
@@ -14,8 +13,8 @@ describe "dm-pagination" do
14
13
  end
15
14
 
16
15
  it "should return Pagination object" do
17
- Post.paginate.should be_kind_of(DmPagination::Pagination)
18
- Post.all.paginate.should be_kind_of(DmPagination::Pagination)
16
+ Post.paginate.should be_kind_of(DmPagination::Paginator::Base)
17
+ Post.all.paginate.should be_kind_of(DmPagination::Paginator::Base)
19
18
  end
20
19
 
21
20
  it "should respond to model's method" do
@@ -50,6 +49,10 @@ describe "dm-pagination" do
50
49
  Post.paginate(:page => 12).count.should == 0
51
50
  Post.paginate(:page => 5, :per_page => 6).count.should == 6
52
51
  end
52
+
53
+ it "should handle :order parametetr" do
54
+ Post.paginate(:page => 1, :order => [:id.desc]).first.id.should == 101
55
+ end
53
56
  end
54
57
 
55
58
  describe "pagination builder" do
@@ -95,5 +98,89 @@ describe "dm-pagination" do
95
98
  url = "/pagination_builder/variant?foo=2"
96
99
  response.should have_xpath("//a[@href='#{url}']")
97
100
  end
101
+
102
+ describe "solo paginator" do
103
+ before :all do
104
+ Merb::Plugins.config[:dm_pagination][:paginator] = :solo
105
+ end
106
+
107
+ after :all do
108
+ Merb::Plugins.config[:dm_pagination][:paginator] = :trio
109
+ end
110
+
111
+ it "should have rendered with pagination(1, 3, 4, 5, 6, 7, 8, 9, 10) at page 2" do
112
+ response = request "/pagination_builder/simple", :params => {:page => 2}
113
+ (1..10).reject{|p| p == 2}.each do |page|
114
+ url = "/pagination_builder/simple?page=#{page}"
115
+ response.should have_xpath("//a[@href='#{url}']")
116
+ end
117
+ [2, 11].each do |page|
118
+ url = "/pagination_builder/simple?page=#{page}"
119
+ response.should_not have_xpath("//a[@href='#{url}']")
120
+ end
121
+ end
122
+
123
+ it "should have rendered with pagination(1, 2, 3, 4, 6, 7, 8, 9, 10) at page 5" do
124
+ response = request "/pagination_builder/simple", :params => {:page => 5}
125
+ (1..10).reject{|p| p == 5}.each do |page|
126
+ url = "/pagination_builder/simple?page=#{page}"
127
+ response.should have_xpath("//a[@href='#{url}']")
128
+ end
129
+ [5, 11].each do |page|
130
+ url = "/pagination_builder/simple?page=#{page}"
131
+ response.should_not have_xpath("//a[@href='#{url}']")
132
+ end
133
+ end
134
+
135
+ it "should have rendered with pagination(2, 3, 4, 5, 6, 8, 9, 10, 11) at page 7" do
136
+ response = request "/pagination_builder/simple", :params => {:page => 7}
137
+ (2..11).reject{|p| p == 7}.each do |page|
138
+ url = "/pagination_builder/simple?page=#{page}"
139
+ response.should have_xpath("//a[@href='#{url}']")
140
+ end
141
+ [1, 7].each do |page|
142
+ url = "/pagination_builder/simple?page=#{page}"
143
+ response.should_not have_xpath("//a[@href='#{url}']")
144
+ end
145
+ end
146
+
147
+ it "should have rendered with pagination(2, 3, 4, 5, 6, 7, 8, 9, 11) at page 10" do
148
+ response = request "/pagination_builder/simple", :params => {:page => 10}
149
+ (2..11).reject{|p| p == 10}.each do |page|
150
+ url = "/pagination_builder/simple?page=#{page}"
151
+ response.should have_xpath("//a[@href='#{url}']")
152
+ end
153
+ [1, 10].each do |page|
154
+ url = "/pagination_builder/simple?page=#{page}"
155
+ response.should_not have_xpath("//a[@href='#{url}']")
156
+ end
157
+ end
158
+
159
+ it "should have rendered with pagination(2, 3, 4, 5, 6, 7, 8, 9, 10) at page 11" do
160
+ response = request "/pagination_builder/simple", :params => {:page => 11}
161
+ (2..10).each do |page|
162
+ url = "/pagination_builder/simple?page=#{page}"
163
+ response.should have_xpath("//a[@href='#{url}']")
164
+ end
165
+ [1, 11].each do |page|
166
+ url = "/pagination_builder/simple?page=#{page}"
167
+ response.should_not have_xpath("//a[@href='#{url}']")
168
+ end
169
+ end
170
+ end
171
+
172
+ it "should have rendered with pagination 'pagination_builder?page=2'" do
173
+ response = request "/pagination_builder"
174
+ url = "/pagination_builder?page=2"
175
+ response.should have_xpath("//a[@href='#{url}']")
176
+ end
177
+ end
178
+
179
+ describe "#to_json" do
180
+ it "returns the json of the collection" do
181
+ pagination = Post.paginate(:page => 1)
182
+
183
+ pagination.to_json.should == pagination.instance_variable_get(:@collection).to_json
184
+ end
98
185
  end
99
186
  end
@@ -8,4 +8,9 @@ class PaginationBuilder < Merb::Controller
8
8
  @posts = Post.paginate(params)
9
9
  render
10
10
  end
11
+
12
+ def index
13
+ @posts = Post.paginate(params)
14
+ render
15
+ end
11
16
  end
@@ -0,0 +1,7 @@
1
+ <ul>
2
+ <% @posts.each do |post| %>
3
+ <li><%= post.index %></li>
4
+ <% end %>
5
+ </ul>
6
+
7
+ <%= paginate @posts %>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: genki-dm-pagination
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Takiuchi
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-27 00:00:00 -07:00
12
+ date: 2009-03-31 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -40,8 +40,11 @@ files:
40
40
  - lib/dm-pagination
41
41
  - lib/dm-pagination/merbtasks.rb
42
42
  - lib/dm-pagination/paginatable.rb
43
- - lib/dm-pagination/pagination.rb
44
43
  - lib/dm-pagination/pagination_builder.rb
44
+ - lib/dm-pagination/paginator
45
+ - lib/dm-pagination/paginator/solo.rb
46
+ - lib/dm-pagination/paginator/trio.rb
47
+ - lib/dm-pagination/paginator.rb
45
48
  - lib/dm-pagination.rb
46
49
  - spec/dm-pagination_spec.rb
47
50
  - spec/fixture
@@ -54,6 +57,7 @@ files:
54
57
  - spec/fixture/app/views/layout
55
58
  - spec/fixture/app/views/layout/application.html.erb
56
59
  - spec/fixture/app/views/pagination_builder
60
+ - spec/fixture/app/views/pagination_builder/index.html.erb
57
61
  - spec/fixture/app/views/pagination_builder/simple.html.erb
58
62
  - spec/fixture/app/views/pagination_builder/variant.html.erb
59
63
  - spec/fixture/config
@@ -1,41 +0,0 @@
1
- module DmPagination
2
- class Pagination
3
- def initialize(collection, options)
4
- @page = (options[:page] || 1).to_i
5
- @per_page = (options[:per_page] || 10).to_i
6
- @proxy_collection = collection
7
- @collection = collection.all(
8
- :offset => (@page - 1)*@per_page, :limit => @per_page)
9
- @num_pages = num_pages
10
- end
11
-
12
- def page
13
- @page
14
- end
15
-
16
- def num_pages
17
- (@proxy_collection.count + @per_page - 1) / @per_page
18
- end
19
-
20
- def pages(window = 5, left = 2, right = 2)
21
- return [] if @num_pages <= 1
22
- (1..@num_pages).inject([]) do |result, i|
23
- i <= left || (@num_pages - i) < right || (i-page).abs < window ?
24
- result << i : (result.last.nil? ? result : result << nil)
25
- end
26
- end
27
-
28
- def count
29
- offset = (@page - 1)*@per_page
30
- [0, [@proxy_collection.count - offset, @per_page].min].max
31
- end
32
-
33
- def respond_to?(*args, &block)
34
- super || @collection.send(:respond_to?, *args, &block)
35
- end
36
-
37
- def method_missing(method, *args, &block)
38
- @collection.send(method, *args, &block)
39
- end
40
- end
41
- end