repres-dosser 1.4.1 → 1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1a42891d206e59f30ee7288b9081d1a89b8c305c
4
- data.tar.gz: 877892ae132a390ed97c2cfd443099457e3513f9
3
+ metadata.gz: 232b89400fa5b292d47c11b04180d4189793a9f8
4
+ data.tar.gz: cd9291a3e4fb5d31850ef40896c11ea30676e6aa
5
5
  SHA512:
6
- metadata.gz: 74546e080b6e2f34d26134a22aa39b376fbe78e92efb8758ade1b5d8ecfe3dd8fa2f62df656656d6c7b8ee3f3f1fd7aa669144f317035c852e78facab3510def
7
- data.tar.gz: 0bef3cd90b48d02788e520eb776b1b57028559f19a00925ed3bea77fd8ab79a1f4cc91136ebe080de0f778d809becdcf203c16e640f28fdeb0bd6e16d105b471
6
+ metadata.gz: 3f04925a73b936ff5099bb3fe8c2fe046e64158dd408fc43461fa672ca1fa5958da6531672c260dede3c6793e312d28b6e104217fe7b356e60541868cc95288c
7
+ data.tar.gz: 5e162d288aa1ac41e221d26cea101ee1d4844173f2b86f1b29a04a39509e32a52e083198f7c797f81404df2c31eb7cff57d8da9211424aaf5d98a995bd0e59af
data/README.md CHANGED
@@ -39,3 +39,26 @@ The following responding methods are supported:
39
39
  - 404 render_not_found
40
40
  - 409 render_conflict
41
41
  - 500 render_internal_server_error
42
+
43
+ ### Pagination
44
+ ```ruby
45
+ include Repres::Dosser::Concerns::ResourcePresentation
46
+
47
+ paginate total_items: 108, per_page: 10, current_page: 2
48
+ # The pagination will be added into the meta of the response body.
49
+ ```
50
+
51
+ ```javascript
52
+ onIndexResourceSuccess: function(data, textStatus, jqXHR)
53
+ {
54
+ var pagination = data.meta.pagination;
55
+ console.info('Here are '+pagination.total_items+' items totally.');
56
+ console.info('Ideally '+pagination.per_page+' items on each page.');
57
+ console.info('The current page is '+pagination.current_page+'.');
58
+ // If the required current_page is greater than the total_pages, the current_page is changed to be equal the total_pages.
59
+ // If the required current_page is less than 1, the current_page is changed to 1. The current_page starts from 1.
60
+ console.info('Here are '+pagination.total_pages+' pages totally.');
61
+ console.info('Here are '+pagination.items_on_current_pages+' items on the current page.');
62
+ console.info('The item index is from '+pagination.min_item_on_current_page+' to '+pagination.max_item_on_current_page+' on the current page.');
63
+ }
64
+ ```
@@ -10,7 +10,7 @@ module Repres::Dosser::Concerns::ResourcePresentation
10
10
  CODE_FAILURE_WRONG_PARAMETER = 'failure-wrong-parameter'.freeze
11
11
  CODE_FAILURE_WRONG_STATE = 'failure-wrong-state'.freeze
12
12
 
13
- self.included do |includer|
13
+ included do |includer|
14
14
 
15
15
  attr_writer :criteria
16
16
 
@@ -201,8 +201,28 @@ module Repres::Dosser::Concerns::ResourcePresentation
201
201
  errors: errors
202
202
  end
203
203
 
204
+ def paginate(total_items: 0, per_page: 0, current_page: 1)
205
+ total_items = total_items.to_i
206
+ per_page = per_page.to_i
207
+ current_page = current_page.to_i
208
+
209
+ raise ArgumentError.new('The total_items argument should be a positive integer.') if total_items<=0
210
+ raise ArgumentError.new('The per_page argument should be a positive integer.') if per_page<=0
211
+
212
+ current_page = 1 if current_page<=0
213
+ total_pages = total_items/per_page+(total_items%per_page>0 ? 1 : 0)
214
+ current_page = [ current_page, total_pages ].min
215
+ min_item_on_current_page = per_page*(current_page-1)+1
216
+ max_item_on_current_page = [ per_page*current_page, total_items ].min
217
+ items_on_current_page = max_item_on_current_page-min_item_on_current_page+1
218
+
219
+ @pagination = { total_items: total_items, per_page: per_page, current_page: current_page, total_pages: total_pages, min_item_on_current_page: min_item_on_current_page, max_item_on_current_page: max_item_on_current_page, items_on_current_page: items_on_current_page }
220
+
221
+ end
222
+
204
223
  def respond_result(status, result)
205
224
  result[:meta] = { criteria: @criteria, request_id: request.uuid, timestamp: Time.now.to_i }
225
+ result[:meta][:pagination] = @pagination if @pagination.present?
206
226
  respond_to do |format|
207
227
  format.json do render status: status, json: result end
208
228
  format.xml do render status: status, xml: result end
@@ -1,5 +1,5 @@
1
1
  module Repres
2
2
  module Dosser
3
- VERSION = '1.4.1'.freeze
3
+ VERSION = '1.5'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: repres-dosser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: '1.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Topbit Du
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-29 00:00:00.000000000 Z
11
+ date: 2016-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails