ramaze 2010.06.18 → 2011.01
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/.gitignore +1 -0
- data/MANIFEST +9 -16
- data/README.md +37 -30
- data/Rakefile +5 -1
- data/TODO.md +19 -0
- data/doc/AUTHORS +5 -1
- data/doc/CHANGELOG +3553 -3272
- data/doc/tutorial/todolist.html +1512 -1512
- data/examples/app/blog/app.rb +2 -0
- data/examples/app/todolist/controller/init.rb +1 -2
- data/examples/app/wiktacular/mkd/main/2007-07-20_19-21-12.mkd +1 -1
- data/examples/app/wiktacular/mkd/main/2007-07-20_19-23-10.mkd +1 -1
- data/examples/app/wiktacular/mkd/main/2007-07-20_19-45-07.mkd +1 -1
- data/examples/app/wiktacular/mkd/main/current.mkd +1 -1
- data/examples/app/wiktacular/mkd/testing/2007-07-20_16-43-46.mkd +1 -1
- data/examples/app/wiktacular/mkd/testing/2007-07-20_19-43-50.mkd +2 -2
- data/examples/app/wiktacular/mkd/testing/2007-07-21_18-47-08.mkd +16 -16
- data/examples/app/wiktacular/mkd/testing/2007-07-21_18-47-54.mkd +16 -16
- data/examples/app/wiktacular/mkd/testing/current.mkd +16 -16
- data/lib/proto/model/init.rb +1 -1
- data/lib/proto/public/js/jquery.js +2034 -1095
- data/lib/proto/start.rb +2 -0
- data/lib/proto/view/index.xhtml +3 -3
- data/lib/ramaze.rb +1 -2
- data/lib/ramaze/cache.rb +1 -0
- data/lib/ramaze/cache/sequel.rb +131 -37
- data/lib/ramaze/controller.rb +1 -0
- data/lib/ramaze/gestalt.rb +75 -46
- data/lib/ramaze/helper.rb +1 -0
- data/lib/ramaze/helper/auth.rb +38 -4
- data/lib/ramaze/helper/blue_form.rb +498 -78
- data/lib/ramaze/helper/cache.rb +2 -2
- data/lib/ramaze/helper/csrf.rb +225 -0
- data/lib/ramaze/helper/erector.rb +67 -9
- data/lib/ramaze/helper/flash.rb +4 -2
- data/lib/ramaze/helper/gestalt.rb +2 -0
- data/lib/ramaze/helper/gravatar.rb +1 -1
- data/lib/ramaze/helper/localize.rb +4 -0
- data/lib/ramaze/helper/send_file.rb +30 -0
- data/lib/ramaze/helper/thread.rb +5 -0
- data/lib/ramaze/helper/user.rb +4 -3
- data/lib/ramaze/helper/xhtml.rb +87 -8
- data/lib/ramaze/log.rb +13 -0
- data/lib/ramaze/log/analogger.rb +15 -5
- data/lib/ramaze/log/growl.rb +28 -13
- data/lib/ramaze/log/hub.rb +12 -4
- data/lib/ramaze/log/informer.rb +28 -11
- data/lib/ramaze/log/knotify.rb +7 -2
- data/lib/ramaze/log/logger.rb +12 -4
- data/lib/ramaze/log/logging.rb +40 -14
- data/lib/ramaze/log/rotatinginformer.rb +47 -23
- data/lib/ramaze/log/syslog.rb +37 -31
- data/lib/ramaze/log/xosd.rb +7 -4
- data/lib/ramaze/middleware_compiler.rb +2 -2
- data/lib/ramaze/snippets/fiber.rb +63 -63
- data/lib/ramaze/snippets/ramaze/lru_hash.rb +1 -1
- data/lib/ramaze/tool/bin.rb +1 -1
- data/lib/ramaze/version.rb +1 -1
- data/lib/ramaze/view.rb +4 -4
- data/lib/ramaze/view/erector.rb +88 -13
- data/ramaze.gemspec +65 -65
- data/spec/ramaze/bin/ramaze.rb +1 -1
- data/spec/ramaze/cache/localmemcache.rb +20 -12
- data/spec/ramaze/cache/sequel.rb +19 -19
- data/spec/ramaze/helper/blue_form.rb +549 -257
- data/spec/ramaze/helper/csrf.rb +109 -0
- data/spec/ramaze/helper/httpdigest.rb +31 -29
- data/spec/ramaze/helper/user.rb +1 -1
- data/spec/ramaze/helper/xhtml.rb +17 -0
- data/spec/ramaze/log/growl.rb +34 -0
- data/spec/ramaze/log/informer.rb +1 -0
- data/spec/ramaze/view/erector.rb +49 -71
- data/spec/ramaze/view/erector/external_view.erector +5 -0
- data/spec/ramaze/view/erector/index.erector +5 -0
- data/spec/ramaze/view/erector/layout.erector +13 -3
- data/spec/ramaze/view/erector/tables.erector +23 -0
- data/spec/ramaze/view/erector/view.erector +6 -0
- data/tasks/git.rake +2 -2
- metadata +133 -176
- data/examples/helpers/form_with_sequel.rb +0 -24
- data/examples/helpers/nitro_form.rb +0 -23
- data/lib/ramaze/helper/form.rb +0 -133
- data/lib/ramaze/helper/nitroform.rb +0 -14
- data/lib/ramaze/helper/pager.rb +0 -367
- data/lib/ramaze/helper/partial.rb +0 -100
- data/lib/ramaze/helper/sequel.rb +0 -55
- data/lib/ramaze/helper/sequel_form.rb +0 -284
- data/lib/vendor/etag.rb +0 -22
- data/spec/ramaze/helper/form.rb +0 -360
- data/spec/ramaze/helper/pager.rb +0 -96
- data/spec/ramaze/helper/sequel_form.rb +0 -94
- data/spec/ramaze/view/erector/external.erector +0 -1
- data/spec/ramaze/view/erector/invoke_helper_method.erector +0 -1
- data/spec/ramaze/view/erector/strict_xhtml.erector +0 -3
- data/spec/ramaze/view/erector/sum.erector +0 -1
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'sequel'
|
2
|
-
require 'ramaze'
|
3
|
-
|
4
|
-
DB = Sequel.sqlite
|
5
|
-
|
6
|
-
class User < Sequel::Model
|
7
|
-
set_schema do
|
8
|
-
varchar :name
|
9
|
-
text :description
|
10
|
-
date :created
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
User.create_table
|
15
|
-
|
16
|
-
class MainController < Ramaze::Controller
|
17
|
-
helper :form
|
18
|
-
|
19
|
-
def index
|
20
|
-
form_for User
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
Ramaze.start
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'ramaze'
|
3
|
-
|
4
|
-
gem 'facets', '=1.4.5'
|
5
|
-
require '/home/manveru/prog/projects/nitroproject/glycerin'
|
6
|
-
require 'nitro'
|
7
|
-
require 'og'
|
8
|
-
|
9
|
-
class Article
|
10
|
-
attr_accessor :title, String
|
11
|
-
end
|
12
|
-
|
13
|
-
Og.setup :store => :sqlite, :destroy => true
|
14
|
-
|
15
|
-
class MainController < Ramaze::Controller
|
16
|
-
helper :nitroform
|
17
|
-
|
18
|
-
def index
|
19
|
-
form(Article.new).to_s
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
Ramaze.start
|
data/lib/ramaze/helper/form.rb
DELETED
@@ -1,133 +0,0 @@
|
|
1
|
-
require 'ramaze/gestalt'
|
2
|
-
|
3
|
-
module Ramaze
|
4
|
-
module Helper
|
5
|
-
module Form
|
6
|
-
def form_text(label, name, value = nil)
|
7
|
-
form_input(label, :type => :text, :name => name, :value => value)
|
8
|
-
end
|
9
|
-
|
10
|
-
def form_checkbox(label, name, checked = false)
|
11
|
-
hash = {:type => :checkbox, :name => name}
|
12
|
-
hash[:checked] = 'checked' if checked
|
13
|
-
form_input(label, hash)
|
14
|
-
end
|
15
|
-
|
16
|
-
def form_checkbox_tag(label, name, value, checked = false)
|
17
|
-
hash = {:type => :checkbox, :name => name + '[]', :value => value}
|
18
|
-
hash[:checked] = 'checked' if checked
|
19
|
-
form_input(label, hash)
|
20
|
-
end
|
21
|
-
|
22
|
-
def form_password(label, name)
|
23
|
-
form_input(label, :type => :password, :name => name)
|
24
|
-
end
|
25
|
-
|
26
|
-
def form_textarea(label, name, value = nil)
|
27
|
-
form_build(:textarea, label, :name => name){ value }
|
28
|
-
end
|
29
|
-
|
30
|
-
def form_file(label, name)
|
31
|
-
form_input(label, :type => :file, :name => name)
|
32
|
-
end
|
33
|
-
|
34
|
-
def form_hidden(name, value = nil)
|
35
|
-
Ramaze::Gestalt.build{ input(:type => :hidden, :name => name, :value => value) }
|
36
|
-
end
|
37
|
-
|
38
|
-
def form_submit(value = nil)
|
39
|
-
hash = {:type => :submit}.merge(form_tabindex)
|
40
|
-
hash[:value] = value if value
|
41
|
-
Ramaze::Gestalt.build{ tr{ td(:colspan => 2){ input(hash) }}}
|
42
|
-
end
|
43
|
-
|
44
|
-
# @example usage, normal select drop-down
|
45
|
-
#
|
46
|
-
# form_select('Favourite colors', :colors, @colors, :selected => @color)
|
47
|
-
#
|
48
|
-
# @example usage for pre-selected value
|
49
|
-
#
|
50
|
-
# form_select('Favourite colors', :colors, @colors, :selected => @color)
|
51
|
-
#
|
52
|
-
# @example usage, allow selecting multiple
|
53
|
-
#
|
54
|
-
# form_select('Cups', :cups, @cups, :selected => @cup, :multiple => 5)
|
55
|
-
def form_select(label, name, values, hash = {})
|
56
|
-
name = name.to_sym
|
57
|
-
id = "form-#{name}"
|
58
|
-
multiple, size = hash.values_at(:multiple, :size)
|
59
|
-
|
60
|
-
s_args = {:name => name, :id => id}.merge(form_tabindex)
|
61
|
-
s_args[:multiple] = :multiple if multiple
|
62
|
-
s_args[:size] = (size || multiple || 1).to_i
|
63
|
-
|
64
|
-
has_selected, selected = hash.key?(:selected), hash[:selected]
|
65
|
-
error = form_errors[name.to_s]
|
66
|
-
|
67
|
-
g = Ramaze::Gestalt.new
|
68
|
-
g.tr do
|
69
|
-
g.td do
|
70
|
-
g.label(:for => id){ "#{label}:" }
|
71
|
-
g.span(:class => 'error'){ error } if error
|
72
|
-
end
|
73
|
-
g.td do
|
74
|
-
g.select(s_args) do
|
75
|
-
values.each do |key, value|
|
76
|
-
value ||= key
|
77
|
-
o_args = {:value => value}
|
78
|
-
o_args[:selected] = :selected if has_selected and value == selected
|
79
|
-
g.option(o_args){ key }
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
g.to_s
|
86
|
-
end
|
87
|
-
|
88
|
-
def form_input(label, hash)
|
89
|
-
form_build(:input, label, hash)
|
90
|
-
end
|
91
|
-
|
92
|
-
def form_build(tag_name, label, hash, &block)
|
93
|
-
name = hash[:name].to_sym
|
94
|
-
form_id = "form-#{name}"
|
95
|
-
opts = hash.merge(form_tabindex.merge(:id => form_id))
|
96
|
-
error = form_errors[name.to_s]
|
97
|
-
|
98
|
-
Ramaze::Gestalt.build do
|
99
|
-
tr do
|
100
|
-
td do
|
101
|
-
label(:for => form_id){ "#{label}:" } unless label==''
|
102
|
-
span(:class => "error"){ error } if error
|
103
|
-
end
|
104
|
-
td do
|
105
|
-
tag(tag_name, opts, &block)
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
def form_tabindex
|
112
|
-
@tabindex ||= 0
|
113
|
-
@tabindex += 1
|
114
|
-
|
115
|
-
{:tabindex => @tabindex}
|
116
|
-
end
|
117
|
-
|
118
|
-
def form_error(name, message)
|
119
|
-
form_errors[name.to_s] = message.to_s
|
120
|
-
end
|
121
|
-
|
122
|
-
def form_errors
|
123
|
-
@form_errors ||= {}
|
124
|
-
end
|
125
|
-
|
126
|
-
def form_errors_from_model(obj)
|
127
|
-
obj.errors.each do |key, value|
|
128
|
-
form_error(key, value.first % key)
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
# Copyright (c) 2008 Michael Fellinger m.fellinger@gmail.com
|
2
|
-
# All files in this distribution are subject to the terms of the Ruby license.
|
3
|
-
|
4
|
-
require 'nitro/helper/form'
|
5
|
-
|
6
|
-
module Ramaze
|
7
|
-
module Helper
|
8
|
-
|
9
|
-
# This helper simply includes the Nitro::FormHelper so you can use its methods
|
10
|
-
# in your Controller.
|
11
|
-
|
12
|
-
Nitroform = ::Nitro::FormHelper
|
13
|
-
end
|
14
|
-
end
|
data/lib/ramaze/helper/pager.rb
DELETED
@@ -1,367 +0,0 @@
|
|
1
|
-
module Ramaze
|
2
|
-
|
3
|
-
# The BSD License
|
4
|
-
#
|
5
|
-
# Copyright (c) 2004-2007, George K. Moschovitis. (http://www.gmosx.com)
|
6
|
-
# All rights reserved.
|
7
|
-
#
|
8
|
-
# Redistribution and use in source and binary forms, with or without
|
9
|
-
# modification, are permitted provided that the following conditions are
|
10
|
-
# met:
|
11
|
-
#
|
12
|
-
# * Redistributions of source code must retain the above copyright
|
13
|
-
# notice, this list of conditions and the following disclaimer.
|
14
|
-
#
|
15
|
-
# * Redistributions in binary form must reproduce the above copyright
|
16
|
-
# notice, this list of conditions and the following disclaimer in the
|
17
|
-
# documentation and/or other materials provided with the distribution.
|
18
|
-
#
|
19
|
-
# * Neither the name of Nitro nor the names of its contributors may be
|
20
|
-
# used to endorse or promote products derived from this software
|
21
|
-
# without specific prior written permission.
|
22
|
-
#
|
23
|
-
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
24
|
-
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
25
|
-
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
26
|
-
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
27
|
-
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
28
|
-
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
29
|
-
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
30
|
-
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
31
|
-
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
32
|
-
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
33
|
-
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34
|
-
#
|
35
|
-
|
36
|
-
# Displays a collection of entitities in multiple pages.
|
37
|
-
#
|
38
|
-
# === Design
|
39
|
-
#
|
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
|
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 {}
|
83
|
-
|
84
|
-
class Pager
|
85
|
-
include Ramaze::Helper::Link
|
86
|
-
include Ramaze::Traited
|
87
|
-
|
88
|
-
# Items per page.
|
89
|
-
|
90
|
-
trait :limit => 10
|
91
|
-
|
92
|
-
# The request key.
|
93
|
-
|
94
|
-
trait :key => '_page'
|
95
|
-
|
96
|
-
# The current page.
|
97
|
-
|
98
|
-
attr_reader :page
|
99
|
-
|
100
|
-
# Items per page.
|
101
|
-
|
102
|
-
attr_reader :limit
|
103
|
-
|
104
|
-
# The total number of pages.
|
105
|
-
|
106
|
-
attr_reader :page_count
|
107
|
-
|
108
|
-
# Total count of items.
|
109
|
-
|
110
|
-
attr_reader :total_count
|
111
|
-
|
112
|
-
# Create a new Pager object.
|
113
|
-
#
|
114
|
-
# request:: Ramaze::Request object providing access to GET parameters
|
115
|
-
# limit:: how many elements go to one page
|
116
|
-
# total_count:: total element count
|
117
|
-
# key:: key used for getting the current page from GET paramaters
|
118
|
-
#
|
119
|
-
# Note: You never have to create this class yourself, use the `paginate()`
|
120
|
-
# convenience method from the Helper::Pager.
|
121
|
-
|
122
|
-
def initialize(request, limit, total_count, key = trait[:key])
|
123
|
-
raise 'limit should be > 0' unless limit > 0
|
124
|
-
|
125
|
-
@request, @key = request, key
|
126
|
-
@page = (request.params[key] || 1).to_i
|
127
|
-
@limit = limit
|
128
|
-
set_count(total_count)
|
129
|
-
@start_idx = (@page - 1) * limit
|
130
|
-
end
|
131
|
-
|
132
|
-
# Return the first page index.
|
133
|
-
|
134
|
-
def first_page
|
135
|
-
1
|
136
|
-
end
|
137
|
-
|
138
|
-
# Is the first page displayed?
|
139
|
-
|
140
|
-
def first_page?
|
141
|
-
@page == 1
|
142
|
-
end
|
143
|
-
|
144
|
-
# Return the last page index.
|
145
|
-
|
146
|
-
def last_page
|
147
|
-
return @page_count
|
148
|
-
end
|
149
|
-
|
150
|
-
# Is the last page displayed?
|
151
|
-
|
152
|
-
def last_page?
|
153
|
-
@page == @page_count
|
154
|
-
end
|
155
|
-
|
156
|
-
# Return the index of the previous page.
|
157
|
-
|
158
|
-
def prev_page
|
159
|
-
[@page - 1, 1].max
|
160
|
-
end
|
161
|
-
|
162
|
-
# Return the index of the next page.
|
163
|
-
|
164
|
-
def next_page
|
165
|
-
[@page + 1, @page_count].min
|
166
|
-
end
|
167
|
-
|
168
|
-
# Returns each element for the current page
|
169
|
-
|
170
|
-
def each(&block)
|
171
|
-
@page_items.each(&block)
|
172
|
-
end
|
173
|
-
|
174
|
-
# Iterator
|
175
|
-
# Returns 1-based index.
|
176
|
-
|
177
|
-
def each_with_index(&block)
|
178
|
-
@page_items.each_with_index(&block)
|
179
|
-
end
|
180
|
-
|
181
|
-
# Is the pager empty, ie has one page only?
|
182
|
-
|
183
|
-
def empty?
|
184
|
-
@page_count < 2
|
185
|
-
end
|
186
|
-
|
187
|
-
# Returns true if a navigation is necessary (meaning there is more than one
|
188
|
-
# page)
|
189
|
-
|
190
|
-
def navigation?
|
191
|
-
!empty?
|
192
|
-
end
|
193
|
-
|
194
|
-
# Returns the amount of all elements in all pages.
|
195
|
-
|
196
|
-
def size
|
197
|
-
@total_count
|
198
|
-
end
|
199
|
-
|
200
|
-
# Override this method in your application if needed.
|
201
|
-
#--
|
202
|
-
# TODO: better markup.
|
203
|
-
#++
|
204
|
-
|
205
|
-
def navigation
|
206
|
-
nav = ""
|
207
|
-
|
208
|
-
unless first_page?
|
209
|
-
nav << %{
|
210
|
-
<div class="first"><a href="#{link_first_page}">First</a></div>
|
211
|
-
<div class="previous"><a href="#{link_prev_page}">Previous</a></div>
|
212
|
-
}
|
213
|
-
end
|
214
|
-
|
215
|
-
unless last_page?
|
216
|
-
nav << %{
|
217
|
-
<div class="last"><a href="#{link_last_page}">Last</a></div>
|
218
|
-
<div class="next"><a href="#{link_next_page}">Next</a></div>
|
219
|
-
}
|
220
|
-
end
|
221
|
-
|
222
|
-
nav << %{<ul>}
|
223
|
-
|
224
|
-
for i in nav_range()
|
225
|
-
if i == @page
|
226
|
-
nav << %{<li class="active">#{i}</li>}
|
227
|
-
else
|
228
|
-
nav << %{<li><a href="#{target_uri(i)}">#{i}</a></li>}
|
229
|
-
end
|
230
|
-
end
|
231
|
-
|
232
|
-
nav << %{</ul>}
|
233
|
-
|
234
|
-
return nav
|
235
|
-
end
|
236
|
-
|
237
|
-
# To be used with Og queries.
|
238
|
-
|
239
|
-
def limit
|
240
|
-
if @start_idx > 0
|
241
|
-
{ :limit => @limit, :offset => @start_idx }
|
242
|
-
else
|
243
|
-
{ :limit => @limit }
|
244
|
-
end
|
245
|
-
end
|
246
|
-
|
247
|
-
# Returns the index of the first element to go into the current page
|
248
|
-
|
249
|
-
def offset
|
250
|
-
@start_idx
|
251
|
-
end
|
252
|
-
|
253
|
-
private
|
254
|
-
|
255
|
-
# Generate the target URI.
|
256
|
-
|
257
|
-
def target_uri(page)
|
258
|
-
params = @request.params.merge(@key => page)
|
259
|
-
Current.action.node.r(Current.action.name, params)
|
260
|
-
end
|
261
|
-
|
262
|
-
# Generate link for the first page.
|
263
|
-
|
264
|
-
def link_first_page; target_uri(first_page); end
|
265
|
-
|
266
|
-
# Generate link for the last page.
|
267
|
-
|
268
|
-
def link_last_page; target_uri(last_page); end
|
269
|
-
|
270
|
-
# Generate link for the previous page.
|
271
|
-
|
272
|
-
def link_prev_page; target_uri(prev_page); end
|
273
|
-
|
274
|
-
# Generate link for the next page.
|
275
|
-
|
276
|
-
def link_next_page; target_uri(next_page); end
|
277
|
-
|
278
|
-
|
279
|
-
# Returns the range of the current page.
|
280
|
-
|
281
|
-
def page_range
|
282
|
-
s = @idx
|
283
|
-
e = [@idx + @items_limit - 1, all_total_count].min
|
284
|
-
|
285
|
-
return [s, e]
|
286
|
-
end
|
287
|
-
|
288
|
-
# Returns the range of
|
289
|
-
# Override if needed.
|
290
|
-
|
291
|
-
def nav_range
|
292
|
-
# effective range = 10 pages.
|
293
|
-
s = [@page - 5, 1].max
|
294
|
-
e = [@page + 9, @page_count].min
|
295
|
-
|
296
|
-
d = 9 - (e - s)
|
297
|
-
e += d if d < 0
|
298
|
-
|
299
|
-
return (s..e)
|
300
|
-
end
|
301
|
-
|
302
|
-
# generates total and page count of the pager.
|
303
|
-
|
304
|
-
def set_count(total_count)
|
305
|
-
@total_count = total_count
|
306
|
-
@page_count = (@total_count.to_f / @limit).ceil
|
307
|
-
end
|
308
|
-
|
309
|
-
end
|
310
|
-
|
311
|
-
# Pager related helper methods.
|
312
|
-
|
313
|
-
module Helper::Pager
|
314
|
-
|
315
|
-
# Helper method that generates a collection of items and the
|
316
|
-
# associated pager object.
|
317
|
-
#
|
318
|
-
# === Example
|
319
|
-
#
|
320
|
-
# items = [ 'item1', 'item2', ... ]
|
321
|
-
# entries, pager = paginate(items, :limit => 10)
|
322
|
-
#
|
323
|
-
# <ul>
|
324
|
-
# <?r for entry in entries ?>
|
325
|
-
# <li>#{entry.to_link}</li>
|
326
|
-
# <?r end ?>
|
327
|
-
# </ul>
|
328
|
-
# #{pager.navigation}
|
329
|
-
#
|
330
|
-
# === Og Example
|
331
|
-
#
|
332
|
-
# entries, pager = paginate(Article, :where => 'title LIKE..', :limit => 10)
|
333
|
-
#
|
334
|
-
# or
|
335
|
-
#
|
336
|
-
# entries, pager = paginate(article.comments, :limit => 10)
|
337
|
-
|
338
|
-
def paginate(items, options = {})
|
339
|
-
limit = options.delete(:limit) || Pager.trait[:limit]
|
340
|
-
pager_key = options.delete(:pager_key) || Pager.trait[:key]
|
341
|
-
|
342
|
-
case items
|
343
|
-
when Array
|
344
|
-
pager = Pager.new(request, limit, items.size, pager_key)
|
345
|
-
items = items.slice(pager.offset, pager.limit[:limit])
|
346
|
-
return items, pager
|
347
|
-
end
|
348
|
-
|
349
|
-
if defined?(Og) && items.is_a?(Og::Collection)
|
350
|
-
pager = Pager.new(request, limit, items.count, pager_key)
|
351
|
-
options.update(pager.limit)
|
352
|
-
items = items.reload(options)
|
353
|
-
return items, pager
|
354
|
-
elsif defined?(Og::EntityMixin) && items.is_a?(Og::EntityMixin) ||
|
355
|
-
defined?(Og::Mixin) && items.is_a?(Og::Mixin) # Og <= 0.41
|
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
|
360
|
-
end
|
361
|
-
|
362
|
-
raise "No suitable pagination method for #{items.inspect}"
|
363
|
-
end
|
364
|
-
|
365
|
-
end
|
366
|
-
|
367
|
-
end
|