json_api_client 1.5.1 → 1.5.2

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: 441f2bb5cf8f6fd819fda9b1b06282a7295b314c
4
- data.tar.gz: 4dafa2d5bdf7a8b8aecad456638c704f609719bb
3
+ metadata.gz: 3b172a6b2cca062c34df683b1fcbea5a017420a5
4
+ data.tar.gz: 129f0173840bafb6982f191f07fa2f815cb5dce1
5
5
  SHA512:
6
- metadata.gz: d7ef003297a74702c6425a6869c30b204cc66996cd579ba46f462607eb1295a37134155160638d301a035d31fc9151d58d465890e5bf406026dff41853106363
7
- data.tar.gz: 1d67ac769d524ed852e9a675b9b5e831c6f6224c0c8f4ab0666f105f010eadf735c6497d3fb5f5ec2e09700869d072c94df9b181ce67101b3bff775550d7b021
6
+ metadata.gz: c80f324ed56f8de4575ce6feada1576c253f034bf23c6ef1cb0bce3233a49efc4719cfa740bea078134057cc740fde6f9a7cdf883cd5bfc2b2536a44984da0f9
7
+ data.tar.gz: 9b8a4f4842c6045772d77757781fb76c33e10704a82754749fb344ec9a4b77f155683d64c3646b6cd2a62457f2a19f2d118eb5028f4b2656188c35da8b2baee3
data/README.md CHANGED
@@ -213,6 +213,10 @@ article.title
213
213
  # should not have returned the created_at
214
214
  article.created_at
215
215
  # => raise NoMethodError
216
+
217
+ # or you can use fieldsets from multiple resources
218
+ # makes request to /articles?fields[articles]=title,body&fields[comments]=tag
219
+ article = Article.select("title", "body",{comments: 'tag'}).first
216
220
  ```
217
221
 
218
222
  ## Sorting
@@ -485,7 +489,7 @@ class MyMoneyCaster
485
489
  end
486
490
  end
487
491
  end
488
-
492
+
489
493
  JsonApiClient::Schema.register money: MyMoneyCaster
490
494
 
491
495
  ```
@@ -16,13 +16,13 @@ module JsonApiClient
16
16
  :"#{attr_name}_id"
17
17
  end
18
18
 
19
- def to_prefix_path
20
- "#{attr_name.to_s.pluralize}/%{#{param}}"
19
+ def to_prefix_path(formatter)
20
+ "#{formatter.format(attr_name.to_s.pluralize)}/%{#{param}}"
21
21
  end
22
22
 
23
- def set_prefix_path(attrs)
23
+ def set_prefix_path(attrs, formatter)
24
24
  attrs[param] = encode_part(attrs[param]) if attrs.key?(param)
25
- to_prefix_path % attrs
25
+ to_prefix_path(formatter) % attrs
26
26
  end
27
27
  end
28
28
  end
@@ -35,11 +35,7 @@ module JsonApiClient
35
35
  end
36
36
 
37
37
  def select(*fields)
38
- fields = Array(fields).flatten
39
- fields = fields.map { |i| i.to_s.split(",") }.flatten
40
-
41
- @fields += fields.map(&:strip)
42
-
38
+ @fields += parse_fields(*fields)
43
39
  self
44
40
  end
45
41
 
@@ -143,7 +139,23 @@ module JsonApiClient
143
139
  end
144
140
 
145
141
  def select_params
146
- @fields.empty? ? {} : {fields: {klass.table_name => @fields.join(",")}}
142
+ if @fields.empty?
143
+ {}
144
+ else
145
+ field_result = Hash.new { |h,k| h[k] = [] }
146
+ @fields.each do |field|
147
+ if field.is_a? Hash
148
+ field.each do |k,v|
149
+ field_result[k.to_s] << v
150
+ field_result[k.to_s] = field_result[k.to_s].flatten
151
+ end
152
+ else
153
+ field_result[klass.table_name] << field
154
+ end
155
+ end
156
+ field_result.each { |k,v| field_result[k] = v.join(',') }
157
+ {fields: field_result}
158
+ end
147
159
  end
148
160
 
149
161
  def parse_related_links(*tables)
@@ -179,6 +191,21 @@ module JsonApiClient
179
191
  end.flatten
180
192
  end
181
193
 
194
+ def parse_fields(*fields)
195
+ fields = fields.split(',') if fields.is_a? String
196
+ fields.map do |field|
197
+ case field
198
+ when Hash
199
+ field.each do |k,v|
200
+ field[k] = parse_fields(v)
201
+ end
202
+ field
203
+ else
204
+ Array(field).flatten.map { |i| i.to_s.split(",") }.flatten.map(&:strip)
205
+ end
206
+ end.flatten
207
+ end
208
+
182
209
  end
183
210
  end
184
211
  end
@@ -263,11 +263,19 @@ module JsonApiClient
263
263
  end
264
264
 
265
265
  def _prefix_path
266
- _belongs_to_associations.map(&:to_prefix_path).join("/")
266
+ paths = _belongs_to_associations.map do |a|
267
+ a.to_prefix_path(route_formatter)
268
+ end
269
+
270
+ paths.join("/")
267
271
  end
268
272
 
269
273
  def _set_prefix_path(attrs)
270
- _belongs_to_associations.map { |a| a.set_prefix_path(attrs) }.join("/")
274
+ paths = _belongs_to_associations.map do |a|
275
+ a.set_prefix_path(attrs, route_formatter)
276
+ end
277
+
278
+ paths.join("/")
271
279
  end
272
280
 
273
281
  def _new_scope
@@ -297,15 +305,17 @@ module JsonApiClient
297
305
  @persisted = nil
298
306
  self.links = self.class.linker.new(params.delete("links") || {})
299
307
  self.relationships = self.class.relationship_linker.new(self.class, params.delete("relationships") || {})
300
- self.class.associations.each do |association|
301
- if params.has_key?(association.attr_name.to_s)
302
- set_attribute(association.attr_name, association.parse(params[association.attr_name.to_s]))
303
- end
304
- end
305
308
  self.attributes = params.merge(self.class.default_attributes)
309
+
306
310
  self.class.schema.each_property do |property|
307
311
  attributes[property.name] = property.default unless attributes.has_key?(property.name) || property.default.nil?
308
312
  end
313
+
314
+ self.class.associations.each do |association|
315
+ if params.has_key?(association.attr_name.to_s)
316
+ set_attribute(association.attr_name, params[association.attr_name.to_s])
317
+ end
318
+ end
309
319
  end
310
320
 
311
321
  # Set the current attributes and try to save them
@@ -1,3 +1,3 @@
1
1
  module JsonApiClient
2
- VERSION = "1.5.1"
2
+ VERSION = "1.5.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 1.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Ching
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-02 00:00:00.000000000 Z
11
+ date: 2017-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -177,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
177
  version: '0'
178
178
  requirements: []
179
179
  rubyforge_project:
180
- rubygems_version: 2.6.8
180
+ rubygems_version: 2.5.2
181
181
  signing_key:
182
182
  specification_version: 4
183
183
  summary: Build client libraries compliant with specification defined by jsonapi.org