simple_record 1.3.4 → 1.3.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/simple_record.rb CHANGED
@@ -879,30 +879,15 @@ module SimpleRecord
879
879
  def self.paginate(options={})
880
880
  # options = args.pop
881
881
  # puts 'paginate options=' + options.inspect if SimpleRecord.logging?
882
- page = options[:page] || 1
883
- per_page = options[:per_page] || self.per_page || 50
884
- total = options[:total_entries]
882
+ page = options[:page].to_i || 1
883
+ per_page = options[:per_page].to_i || self.per_page || 50
884
+ # total = options[:total_entries].to_i
885
+ options[:page] = page # makes sure it's to_i
886
+ options[:per_page] = per_page
885
887
  options[:limit] = page * per_page
886
888
  fr = find(:all, options)
887
- # puts 'fr.size=' + fr.size.to_s
888
- ret = []
889
- i = 0
890
- p = 1
891
- fr.each do |x|
892
- # puts 'x=' + x.inspect
893
- if p == page
894
- # puts 'adding'
895
- ret << x
896
- end
897
- i += 1
898
- if i > 0 && i % per_page == 0
899
- p += 1
900
- if p > page
901
- break
902
- end
903
- end
904
- end
905
- ret
889
+ return fr
890
+
906
891
  end
907
892
 
908
893
 
@@ -19,6 +19,11 @@ module SimpleRecord
19
19
  @items = items
20
20
  @currentset_items = items
21
21
  @next_token = next_token
22
+ @options = @params[1]
23
+ if @options[:page]
24
+ load_to(@options[:per_page] * @options[:page])
25
+ @start_at = @options[:per_page] * (@options[:page] - 1)
26
+ end
22
27
  end
23
28
 
24
29
  def << (val)
@@ -80,9 +85,10 @@ module SimpleRecord
80
85
  return @count if @count
81
86
  params_for_count = params.dup
82
87
  params_for_count[0] = :count
83
- #puts 'params_for_count=' + params_for_count.inspect
88
+ params_for_count[1].delete(:limit)
89
+ puts 'params_for_count=' + params_for_count.inspect
84
90
  @count = clz.find(*params_for_count)
85
- # puts '@count=' + @count.to_s
91
+ puts '@count=' + @count.to_s
86
92
  @count
87
93
  end
88
94
 
@@ -91,7 +97,7 @@ module SimpleRecord
91
97
  end
92
98
 
93
99
  def each(&blk)
94
- each2(0, &blk)
100
+ each2((@start_at || 0), &blk)
95
101
  end
96
102
 
97
103
  def each2(i, &blk)
@@ -119,6 +125,53 @@ module SimpleRecord
119
125
  end
120
126
  end
121
127
 
128
+ # for will_paginate support
129
+ def total_pages
130
+ puts 'total_pages'
131
+ puts @params[1][:per_page].to_s
132
+ return 1 if @params[1][:per_page].nil?
133
+ ret = (size / @params[1][:per_page].to_f).ceil
134
+ puts 'ret=' + ret.to_s
135
+ ret
136
+ end
137
+
138
+ def current_page
139
+ return query_options[:page] || 1
140
+ end
141
+
142
+ def query_options
143
+ return @options
144
+ end
145
+
146
+ def total_entries
147
+ return size
148
+ end
149
+
150
+ # Helper method that is true when someone tries to fetch a page with a
151
+ # larger number than the last page. Can be used in combination with flashes
152
+ # and redirecting.
153
+ def out_of_bounds?
154
+ current_page > total_pages
155
+ end
156
+
157
+ # Current offset of the paginated collection. If we're on the first page,
158
+ # it is always 0. If we're on the 2nd page and there are 30 entries per page,
159
+ # the offset is 30. This property is useful if you want to render ordinals
160
+ # side by side with records in the view: simply start with offset + 1.
161
+ def offset
162
+ (current_page - 1) * per_page
163
+ end
164
+
165
+ # current_page - 1 or nil if there is no previous page
166
+ def previous_page
167
+ current_page > 1 ? (current_page - 1) : nil
168
+ end
169
+
170
+ # current_page + 1 or nil if there is no next page
171
+ def next_page
172
+ current_page < total_pages ? (current_page + 1) : nil
173
+ end
174
+
122
175
  def load_next_token_set
123
176
  options = @params[1]
124
177
  options[:next_token] = @next_token
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_record
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 3
9
- - 4
10
- version: 1.3.4
9
+ - 5
10
+ version: 1.3.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Travis Reeder
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2010-05-27 00:00:00 -07:00
20
+ date: 2010-06-02 00:00:00 -07:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency