jsonapi-resources 0.10.0.beta3 → 0.10.0.beta4

Sign up to get free protection for your applications and to get access to all the features.
@@ -59,7 +59,7 @@ module JSONAPI
59
59
  end
60
60
  end
61
61
 
62
- fail "To Many primary objects for show" if (primary_objects.count > 1)
62
+ fail "Too many primary objects for show" if (primary_objects.count > 1)
63
63
  primary_hash = { 'data' => primary_objects[0] }
64
64
 
65
65
  primary_hash['included'] = included_objects if included_objects.size > 0
@@ -93,24 +93,19 @@ module JSONAPI
93
93
  return serialize_resource_set_to_hash_plural(resource_set)
94
94
  end
95
95
 
96
- def serialize_to_links_hash(source, requested_relationship, resource_ids)
96
+ def serialize_to_relationship_hash(source, requested_relationship, resource_ids)
97
97
  if requested_relationship.is_a?(JSONAPI::Relationship::ToOne)
98
98
  data = to_one_linkage(resource_ids[0])
99
99
  else
100
100
  data = to_many_linkage(resource_ids)
101
101
  end
102
102
 
103
- {
104
- 'links' => {
105
- 'self' => self_link(source, requested_relationship),
106
- 'related' => related_link(source, requested_relationship)
107
- },
108
- 'data' => data
109
- }
110
- end
103
+ rel_hash = { 'data': data }
111
104
 
112
- def query_link(query_params)
113
- link_builder.query_link(query_params)
105
+ links = default_relationship_links(source, requested_relationship)
106
+ rel_hash['links'] = links unless links.blank?
107
+
108
+ rel_hash
114
109
  end
115
110
 
116
111
  def format_key(key)
@@ -140,7 +135,6 @@ module JSONAPI
140
135
  supplying_attribute_fields: supplying_attribute_fields(resource_klass).sort,
141
136
  supplying_relationship_fields: supplying_relationship_fields(resource_klass).sort,
142
137
  link_builder_base_url: link_builder.base_url,
143
- route_formatter_class: link_builder.route_formatter.uncached.class.name,
144
138
  key_formatter_class: key_formatter.uncached.class.name,
145
139
  always_include_to_one_linkage_data: always_include_to_one_linkage_data,
146
140
  always_include_to_many_linkage_data: always_include_to_many_linkage_data
@@ -165,7 +159,7 @@ module JSONAPI
165
159
  obj_hash['attributes'] = source.attributes_json if source.attributes_json
166
160
 
167
161
  relationships = cached_relationships_hash(source, fetchable_fields, relationship_data)
168
- obj_hash['relationships'] = relationships unless relationships.nil? || relationships.empty?
162
+ obj_hash['relationships'] = relationships unless relationships.blank?
169
163
 
170
164
  obj_hash['meta'] = source.meta_json if source.meta_json
171
165
  else
@@ -184,7 +178,7 @@ module JSONAPI
184
178
  obj_hash['attributes'] = attributes unless attributes.empty?
185
179
 
186
180
  relationships = relationships_hash(source, fetchable_fields, relationship_data)
187
- obj_hash['relationships'] = relationships unless relationships.nil? || relationships.empty?
181
+ obj_hash['relationships'] = relationships unless relationships.blank?
188
182
 
189
183
  meta = meta_hash(source)
190
184
  obj_hash['meta'] = meta unless meta.empty?
@@ -199,7 +193,7 @@ module JSONAPI
199
193
  @_supplying_attribute_fields.fetch resource_klass do
200
194
  attrs = Set.new(resource_klass._attributes.keys.map(&:to_sym))
201
195
  cur = resource_klass
202
- while cur != JSONAPI::Resource
196
+ while !cur.root? # do not traverse beyond the first root resource
203
197
  if @fields.has_key?(cur._type)
204
198
  attrs &= @fields[cur._type]
205
199
  break
@@ -214,7 +208,7 @@ module JSONAPI
214
208
  @_supplying_relationship_fields.fetch resource_klass do
215
209
  relationships = Set.new(resource_klass._relationships.keys.map(&:to_sym))
216
210
  cur = resource_klass
217
- while cur != JSONAPI::Resource
211
+ while !cur.root? # do not traverse beyond the first root resource
218
212
  if @fields.has_key?(cur._type)
219
213
  relationships &= @fields[cur._type]
220
214
  break
@@ -249,7 +243,9 @@ module JSONAPI
249
243
 
250
244
  def links_hash(source)
251
245
  links = custom_links_hash(source)
252
- links['self'] = link_builder.self_link(source) unless links.key?('self')
246
+ if !links.key?('self') && !source.class.exclude_link?(:self)
247
+ links['self'] = link_builder.self_link(source)
248
+ end
253
249
  links.compact
254
250
  end
255
251
 
@@ -274,7 +270,8 @@ module JSONAPI
274
270
  end
275
271
  end
276
272
 
