jsonapi-utils 0.3.3 → 0.3.4

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: dd9b7abb145201ff33ce27c5fa8a70c37914eb94
4
- data.tar.gz: 188eb2baf1d0f0182b60257cdebab1696cc9d8ee
3
+ metadata.gz: e0600b0456c3a82eead45d6b278f6edd3a0eaf69
4
+ data.tar.gz: f7ae630485129ae7f4ed320b2b66a43b4988be2b
5
5
  SHA512:
6
- metadata.gz: 8d332f2619cb712f97f6f7b104a615fa67d7baed80736580401d7966ef6a9901c60c88404f544cc77ec844af764bcf09efde3cf574304e510c27c8aef4075460
7
- data.tar.gz: 111fcd196807c42afe7bddea2a8291499efca754aef0c0c8c876656a46ee485d736373af39b0de192df40d5a25ff455cab6de35130ea118bb21f2f0a8fee3eec
6
+ metadata.gz: 17a24aeb6bd942161301874c84c290d6214382a89f6d459b3c27a2c0e8036c6a24671e780cebe0e575ef699a0e1d86144839e7b626423f4c2803f267772b6a2c
7
+ data.tar.gz: 90a95d43903965f646c5b2be1679b01bab64d6868785d245bffd008e65e6ee8e5c62c30f8c6ebe090234e0890c98a728494e0012caae26807a4a028904eb5f2b
data/lib/jsonapi/utils.rb CHANGED
@@ -93,7 +93,6 @@ module JSONAPI
93
93
  end
94
94
 
95
95
  def pagination_params(options)
96
- @paginator ||= paginator(params)
97
96
  if @paginator && JSONAPI.configuration.top_level_links_include_pagination
98
97
  options = {}
99
98
  @paginator.class.requires_record_count &&
@@ -105,9 +104,7 @@ module JSONAPI
105
104
  end
106
105
 
107
106
  def build_collection(records, options = {})
108
- unless JSONAPI.configuration.default_paginator == :none
109
- records = paginator(@request.params).apply(records, nil)
110
- end
107
+ records = paginate(records, options)
111
108
  records.respond_to?(:to_ary) ? records.map { |record| turn_into_resource(record, options) } : []
112
109
  end
113
110
 
@@ -119,17 +116,40 @@ module JSONAPI
119
116
  end
120
117
  end
121
118
 
122
- def paginator(params)
123
- page_params = ActionController::Parameters.new(params[:page])
119
+ def paginate(records, options = {})
120
+ return records if pagination_disabled?(options)
121
+ pagination = set_pagination(options)
124
122
 
125
- @paginator ||=
126
- if JSONAPI.configuration.default_paginator == :paged
127
- PagedPaginator.new(page_params)
128
- elsif JSONAPI.configuration.default_paginator == :offset
129
- OffsetPaginator.new(page_params)
123
+ records =
124
+ if records.is_a?(Array)
125
+ records[pagination[:range]]
126
+ else
127
+ pagination[:paginator].apply(records, nil)
130
128
  end
131
129
  end
132
130
 
131
+ def set_pagination(options)
132
+ page_params = ActionController::Parameters.new(@request.params[:page])
133
+ if JSONAPI.configuration.default_paginator == :paged
134
+ @paginator ||= PagedPaginator.new(page_params)
135
+ n = page_params['number'].to_i.nonzero? || 1
136
+ s = page_params['size'].to_i.nonzero? || JSONAPI.configuration.default_page_size
137
+ { paginator: @paginator, range: (n - 1) * s..n * s - 1 }
138
+ elsif JSONAPI.configuration.default_paginator == :offset
139
+ @paginator ||= OffsetPaginator.new(page_params)
140
+ o = page_params['offset'].to_i.nonzero? || 0
141
+ l = page_params['limit'].to_i.nonzero? || JSONAPI.configuration.default_page_size
142
+ { paginator: @paginator, range: o..o + l - 1 }
143
+ else
144
+ {}
145
+ end
146
+ end
147
+
148
+ def pagination_disabled?(options)
149
+ JSONAPI.configuration.default_paginator == :none ||
150
+ !options[:paginate].nil? && !options[:paginate]
151
+ end
152
+
133
153
  def fix_when_hash(records, options)
134
154
  return [] unless options[:model]
135
155
  records.map { |hash| options[:model].new(hash) }
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Utils
3
- VERSION = '0.3.3'
3
+ VERSION = '0.3.4'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tiago Guedes
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2015-11-10 00:00:00.000000000 Z
12
+ date: 2016-03-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler