ramaze 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/ramaze +58 -26
- data/doc/AUTHORS +2 -0
- data/doc/CHANGELOG +401 -0
- data/doc/README +14 -9
- data/doc/README.html +737 -0
- data/doc/TODO +14 -14
- data/doc/changes.txt +401 -0
- data/doc/changes.xml +401 -0
- data/doc/meta/announcement.txt +28 -15
- data/doc/meta/configuration.txt +3 -3
- data/doc/meta/internals.txt +2 -2
- data/doc/meta/users.kml +62 -0
- data/doc/readme_chunks/examples.txt +1 -1
- data/doc/readme_chunks/features.txt +13 -8
- data/doc/tutorial/todolist.html +2 -2
- data/doc/tutorial/todolist.mkd +2 -2
- data/examples/blog/README +3 -0
- data/examples/blog/spec/blog.rb +101 -0
- data/examples/blog/{main.rb → start.rb} +0 -0
- data/examples/caching.rb +16 -5
- data/examples/layout.rb +41 -0
- data/examples/templates/template_amrita2.rb +1 -1
- data/examples/templates/template_erubis.rb +1 -1
- data/examples/templates/template_ezamar.rb +1 -1
- data/examples/templates/template_haml.rb +1 -1
- data/examples/templates/template_liquid.rb +1 -1
- data/examples/templates/template_markaby.rb +1 -1
- data/examples/templates/template_remarkably.rb +1 -1
- data/examples/todolist/README +1 -0
- data/{spec/examples → examples/todolist/spec}/todolist.rb +3 -3
- data/examples/todolist/{main.rb → start.rb} +0 -0
- data/examples/whywiki/{main.rb → start.rb} +0 -0
- data/examples/wiktacular/README +2 -0
- data/examples/wiktacular/spec/wiktacular.rb +146 -0
- data/examples/wiktacular/src/controller.rb +11 -0
- data/examples/wiktacular/src/model.rb +5 -1
- data/examples/wiktacular/{main.rb → start.rb} +0 -1
- data/examples/wiktacular/template/edit.xhtml +1 -2
- data/examples/wiktacular/template/html_layout.xhtml +27 -0
- data/examples/wiktacular/template/index.xhtml +8 -10
- data/examples/wiktacular/template/new.xhtml +1 -2
- data/lib/proto/{main.rb → start.rb} +0 -0
- data/lib/proto/template/index.xhtml +1 -1
- data/lib/ramaze/action/render.rb +58 -4
- data/lib/ramaze/action.rb +20 -0
- data/lib/ramaze/adapter/cgi.rb +7 -4
- data/lib/ramaze/adapter/fcgi.rb +6 -4
- data/lib/ramaze/adapter/mongrel.rb +4 -0
- data/lib/ramaze/adapter/webrick.rb +20 -9
- data/lib/ramaze/adapter.rb +3 -1
- data/lib/ramaze/cache/memcached.rb +2 -26
- data/lib/ramaze/cache.rb +8 -4
- data/lib/ramaze/controller/resolve.rb +26 -8
- data/lib/ramaze/controller.rb +44 -7
- data/lib/ramaze/dispatcher/action.rb +5 -1
- data/lib/ramaze/dispatcher/directory.rb +115 -0
- data/lib/ramaze/dispatcher/error.rb +19 -5
- data/lib/ramaze/dispatcher/file.rb +2 -2
- data/lib/ramaze/dispatcher.rb +52 -39
- data/lib/ramaze/global/dsl.rb +3 -2
- data/lib/ramaze/global/globalstruct.rb +26 -4
- data/lib/ramaze/global.rb +20 -11
- data/lib/ramaze/helper/aspect.rb +29 -11
- data/lib/ramaze/helper/auth.rb +2 -2
- data/lib/ramaze/helper/cache.rb +2 -0
- data/lib/ramaze/helper/identity.rb +21 -6
- data/lib/ramaze/helper/link.rb +1 -1
- data/lib/ramaze/helper/pager.rb +158 -100
- data/lib/ramaze/helper/partial.rb +22 -0
- data/lib/ramaze/helper/redirect.rb +1 -2
- data/lib/ramaze/helper/stack.rb +1 -1
- data/lib/ramaze/inform/informer.rb +20 -0
- data/lib/ramaze/inform/syslog.rb +5 -0
- data/lib/ramaze/snippets/kernel/__dir__.rb +21 -0
- data/lib/ramaze/snippets/kernel/aquire.rb +22 -12
- data/lib/ramaze/snippets/numeric/human_readable_filesize_format.rb +33 -0
- data/lib/ramaze/snippets/ramaze/caller_lines.rb +1 -1
- data/lib/ramaze/snippets/struct/values_at.rb +11 -4
- data/lib/ramaze/sourcereload.rb +29 -1
- data/{spec → lib/ramaze/spec}/helper/context.rb +0 -0
- data/{spec → lib/ramaze/spec}/helper/layout.rb +1 -1
- data/{spec → lib/ramaze/spec}/helper/minimal.rb +0 -0
- data/{spec → lib/ramaze/spec}/helper/mock_http.rb +0 -0
- data/{spec → lib/ramaze/spec}/helper/requester.rb +0 -0
- data/{spec → lib/ramaze/spec}/helper/simple_http.rb +0 -0
- data/{spec → lib/ramaze/spec}/helper/wrap.rb +0 -0
- data/lib/ramaze/spec/helper.rb +60 -0
- data/lib/ramaze/store/default.rb +3 -0
- data/lib/ramaze/template/ezamar/render_partial.rb +3 -0
- data/lib/ramaze/template/sass.rb +45 -0
- data/lib/ramaze/template.rb +1 -1
- data/lib/ramaze/tool/create.rb +5 -5
- data/lib/ramaze/tool/localize.rb +26 -5
- data/lib/ramaze/tool/mime.rb +7 -0
- data/lib/ramaze/trinity/response.rb +15 -0
- data/lib/ramaze/trinity/session.rb +1 -1
- data/lib/ramaze/version.rb +1 -1
- data/lib/ramaze.rb +5 -0
- data/rake_tasks/conf.rake +4 -4
- data/rake_tasks/maintaince.rake +12 -7
- data/rake_tasks/spec.rake +7 -4
- data/spec/examples/caching.rb +6 -8
- data/spec/helper.rb +3 -60
- data/spec/ramaze/action/basics.rb +17 -0
- data/spec/ramaze/action/cache.rb +28 -0
- data/spec/ramaze/action/layout.rb +110 -0
- data/spec/ramaze/action/render.rb +14 -0
- data/spec/ramaze/action/template/bar.xhtml +1 -0
- data/spec/ramaze/action/template/other_wrapper.xhtml +1 -0
- data/spec/ramaze/action/template/single_wrapper.xhtml +1 -0
- data/spec/ramaze/controller/template/edit/content.xhtml +1 -0
- data/spec/ramaze/controller/template/edit.xhtml +1 -0
- data/spec/ramaze/controller/template_resolving.rb +2 -2
- data/spec/ramaze/dispatcher/directory.rb +53 -0
- data/spec/ramaze/dispatcher/file.rb +0 -3
- data/spec/ramaze/error.rb +17 -2
- data/spec/ramaze/helper/aspect.rb +14 -20
- data/spec/ramaze/helper/pager.rb +83 -1
- data/spec/ramaze/helper/partial.rb +1 -1
- data/spec/ramaze/helper/template/test_template.xhtml +1 -0
- data/spec/ramaze/inform/informer.rb +37 -5
- data/spec/ramaze/localize.rb +1 -1
- data/spec/ramaze/template/haml.rb +5 -5
- data/spec/ramaze/template/sass/file.css.sass +5 -0
- data/spec/ramaze/template/sass.rb +46 -0
- data/spec/ramaze/template.rb +1 -1
- data/spec/ramaze/trinity/session.rb +27 -0
- data/spec/snippets/kernel/__dir__.rb +8 -0
- data/spec/snippets/kernel/aquire.rb +71 -0
- data/spec/snippets/kernel/constant.rb +27 -0
- data/spec/snippets/numeric/human_readable_filesize_format.rb +11 -0
- data/spec/snippets/ramaze/caller_info.rb +39 -0
- data/spec/snippets/ramaze/caller_lines.rb +27 -0
- data/spec/snippets/string/DIVIDE.rb +18 -0
- data/spec/snippets/string/camel_case.rb +25 -0
- data/spec/snippets/string/color.rb +11 -0
- data/spec/snippets/string/snake_case.rb +17 -0
- data/spec/snippets/struct/fill.rb +27 -0
- data/spec/snippets/struct/values_at.rb +39 -0
- metadata +71 -31
- data/examples/todolist/todolist.db +0 -5
- data/examples/wiktacular/src/page.rb +0 -66
- data/lib/ramaze/helper/feed.rb +0 -135
- data/lib/ramaze/helper/form.rb +0 -204
- data/lib/ramaze/snippets/openstruct/temp.rb +0 -13
- data/lib/ramaze/store/yaml.rb +0 -170
- data/spec/ramaze/helper/feed.rb +0 -127
- data/spec/ramaze/helper/form.rb +0 -146
- data/spec/ramaze/store/yaml.rb +0 -76
data/lib/ramaze/helper/auth.rb
CHANGED
@@ -8,7 +8,7 @@ module Ramaze
|
|
8
8
|
# A really, really, totally stupid way to do authentication. It has no
|
9
9
|
# roles and only a single password without usernames.
|
10
10
|
#
|
11
|
-
# It is intended to be a simple way to protect various
|
11
|
+
# It is intended to be a simple way to protect various portions of a page
|
12
12
|
# when you start working on it. Also it is a nice way to see how you could
|
13
13
|
# implement your own authentication.
|
14
14
|
|
@@ -100,7 +100,7 @@ module Ramaze
|
|
100
100
|
#
|
101
101
|
# # The method to be called.
|
102
102
|
# trait :auth_table => :auth_table
|
103
|
-
# trait :
|
103
|
+
# trait :auth_table => 'auth_table'
|
104
104
|
#
|
105
105
|
# # Lambda that will be called upon demand
|
106
106
|
# trait :auth_table => lambda{ {'manveru' => Digest::SHA1.hexdigest 'password'} }
|
data/lib/ramaze/helper/cache.rb
CHANGED
@@ -7,27 +7,34 @@ require 'openid'
|
|
7
7
|
module Ramaze
|
8
8
|
|
9
9
|
openid_store_file = File.join(Dir.tmpdir, 'openid-store')
|
10
|
+
|
11
|
+
# Constant for storing meta-information persistent
|
10
12
|
OpenIDStore = OpenID::FilesystemStore.new(openid_store_file)
|
11
13
|
|
12
14
|
# This is called Identity to avoid collisions with the original openid.rb
|
15
|
+
# It provides a nice and simple way to provide and control access over the
|
16
|
+
# OpenID authentication model.
|
13
17
|
|
14
18
|
module IdentityHelper
|
15
|
-
def self.included(klass)
|
16
|
-
klass.send(:helper, :flash)
|
17
|
-
end
|
18
19
|
|
20
|
+
# Simple form for use or overwriting.
|
21
|
+
# Has to provide the same functionality when overwritten or directly
|
22
|
+
# embedded into a page.
|
19
23
|
def openid_login_form
|
20
24
|
%{
|
21
|
-
<form method="GET" action="#{
|
25
|
+
<form method="GET" action="#{Rs(:openid_begin)}">
|
22
26
|
Identity URL: <input type="text" name="url" />
|
23
27
|
<input type="submit" />
|
24
28
|
</form>
|
25
29
|
}
|
26
30
|
end
|
27
31
|
|
32
|
+
# We land here from the openid_login_form and if we can find a matching
|
33
|
+
# OpenID server we redirect the user to it, the browser will return to
|
34
|
+
# openid_complete when the authentication is complete.
|
28
35
|
def openid_begin
|
29
36
|
url = request['url']
|
30
|
-
redirect_referrer if url.
|
37
|
+
redirect_referrer if url.to_s.empty?
|
31
38
|
session[:openid_entry] = request.referrer
|
32
39
|
|
33
40
|
openid_request = openid_consumer.begin(url)
|
@@ -38,13 +45,20 @@ module Ramaze
|
|
38
45
|
redirect_referrer
|
39
46
|
when OpenID::SUCCESS
|
40
47
|
root = "http://#{request.http_host}/"
|
41
|
-
return_to = root[0..-2] +
|
48
|
+
return_to = root[0..-2] + Rs(:openid_complete)
|
42
49
|
redirect_url = openid_request.redirect_url(root, return_to)
|
43
50
|
|
44
51
|
redirect(redirect_url)
|
45
52
|
end
|
46
53
|
end
|
47
54
|
|
55
|
+
# After having authenticated at the OpenID server browsers are redirected
|
56
|
+
# back here and on success we set the session[:openid_identity] and a little
|
57
|
+
# default flash message. Then we redirect to wherever session[:openid_entry]
|
58
|
+
# points us to, which was set on openid_begin to the referrer
|
59
|
+
#
|
60
|
+
# TODO:
|
61
|
+
# - maybe using StackHelper, but this is a really minimal overlap?
|
48
62
|
def openid_complete
|
49
63
|
openid_response = openid_consumer.complete(request.params)
|
50
64
|
|
@@ -63,6 +77,7 @@ module Ramaze
|
|
63
77
|
|
64
78
|
private
|
65
79
|
|
80
|
+
# Fetch/Create a OpenID::Consumer for current session.
|
66
81
|
def openid_consumer
|
67
82
|
OpenID::Consumer.new(session, Ramaze::OpenIDStore)
|
68
83
|
end
|
data/lib/ramaze/helper/link.rb
CHANGED
data/lib/ramaze/helper/pager.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
module Ramaze
|
2
|
+
|
1
3
|
# The BSD License
|
2
4
|
#
|
3
5
|
# Copyright (c) 2004-2007, George K. Moschovitis. (http://www.gmosx.com)
|
@@ -31,17 +33,53 @@
|
|
31
33
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32
34
|
#
|
33
35
|
|
34
|
-
module Ramaze
|
35
|
-
|
36
36
|
# Displays a collection of entitities in multiple pages.
|
37
37
|
#
|
38
38
|
# === Design
|
39
39
|
#
|
40
|
-
# This pager is carefully designed for scaleability. It stores
|
41
|
-
#
|
42
|
-
#
|
43
|
-
# leverages the SQL LIMIT option to optimize database
|
40
|
+
# This pager is carefully designed for scaleability. It stores only the items
|
41
|
+
# for one page. The key parameter is needed, multiple pagers can coexist in a
|
42
|
+
# single page. The pager leverages the SQL LIMIT option to optimize database
|
44
43
|
# interaction.
|
44
|
+
#
|
45
|
+
#
|
46
|
+
# === Example
|
47
|
+
#
|
48
|
+
# class MyController
|
49
|
+
# def index
|
50
|
+
# objs = (0..200).to_a
|
51
|
+
# @entries, @pager = paginate(objs, :limit => 20)
|
52
|
+
# end
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
#
|
56
|
+
# <html>
|
57
|
+
# <head><title>Pager</title></head>
|
58
|
+
# <body>
|
59
|
+
# <?r if pager.navigation? ?>
|
60
|
+
# <div class="pager">#{@pager.navigation}</div>
|
61
|
+
# <?r end ?>
|
62
|
+
# <ul>
|
63
|
+
# <?r @entries.each do |entry| ?>
|
64
|
+
# <li>#{entry}</li>
|
65
|
+
# <?r end ?>
|
66
|
+
# </ul>
|
67
|
+
# </body>
|
68
|
+
# </html>
|
69
|
+
#
|
70
|
+
# === Styling
|
71
|
+
#
|
72
|
+
# The following classes can be used for styling with CSS (provided you put the
|
73
|
+
# pager in a element with class 'pager' like shown above):
|
74
|
+
#
|
75
|
+
# .pager {}
|
76
|
+
# .pager .first {}
|
77
|
+
# .pager .previous {}
|
78
|
+
# .pager .next {}
|
79
|
+
# .pager .last {}
|
80
|
+
# .pager ul {}
|
81
|
+
# .pager li {}
|
82
|
+
# .pager li.active {}
|
45
83
|
|
46
84
|
class Pager
|
47
85
|
include Ramaze::LinkHelper
|
@@ -56,19 +94,29 @@ class Pager
|
|
56
94
|
|
57
95
|
# The current page.
|
58
96
|
|
59
|
-
|
97
|
+
attr_reader :page
|
60
98
|
|
61
99
|
# Items per page.
|
62
100
|
|
63
|
-
|
101
|
+
attr_reader :limit
|
64
102
|
|
65
103
|
# The total number of pages.
|
66
104
|
|
67
|
-
|
105
|
+
attr_reader :page_count
|
68
106
|
|
69
107
|
# Total count of items.
|
70
108
|
|
71
|
-
|
109
|
+
attr_reader :total_count
|
110
|
+
|
111
|
+
# Create a new Pager object.
|
112
|
+
#
|
113
|
+
# request:: Ramaze::Request object providing access to GET parameters
|
114
|
+
# limit:: how many elements go to one page
|
115
|
+
# total_count:: total element count
|
116
|
+
# key:: key used for getting the current page from GET paramaters
|
117
|
+
#
|
118
|
+
# Note: You never have to create this class yourself, use the `paginate()`
|
119
|
+
# convenience method from the PagerHelper.
|
72
120
|
|
73
121
|
def initialize(request, limit, total_count, key = trait[:key])
|
74
122
|
raise 'limit should be > 0' unless limit > 0
|
@@ -80,11 +128,6 @@ class Pager
|
|
80
128
|
@start_idx = (@page - 1) * limit
|
81
129
|
end
|
82
130
|
|
83
|
-
def set_count(total_count)
|
84
|
-
@total_count = total_count
|
85
|
-
@page_count = (@total_count.to_f / @limit).ceil
|
86
|
-
end
|
87
|
-
|
88
131
|
# Return the first page index.
|
89
132
|
|
90
133
|
def first_page
|
@@ -121,12 +164,7 @@ class Pager
|
|
121
164
|
[@page + 1, @page_count].min
|
122
165
|
end
|
123
166
|
|
124
|
-
|
125
|
-
def link_last_page; target_uri(last_page); end
|
126
|
-
def link_prev_page; target_uri(prev_page); end
|
127
|
-
def link_next_page; target_uri(next_page); end
|
128
|
-
|
129
|
-
# Iterator
|
167
|
+
# Returns each element for the current page
|
130
168
|
|
131
169
|
def each(&block)
|
132
170
|
@page_items.each(&block)
|
@@ -135,60 +173,27 @@ class Pager
|
|
135
173
|
# Iterator
|
136
174
|
# Returns 1-based index.
|
137
175
|
|
138
|
-
def each_with_index
|
139
|
-
|
140
|
-
for item in @page_items
|
141
|
-
yield(idx + 1, item)
|
142
|
-
idx += 1
|
143
|
-
end
|
176
|
+
def each_with_index(&block)
|
177
|
+
@page_items.each_with_index(&block)
|
144
178
|
end
|
145
179
|
|
146
180
|
# Is the pager empty, ie has one page only?
|
147
181
|
|
148
182
|
def empty?
|
149
|
-
@page_count <
|
183
|
+
@page_count < 2
|
150
184
|
end
|
151
185
|
|
152
|
-
#
|
186
|
+
# Returns true if a navigation is necessary (meaning there is more than one
|
187
|
+
# page)
|
153
188
|
|
154
|
-
def
|
155
|
-
|
156
|
-
end
|
157
|
-
|
158
|
-
# Returns the range of the current page.
|
159
|
-
|
160
|
-
def page_range
|
161
|
-
s = @idx
|
162
|
-
e = [@idx + @items_limit - 1, all_total_count].min
|
163
|
-
|
164
|
-
return [s, e]
|
165
|
-
end
|
166
|
-
|
167
|
-
# Override if needed.
|
168
|
-
|
169
|
-
def nav_range
|
170
|
-
# effective range = 10 pages.
|
171
|
-
s = [@page - 5, 1].max
|
172
|
-
e = [@page + 9, @page_count].min
|
173
|
-
|
174
|
-
d = 9 - (e - s)
|
175
|
-
e += d if d < 0
|
176
|
-
|
177
|
-
return (s..e)
|
189
|
+
def navigation?
|
190
|
+
!empty?
|
178
191
|
end
|
179
192
|
|
180
|
-
#
|
181
|
-
|
182
|
-
def limit
|
183
|
-
if @start_idx > 0
|
184
|
-
{ :limit => @limit, :offset => @start_idx }
|
185
|
-
else
|
186
|
-
{ :limit => @limit }
|
187
|
-
end
|
188
|
-
end
|
193
|
+
# Returns the amount of all elements in all pages.
|
189
194
|
|
190
|
-
def
|
191
|
-
@
|
195
|
+
def size
|
196
|
+
@total_count
|
192
197
|
end
|
193
198
|
|
194
199
|
# Override this method in your application if needed.
|
@@ -217,13 +222,9 @@ class Pager
|
|
217
222
|
|
218
223
|
for i in nav_range()
|
219
224
|
if i == @page
|
220
|
-
nav << %{
|
221
|
-
<li class="active">#{i}</li>
|
222
|
-
}
|
225
|
+
nav << %{<li class="active">#{i}</li>}
|
223
226
|
else
|
224
|
-
nav << %{
|
225
|
-
<li><a href="#{target_uri(i)}">#{i}</a></li>
|
226
|
-
}
|
227
|
+
nav << %{<li><a href="#{target_uri(i)}">#{i}</a></li>}
|
227
228
|
end
|
228
229
|
end
|
229
230
|
|
@@ -232,8 +233,20 @@ class Pager
|
|
232
233
|
return nav
|
233
234
|
end
|
234
235
|
|
235
|
-
|
236
|
-
|
236
|
+
# To be used with Og queries.
|
237
|
+
|
238
|
+
def limit
|
239
|
+
if @start_idx > 0
|
240
|
+
{ :limit => @limit, :offset => @start_idx }
|
241
|
+
else
|
242
|
+
{ :limit => @limit }
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
# Returns the index of the first element to go into the current page
|
247
|
+
|
248
|
+
def offset
|
249
|
+
@start_idx
|
237
250
|
end
|
238
251
|
|
239
252
|
private
|
@@ -241,10 +254,57 @@ private
|
|
241
254
|
# Generate the target URI.
|
242
255
|
|
243
256
|
def target_uri(page)
|
244
|
-
params =
|
257
|
+
params = @request.params.merge(@key => page)
|
245
258
|
Rs(Action.current.method, params)
|
246
259
|
end
|
247
260
|
|
261
|
+
# Generate link for the first page.
|
262
|
+
|
263
|
+
def link_first_page; target_uri(first_page); end
|
264
|
+
|
265
|
+
# Generate link for the last page.
|
266
|
+
|
267
|
+
def link_last_page; target_uri(last_page); end
|
268
|
+
|
269
|
+
# Generate link for the previous page.
|
270
|
+
|
271
|
+
def link_prev_page; target_uri(prev_page); end
|
272
|
+
|
273
|
+
# Generate link for the next page.
|
274
|
+
|
275
|
+
def link_next_page; target_uri(next_page); end
|
276
|
+
|
277
|
+
|
278
|
+
# Returns the range of the current page.
|
279
|
+
|
280
|
+
def page_range
|
281
|
+
s = @idx
|
282
|
+
e = [@idx + @items_limit - 1, all_total_count].min
|
283
|
+
|
284
|
+
return [s, e]
|
285
|
+
end
|
286
|
+
|
287
|
+
# Returns the range of
|
288
|
+
# Override if needed.
|
289
|
+
|
290
|
+
def nav_range
|
291
|
+
# effective range = 10 pages.
|
292
|
+
s = [@page - 5, 1].max
|
293
|
+
e = [@page + 9, @page_count].min
|
294
|
+
|
295
|
+
d = 9 - (e - s)
|
296
|
+
e += d if d < 0
|
297
|
+
|
298
|
+
return (s..e)
|
299
|
+
end
|
300
|
+
|
301
|
+
# generates total and page count of the pager.
|
302
|
+
|
303
|
+
def set_count(total_count)
|
304
|
+
@total_count = total_count
|
305
|
+
@page_count = (@total_count.to_f / @limit).ceil
|
306
|
+
end
|
307
|
+
|
248
308
|
end
|
249
309
|
|
250
310
|
# Pager related helper methods.
|
@@ -258,50 +318,48 @@ private
|
|
258
318
|
#
|
259
319
|
# === Example
|
260
320
|
#
|
261
|
-
# entries, pager = paginate(Article, :where => 'title LIKE..', :limit => 10)
|
262
|
-
#
|
263
|
-
# or
|
264
|
-
#
|
265
321
|
# items = [ 'item1', 'item2', ... ]
|
266
322
|
# entries, pager = paginate(items, :limit => 10)
|
267
323
|
#
|
268
|
-
# or
|
269
|
-
#
|
270
|
-
# entries, pager = paginate(article.comments, :limit => 10)
|
271
|
-
#
|
272
324
|
# <ul>
|
273
325
|
# <?r for entry in entries ?>
|
274
326
|
# <li>#{entry.to_link}</li>
|
275
327
|
# <?r end ?>
|
276
328
|
# </ul>
|
277
329
|
# #{pager.navigation}
|
330
|
+
#
|
331
|
+
# === Og Example
|
332
|
+
#
|
333
|
+
# entries, pager = paginate(Article, :where => 'title LIKE..', :limit => 10)
|
334
|
+
#
|
335
|
+
# or
|
336
|
+
#
|
337
|
+
# entries, pager = paginate(article.comments, :limit => 10)
|
278
338
|
|
279
339
|
def paginate(items, options = {})
|
280
|
-
limit = options.delete(:limit) ||
|
340
|
+
limit = options.delete(:limit) || Pager.trait[:limit]
|
281
341
|
pager_key = options.delete(:pager_key) || Pager.trait[:key]
|
282
342
|
|
283
343
|
case items
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
344
|
+
when Array
|
345
|
+
pager = Pager.new(request, limit, items.size, pager_key)
|
346
|
+
items = items.slice(pager.offset, pager.limit[:limit])
|
347
|
+
return items, pager
|
288
348
|
end
|
289
349
|
|
290
|
-
if defined?(Og)
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
options.update(pager.limit)
|
301
|
-
items = items.all(options)
|
302
|
-
return items, pager
|
303
|
-
end
|
350
|
+
if defined?(Og) && items.is_a?(Og::Collection)
|
351
|
+
pager = Pager.new(request, limit, items.count, pager_key)
|
352
|
+
options.update(pager.limit)
|
353
|
+
items = items.reload(options)
|
354
|
+
return items, pager
|
355
|
+
elsif defined?(Og) && items.is_a?(Og::Mixin)
|
356
|
+
pager = Pager.new(request, limit, items.count(options), pager_key)
|
357
|
+
options.update(pager.limit)
|
358
|
+
items = items.all(options)
|
359
|
+
return items, pager
|
304
360
|
end
|
361
|
+
|
362
|
+
raise "No suitable pagination method for #{items.inspect}"
|
305
363
|
end
|
306
364
|
|
307
365
|
end
|