277
- hash[format_key(name)] = link_object(source, relationship, rids, include_data)
273
+ ro = relationship_object(source, relationship, rids, include_data)
274
+ hash[format_key(name)] = ro unless ro.blank?
278
275
  end
279
276
  end
280
277
  end
@@ -323,6 +320,13 @@ module JSONAPI
323
320
  link_builder.relationships_related_link(source, relationship)
324
321
  end
325
322
 
323
+ def default_relationship_links(source, relationship)
324
+ links = {}
325
+ links['self'] = self_link(source, relationship) unless relationship.exclude_link?(:self)
326
+ links['related'] = related_link(source, relationship) unless relationship.exclude_link?(:related)
327
+ links.compact
328
+ end
329
+
326
330
  def to_many_linkage(rids)
327
331
  linkage = []
328
332
 
@@ -346,36 +350,36 @@ module JSONAPI
346
350
  }
347
351
  end
348
352
 
349
- def link_object_to_one(source, relationship, rid, include_data)
353
+ def relationship_object_to_one(source, relationship, rid, include_data)
350
354
  link_object_hash = {}
351
- link_object_hash['links'] = {}
352
- link_object_hash['links']['self'] = self_link(source, relationship)
353
- link_object_hash['links']['related'] = related_link(source, relationship)
355
+
356
+ links = default_relationship_links(source, relationship)
357
+
358
+ link_object_hash['links'] = links unless links.blank?
354
359
  link_object_hash['data'] = to_one_linkage(rid) if include_data
355
360
  link_object_hash
356
361
  end
357
362
 
358
- def link_object_to_many(source, relationship, rids, include_data)
363
+ def relationship_object_to_many(source, relationship, rids, include_data)
359
364
  link_object_hash = {}
360
- link_object_hash['links'] = {}
361
- link_object_hash['links']['self'] = self_link(source, relationship)
362
- link_object_hash['links']['related'] = related_link(source, relationship)
365
+
366
+ links = default_relationship_links(source, relationship)
367
+ link_object_hash['links'] = links unless links.blank?
363
368
  link_object_hash['data'] = to_many_linkage(rids) if include_data
364
369
  link_object_hash
365
370
  end
366
371
 
367
- def link_object(source, relationship, rid, include_data)
372
+ def relationship_object(source, relationship, rid, include_data)
368
373
  if relationship.is_a?(JSONAPI::Relationship::ToOne)
369
- link_object_to_one(source, relationship, rid, include_data)
374
+ relationship_object_to_one(source, relationship, rid, include_data)
370
375
  elsif relationship.is_a?(JSONAPI::Relationship::ToMany)
371
- link_object_to_many(source, relationship, rid, include_data)
376
+ relationship_object_to_many(source, relationship, rid, include_data)
372
377
  end
373
378
  end
374
379
 
375
380
  def generate_link_builder(primary_resource_klass, options)
376
381
  LinkBuilder.new(
377
382
  base_url: options.fetch(:base_url, ''),
378
- route_formatter: options.fetch(:route_formatter, JSONAPI.configuration.route_formatter),
379
383
  primary_resource_klass: primary_resource_klass,
380
384
  )
381
385
  end
@@ -91,7 +91,7 @@ module JSONAPI
91
91
  # Step Four find any of the missing resources and join them into the result
92
92
  missed_resource_ids.each_pair do |resource_klass, ids|
93
93
  find_opts = {context: context, fields: find_options[:fields]}
94
- found_resources = resource_klass.find_by_keys(ids, find_opts)
94
+ found_resources = resource_klass.find_to_populate_by_keys(ids, find_opts)
95
95
 
96
96
  found_resources.each do |resource|
97
97
  relationship_data = @resource_klasses[resource_klass][resource.id][:relationships]
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Resources
3
- VERSION = '0.10.0.beta3'
3
+ VERSION = '0.10.0.beta4'
4
4
  end
5
5
  end
@@ -118,10 +118,15 @@ module JSONAPI
118
118
  result.pagination_params.each_pair do |link_name, params|
119
119
  if result.is_a?(JSONAPI::RelatedResourcesSetOperationResult)
120
120
  relationship = result.source_resource.class._relationships[result._type.to_sym]
121
- @top_level_links[link_name] = serializer.link_builder.relationships_related_link(result.source_resource, relationship, query_params(params))
121
+ unless relationship.exclude_link?(link_name)
122
+ link = serializer.link_builder.relationships_related_link(result.source_resource, relationship, query_params(params))
123
+ end
122
124
  else
123
- @top_level_links[link_name] = serializer.query_link(query_params(params))
125
+ unless serializer.link_builder.primary_resource_klass.exclude_link?(link_name)
126
+ link = serializer.link_builder.query_link(query_params(params))
127
+ end
124
128
  end
129
+ @top_level_links[link_name] = link unless link.blank?
125
130
  end
126
131
  end
127
132
  end
@@ -20,6 +20,10 @@ module ActionDispatch
20
20
  @resource_type = resources.first
21
21
  res = JSONAPI::Resource.resource_klass_for(resource_type_with_module_prefix(@resource_type))
22
22
 
23
+ unless res.singleton?
24
+ warn "Singleton routes created for non singleton resource #{res}. Links may not be generated correctly."
25
+ end
26
+
23
27
  options = resources.extract_options!.dup
24
28
  options[:controller] ||= @resource_type
25
29
  options.merge!(res.routing_resource_options)
@@ -80,6 +84,10 @@ module ActionDispatch
80
84
  @resource_type = resources.first
81
85
  res = JSONAPI::Resource.resource_klass_for(resource_type_with_module_prefix(@resource_type))
82
86
 
87
+ if res.singleton?
88
+ warn "Singleton resource #{res} should use `jsonapi_resource` instead."
89
+ end
90
+
83
91
  options = resources.extract_options!.dup
84
92
  options[:controller] ||= @resource_type
85
93
  options.merge!(res.routing_resource_options)
@@ -154,7 +162,8 @@ module ActionDispatch
154
162
 
155
163
  if methods.include?(:show)
156
164
  match "relationships/#{formatted_relationship_name}", controller: options[:controller],
157
- action: 'show_relationship', relationship: link_type.to_s, via: [:get]
165
+ action: 'show_relationship', relationship: link_type.to_s, via: [:get],
166
+ as: "relationships/#{link_type}"
158
167
  end
159
168
 
160
169
  if res.mutable?
@@ -182,7 +191,8 @@ module ActionDispatch
182
191
 
183
192
  if methods.include?(:show)
184
193
  match "relationships/#{formatted_relationship_name}", controller: options[:controller],
185
- action: 'show_relationship', relationship: link_type.to_s, via: [:get]
194
+ action: 'show_relationship', relationship: link_type.to_s, via: [:get],
195
+ as: "relationships/#{link_type}"
186
196
  end
187
197
 
188
198
  if res.mutable?
@@ -221,7 +231,8 @@ module ActionDispatch
221
231
 
222
232
  match formatted_relationship_name, controller: options[:controller],
223
233
  relationship: relationship.name, source: resource_type_with_module_prefix(source._type),
224
- action: 'show_related_resource', via: [:get]
234
+ action: 'show_related_resource', via: [:get],
235
+ as: relationship_name
225
236
  end
226
237
 
227
238
  def jsonapi_related_resources(*relationship)
@@ -238,7 +249,8 @@ module ActionDispatch
238
249
  match formatted_relationship_name,
239
250
  controller: options[:controller],
240
251
  relationship: relationship.name, source: resource_type_with_module_prefix(source._type),
241
- action: 'index_related_resources', via: [:get]
252
+ action: 'index_related_resources', via: [:get],
253
+ as: relationship_name
242
254
  end
243
255
 
244
256
  protected
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0.beta3
4
+ version: 0.10.0.beta4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Gebhardt
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-02-25 00:00:00.000000000 Z
12
+ date: 2019-05-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -115,6 +115,20 @@ dependencies:
115
115
  - - ">="
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
+ - !ruby/object:Gem::Dependency
119
+ name: database_cleaner
120
+ requirement: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ type: :development
126
+ prerelease: false
127
+ version_requirements: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
118
132
  - !ruby/object:Gem::Dependency
119
133
  name: activerecord
120
134
  requirement: !ruby/object:Gem::Requirement
@@ -176,10 +190,11 @@ files:
176
190
  - lib/generators/jsonapi/templates/jsonapi_controller.rb
177
191
  - lib/generators/jsonapi/templates/jsonapi_resource.rb
178
192
  - lib/jsonapi-resources.rb
179
- - lib/jsonapi/active_relation_resource_finder.rb
180
- - lib/jsonapi/active_relation_resource_finder/adapters/join_left_active_record_adapter.rb
181
- - lib/jsonapi/active_relation_resource_finder/join_manager.rb
193
+ - lib/jsonapi/active_relation/adapters/join_left_active_record_adapter.rb
194
+ - lib/jsonapi/active_relation/join_manager.rb
195
+ - lib/jsonapi/active_relation_resource.rb
182
196
  - lib/jsonapi/acts_as_resource_controller.rb
197
+ - lib/jsonapi/basic_resource.rb
183
198
  - lib/jsonapi/cached_response_fragment.rb
184
199
  - lib/jsonapi/callbacks.rb
185
200
  - lib/jsonapi/compiled_json.rb
@@ -232,7 +247,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
232
247
  - !ruby/object:Gem::Version
233
248
  version: 1.3.1
234
249
  requirements: []
235
- rubygems_version: 3.0.1
250
+ rubyforge_project:
251
+ rubygems_version: 2.7.6
236
252
  signing_key:
237
253
  specification_version: 4
238
254
  summary: Easily support JSON API in Rails.