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
52
|
# File 'lib/simply_couch/model/pagination_options.rb', line 4
def (options, &block)
total_entries = options.delete(:total_entries)
if ([:page, :per_page] & options.keys).any? page = [options.delete(:page).to_i, 1].max
per_page = options.delete(:per_page).to_i per_page = 22 unless per_page > 0 options[:limit] = per_page
options[:skip] = (page - 1) * options[:limit]
elsif options[:offset] && options[:limit] options[:skip] = options.delete(:offset)
options.delete(:order)
page = (options[:skip].to_i / options[:limit].to_i).floor.succ
per_page = options[:limit]
else
page = 1
per_page = 22
end
result = block.call(options)
if result
result.instance_eval <<-PAGINATION, __FILE__, __LINE__
unless respond_to?(:total_rows)
def total_rows
1
end
end
def total_entries
#{total_entries || 'total_rows'}
end
alias total_count total_entries
def #{current_page_method}
#{page}
end
def #{num_pages_method}
return 1 if total_entries.zero?
(total_entries.to_f / #{per_page}).ceil
end
alias :#{total_pages_method} :#{num_pages_method}
def #{per_page_method}
#{per_page}
end
PAGINATION
end
result
end
|