sinatra-pagin 0.0.1 → 0.0.2
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.md +4 -0
- data/VERSION +1 -1
- data/lib/sinatra/pagin.rb +14 -0
- data/sinatra-pagin.gemspec +1 -1
- data/spec/pagin_spec.rb +88 -8
- metadata +1 -1
data/Changelog.md
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/lib/sinatra/pagin.rb
CHANGED
@@ -25,6 +25,20 @@ module Sinatra
|
|
25
25
|
|
26
26
|
@page || 1
|
27
27
|
end
|
28
|
+
|
29
|
+
def href_for_pagin(total_pages, direction = :next)
|
30
|
+
path_info = request.path_info.gsub(/\/$/, '') # clear off the last slash just in case
|
31
|
+
page_num = 1
|
32
|
+
|
33
|
+
case
|
34
|
+
when direction === :next
|
35
|
+
page_num = page+1 >= total_pages ? total_pages : page+1
|
36
|
+
when direction === :prev
|
37
|
+
page_num = page-1 <= 0 ? 1 : page-1
|
38
|
+
end
|
39
|
+
|
40
|
+
path_info+"/page/#{page_num}"
|
41
|
+
end
|
28
42
|
end
|
29
43
|
end
|
30
44
|
|
data/sinatra-pagin.gemspec
CHANGED
data/spec/pagin_spec.rb
CHANGED
@@ -140,7 +140,7 @@ describe 'Sinatra' do
|
|
140
140
|
describe "Helpers" do
|
141
141
|
include Sinatra::Pagin::Helpers
|
142
142
|
|
143
|
-
describe "
|
143
|
+
describe "page" do
|
144
144
|
it "should return 1 if @page.nil?" do
|
145
145
|
page.should == 1
|
146
146
|
end
|
@@ -152,15 +152,95 @@ describe 'Sinatra' do
|
|
152
152
|
end
|
153
153
|
end
|
154
154
|
|
155
|
-
|
156
|
-
|
157
|
-
"
|
155
|
+
context "within an actual app" do
|
156
|
+
before(:all) do
|
157
|
+
app.get "/get/page/value/if" do
|
158
|
+
"The page is #{page}"
|
159
|
+
end
|
158
160
|
end
|
159
161
|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
162
|
+
it "should actually work inside the app" do
|
163
|
+
get "/get/page/value/if/page/123"
|
164
|
+
last_response.should be_ok
|
165
|
+
last_response.body.should == "The page is 123"
|
166
|
+
last_request.url.should == "http://example.org/get/page/value/if"
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
describe "href_for_pagin" do
|
172
|
+
describe "" do
|
173
|
+
before(:each) do
|
174
|
+
stub!(:request)
|
175
|
+
request.stub!(:path_info).and_return '/2009/10'
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should return a paginated uri based on the current request" do
|
179
|
+
page 2
|
180
|
+
|
181
|
+
next_page = href_for_pagin(3, :next)
|
182
|
+
prev_page = href_for_pagin(3, :prev)
|
183
|
+
|
184
|
+
next_page.should == "/2009/10/page/3"
|
185
|
+
prev_page.should == "/2009/10/page/1"
|
186
|
+
end
|
187
|
+
|
188
|
+
it "should return ../page/1 for :prev if page is 1" do
|
189
|
+
page 1
|
190
|
+
href_for_pagin(3, :prev).should == "/2009/10/page/1"
|
191
|
+
end
|
192
|
+
|
193
|
+
it "should return ../page/*total_pages for :next if page is the last page" do
|
194
|
+
page 3
|
195
|
+
href_for_pagin(3, :next).should == "/2009/10/page/3"
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
context "within an actual app" do
|
200
|
+
before(:all) do
|
201
|
+
app.get "/this/path/should/be/base" do
|
202
|
+
total_pages = 4
|
203
|
+
<<-HTML
|
204
|
+
<a href="#{href_for_pagin(total_pages, :prev)}">previous</a> |
|
205
|
+
<a href="#{href_for_pagin(total_pages, :next)}">next</a>
|
206
|
+
HTML
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
it "should return the proper hrefs given ../page/1" do
|
211
|
+
get "/this/path/should/be/base/page/1"
|
212
|
+
last_response.should be_ok
|
213
|
+
last_response.should have_selector 'a', :href => "/this/path/should/be/base/page/1" do |a|
|
214
|
+
a.inner_text.should == 'previous'
|
215
|
+
end
|
216
|
+
last_response.should have_selector 'a', :href => "/this/path/should/be/base/page/2" do |a|
|
217
|
+
a.inner_text.should == 'next'
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
[2, 3].each do |p|
|
222
|
+
it "should return the proper hrefs given ../page/#{p}" do
|
223
|
+
get "/this/path/should/be/base/page/#{p}"
|
224
|
+
last_response.should be_ok
|
225
|
+
last_response.should have_selector 'a', :href => "/this/path/should/be/base/page/#{p-1}" do |a|
|
226
|
+
a.inner_text.should == 'previous'
|
227
|
+
end
|
228
|
+
last_response.should have_selector 'a', :href => "/this/path/should/be/base/page/#{p+1}" do |a|
|
229
|
+
a.inner_text.should == 'next'
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
it "should return the proper hrefs given ../page/4" do
|
235
|
+
get "/this/path/should/be/base/page/4"
|
236
|
+
last_response.should be_ok
|
237
|
+
last_response.should have_selector 'a', :href => "/this/path/should/be/base/page/3" do |a|
|
238
|
+
a.inner_text.should == 'previous'
|
239
|
+
end
|
240
|
+
last_response.should have_selector 'a', :href => "/this/path/should/be/base/page/4" do |a|
|
241
|
+
a.inner_text.should == 'next'
|
242
|
+
end
|
243
|
+
end
|
164
244
|
end
|
165
245
|
end
|
166
246
|
end
|