jsonapi-resources 0.9.0 → 0.9.1.beta1

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: a367b0f58fd1570cdc45f28570f440ab2b68c1c7
4
- data.tar.gz: 48fdfb4691776dea1491b237c7f9fc1eca401072
3
+ metadata.gz: 31f232713ef6f5199ccbc94bccf4a0135fc287ae
4
+ data.tar.gz: bb5a0238e1b0027b4224eda64624b10e8e71cd6e
5
5
  SHA512:
6
- metadata.gz: a1664c0f610a4baaa22c80642575cdb177910476d0d3154bf259902e24b27261ee26b2f385820a360bb50a229a5e74c04ed55fd69f6507ab3596db6cf5324cc9
7
- data.tar.gz: b372447e19a732fadcdb1154894f963460187a2b92e5481df411b798b27347808f4d79f90c51f49b6198a95cd7c1e2d05901ca5a42463064ee89340c80ffa4ad
6
+ metadata.gz: efb1ad213d3b6d67cf5d25c86a84a5cfa393fb7a4c69e6ad3249010bf21aae1b78941f575c4ee27a5fff2edb110edf43ebbcc8b65def43351a15b26f31cac23c
7
+ data.tar.gz: 1eef8ebb63b4bb177d327dc92a139fcc61728ff8955096af8e842341154bb5788ce09b7cedd484ee5b9f83a4a2316225f091034821770cf6261c590a2dc97d17
@@ -364,24 +364,21 @@ module JSONAPI
364
364
  end
365
365
  end
366
366
 
367
- class ParametersNotAllowed < Error
368
- attr_accessor :params
367
+ class ParameterNotAllowed < Error
368
+ attr_accessor :param
369
369
 
370
- def initialize(params, error_object_overrides = {})
371
- @params = params
370
+ def initialize(param, error_object_overrides = {})
371
+ @param = param
372
372
  super(error_object_overrides)
373
373
  end
374
374
 
375
375
  def errors
376
- params.collect do |param|
377
- create_error_object(code: JSONAPI::PARAM_NOT_ALLOWED,
378
- status: :bad_request,
379
- title: I18n.translate('jsonapi-resources.exceptions.parameters_not_allowed.title',
380
- default: 'Param not allowed'),
381
- detail: I18n.translate('jsonapi-resources.exceptions.parameters_not_allowed.detail',
382
- default: "#{param} is not allowed.", param: param))
383
-
384
- end
376
+ [create_error_object(code: JSONAPI::PARAM_NOT_ALLOWED,
377
+ status: :bad_request,
378
+ title: I18n.translate('jsonapi-resources.exceptions.parameter_not_allowed.title',
379
+ default: 'Param not allowed'),
380
+ detail: I18n.translate('jsonapi-resources.exceptions.parameter_not_allowed.detail',
381
+ default: "#{param} is not allowed.", param: param))]
385
382
  end
386
383
  end
387
384
 
@@ -52,7 +52,7 @@ module JSONAPI
52
52
  current_relationship = current_resource_klass._relationships[fragment]
53
53
  current_resource_klass = current_relationship.try(:resource_klass)
54
54
  else
55
- warn "[RELATIONSHIP NOT FOUND] Relationship could not be found for #{current_path}."
55
+ raise JSONAPI::Exceptions::InvalidInclude.new(current_resource_klass, current_path)
56
56
  end
57
57
 
58
58
  include_in_join = @force_eager_load || !current_relationship || current_relationship.eager_load_on_include
@@ -100,7 +100,7 @@ module JSONAPI
100
100
  end
101
101
 
102
102
  if JSONAPI.configuration.top_level_links_include_pagination && paginator
103
- page_options[:pagination_params] = paginator.links_page_params(page_options)
103
+ page_options[:pagination_params] = paginator.links_page_params(page_options.merge(fetched_resources: resource_records))
104
104
  end
105
105
 
106
106
  return JSONAPI::ResourcesOperationResult.new(:ok, resource_records, page_options)
@@ -166,9 +166,10 @@ module JSONAPI
166
166
  include_directives = params[:include_directives]
167
167
 
168
168
  source_resource ||= source_klass.find_by_key(source_id, context: context, fields: fields)
169
+ verified_filters = resource_klass.verify_filters(filters, context)
169
170
 
170
171
  rel_opts = {
171
- filters: filters,
172
+ filters: verified_filters,
172
173
  sort_criteria: sort_criteria,
173
174
  paginator: paginator,
174
175
  fields: fields,
@@ -209,7 +210,7 @@ module JSONAPI
209
210
  pagination_params = if paginator && JSONAPI.configuration.top_level_links_include_pagination
210
211
  page_options = {}
211
212
  page_options[:record_count] = record_count if paginator.class.requires_record_count
212
- paginator.links_page_params(page_options)
213
+ paginator.links_page_params(page_options.merge(fetched_resources: related_resources))
213
214
  else
214
215
  {}
215
216
  end
@@ -66,10 +66,8 @@ module JSONAPI
66
66
  end
67
67
 
68
68
  sort_criteria = options.fetch(:sort_criteria, {})
69
- unless sort_criteria.nil? || sort_criteria.empty?
70
- order_options = relationship.resource_klass.construct_order_options(sort_criteria)
71
- records = resource_klass.apply_sort(records, order_options, @context)
72
- end
69
+ order_options = relationship.resource_klass.construct_order_options(sort_criteria)
70
+ records = resource_klass.apply_sort(records, order_options, @context)
73
71
 
74
72
  paginator = options[:paginator]
75
73
  if paginator
@@ -170,13 +170,11 @@ module JSONAPI
170
170
  end
171
171
  type_resource = Resource.resource_for(@resource_klass.module_path + underscored_type.to_s)
172
172
  rescue NameError
173
- @errors.concat(JSONAPI::Exceptions::InvalidResource.new(type).errors)
174
- rescue JSONAPI::Exceptions::InvalidResource => e
175
- @errors.concat(e.errors)
173
+ fail JSONAPI::Exceptions::InvalidResource.new(type)
176
174
  end
177
175
 
178
176
  if type_resource.nil?
179
- @errors.concat(JSONAPI::Exceptions::InvalidResource.new(type).errors)
177
+ fail JSONAPI::Exceptions::InvalidResource.new(type)
180
178
  else
181
179
  unless values.nil?
182
180
  valid_fields = type_resource.fields.collect { |key| format_key(key) }
@@ -184,11 +182,11 @@ module JSONAPI
184
182
  if valid_fields.include?(field)
185
183
  extracted_fields[type].push unformat_key(field)
186
184
  else
187
- @errors.concat(JSONAPI::Exceptions::InvalidField.new(type, field).errors)
185
+ fail JSONAPI::Exceptions::InvalidField.new(type, field)
188
186
  end
189
187
  end
190
188
  else
191
- @errors.concat(JSONAPI::Exceptions::InvalidField.new(type, 'nil').errors)
189
+ fail JSONAPI::Exceptions::InvalidField.new(type, 'nil')
192
190
  end
193
191
  end
194
192
  end
@@ -205,8 +203,7 @@ module JSONAPI
205
203
  check_include(Resource.resource_for(resource_klass.module_path + relationship.class_name.to_s.underscore), include_parts.last.partition('.'))
206
204
  end
207
205
  else
208
- @errors.concat(JSONAPI::Exceptions::InvalidInclude.new(format_key(resource_klass._type),
209
- include_parts.first).errors)
206
+ fail JSONAPI::Exceptions::InvalidInclude.new(format_key(resource_klass._type), include_parts.first)
210
207
  end
211
208
  end
212
209
 
@@ -214,7 +211,7 @@ module JSONAPI
214
211
  return unless raw_include
215
212
 
216
213
  unless JSONAPI.configuration.allow_include
217
- fail JSONAPI::Exceptions::ParametersNotAllowed.new([:include])
214
+ fail JSONAPI::Exceptions::ParameterNotAllowed.new(:include)
218
215
  end
219
216
 
220
217
  included_resources = []
@@ -226,19 +223,24 @@ module JSONAPI
226
223
 
227
224
  return if included_resources.empty?
228
225
 
229
- result = included_resources.compact.map do |included_resource|
230
- check_include(@resource_klass, included_resource.partition('.'))
231
- unformat_key(included_resource).to_s
232
- end
226
+ begin
227
+ result = included_resources.compact.map do |included_resource|
228
+ check_include(@resource_klass, included_resource.partition('.'))
229
+ unformat_key(included_resource).to_s
230
+ end
233
231
 
234
- @include_directives = JSONAPI::IncludeDirectives.new(@resource_klass, result)
232
+ @include_directives = JSONAPI::IncludeDirectives.new(@resource_klass, result)
233
+ rescue JSONAPI::Exceptions::InvalidInclude => e
234
+ @errors.concat(e.errors)
235
+ @include_directives = {}
236
+ end
235
237
  end
236
238
 
237
239
  def parse_filters(filters)
238
240
  return unless filters
239
241
 
240
242
  unless JSONAPI.configuration.allow_filter
241
- fail JSONAPI::Exceptions::ParametersNotAllowed.new([:filter])
243
+ fail JSONAPI::Exceptions::ParameterNotAllowed.new(:filter)
242
244
  end
243
245
 
244
246
  unless filters.class.method_defined?(:each)
@@ -251,7 +253,7 @@ module JSONAPI
251
253
  if @resource_klass._allowed_filter?(filter)
252
254
  @filters[filter] = value
253
255
  else
254
- @errors.concat(JSONAPI::Exceptions::FilterNotAllowed.new(filter).errors)
256
+ fail JSONAPI::Exceptions::FilterNotAllowed.new(filter)
255
257
  end
256
258
  end
257
259
  end
@@ -267,7 +269,7 @@ module JSONAPI
267
269
  return unless sort_criteria.present?
268
270
 
269
271
  unless JSONAPI.configuration.allow_sort
270
- fail JSONAPI::Exceptions::ParametersNotAllowed.new([:sort])
272
+ fail JSONAPI::Exceptions::ParameterNotAllowed.new(:sort)
271
273
  end
272
274
 
273
275
  sorts = []
@@ -296,9 +298,8 @@ module JSONAPI
296
298
  sort_field = sort_criteria[:field]
297
299
  sortable_fields = resource_klass.sortable_fields(context)
298
300
 
299
- unless sortable_fields.include? sort_field.to_sym
300
- @errors.concat(JSONAPI::Exceptions::InvalidSortCriteria
301
- .new(format_key(resource_klass._type), sort_field).errors)
301
+ unless sortable_fields.include?sort_field.to_sym
302
+ fail JSONAPI::Exceptions::InvalidSortCriteria.new(format_key(resource_klass._type), sort_field)
302
303
  end
303
304
  end
304
305
 
@@ -473,7 +474,7 @@ module JSONAPI
473
474
 
474
475
  unless links_object[:id].nil?
475
476
  resource = self.resource_klass || Resource
476
- relationship_resource = resource.resource_for(unformat_key(links_object[:type]).to_s)
477
+ relationship_resource = resource.resource_for(unformat_key(relationship.options[:class_name] || links_object[:type]).to_s)
477
478
  relationship_id = relationship_resource.verify_key(links_object[:id], @context)
478
479
  if relationship.polymorphic?
479
480
  { id: relationship_id, type: unformat_key(links_object[:type].to_s) }
@@ -528,8 +529,10 @@ module JSONAPI
528
529
  when 'relationships'
529
530
  value.keys.each do |links_key|
530
531
  unless formatted_allowed_fields.include?(links_key.to_sym)
531
- params_not_allowed.push(links_key)
532
- unless JSONAPI.configuration.raise_if_parameters_not_allowed
532
+ if JSONAPI.configuration.raise_if_parameters_not_allowed
533
+ fail JSONAPI::Exceptions::ParameterNotAllowed.new(links_key)
534
+ else
535
+ params_not_allowed.push(links_key)
533
536
  value.delete links_key
534
537
  end
535
538
  end
@@ -537,8 +540,10 @@ module JSONAPI
537
540
  when 'attributes'
538
541
  value.each do |attr_key, attr_value|
539
542
  unless formatted_allowed_fields.include?(attr_key.to_sym)
540
- params_not_allowed.push(attr_key)
541
- unless JSONAPI.configuration.raise_if_parameters_not_allowed
543
+ if JSONAPI.configuration.raise_if_parameters_not_allowed
544
+ fail JSONAPI::Exceptions::ParameterNotAllowed.new(attr_key)
545
+ else
546
+ params_not_allowed.push(attr_key)
542
547
  value.delete attr_key
543
548
  end
544
549
  end
@@ -546,27 +551,30 @@ module JSONAPI
546
551
  when 'type'
547
552
  when 'id'
548
553
  unless formatted_allowed_fields.include?(:id)
549
- params_not_allowed.push(:id)
550
- unless JSONAPI.configuration.raise_if_parameters_not_allowed
554
+ if JSONAPI.configuration.raise_if_parameters_not_allowed
555
+ fail JSONAPI::Exceptions::ParameterNotAllowed.new(:id)
556
+ else
557
+ params_not_allowed.push(:id)
551
558
  params.delete :id
552
559
  end
553
560
  end
554
561
  else
555
- params_not_allowed.push(key)
562
+ if JSONAPI.configuration.raise_if_parameters_not_allowed
563
+ fail JSONAPI::Exceptions::ParameterNotAllowed.new(key)
564
+ else
565
+ params_not_allowed.push(key)
566
+ params.delete key
567
+ end
556
568
  end
557
569
  end
558
570
 
559
571
  if params_not_allowed.length > 0
560
- if JSONAPI.configuration.raise_if_parameters_not_allowed
561
- fail JSONAPI::Exceptions::ParametersNotAllowed.new(params_not_allowed)
562
- else
563
- params_not_allowed_warnings = params_not_allowed.map do |key|
564
- JSONAPI::Warning.new(code: JSONAPI::PARAM_NOT_ALLOWED,
565
- title: 'Param not allowed',
566
- detail: "#{key} is not allowed.")
567
- end
568
- self.warnings.concat(params_not_allowed_warnings)
572
+ params_not_allowed_warnings = params_not_allowed.map do |param|
573
+ JSONAPI::Warning.new(code: JSONAPI::PARAM_NOT_ALLOWED,
574
+ title: 'Param not allowed',
575
+ detail: "#{param} is not allowed.")
569
576
  end
577
+ self.warnings.concat(params_not_allowed_warnings)
570
578
  end
571
579
  end
572
580
 
@@ -453,7 +453,7 @@ module JSONAPI
453
453
 
454
454
  def resource_for(type)
455
455
  type = type.underscore
456
- type_with_module = type.include?('/') ? type : module_path + type
456
+ type_with_module = type.start_with?(module_path) ? type : module_path + type
457
457
 
458
458
  resource_name = _resource_name_from_type(type_with_module)
459
459
  resource = resource_name.safe_constantize if resource_name
@@ -507,10 +507,12 @@ module JSONAPI
507
507
  end
508
508
  end
509
509
 
510
- def attribute(attr, options = {})
510
+ def attribute(attribute_name, options = {})
511
+ attr = attribute_name.to_sym
512
+
511
513
  check_reserved_attribute_name(attr)
512
514
 
513
- if (attr.to_sym == :id) && (options[:format].nil?)
515
+ if (attr == :id) && (options[:format].nil?)
514
516
  ActiveSupport::Deprecation.warn('Id without format is no longer supported. Please remove ids from attributes, or specify a format.')
515
517
  end
516
518
 
@@ -808,6 +810,9 @@ module JSONAPI
808
810
 
809
811
  def verify_filters(filters, context = nil)
810
812
  verified_filters = {}
813
+
814
+ return verified_filters if filters.nil?
815
+
811
816
  filters.each do |filter, raw_value|
812
817
  verified_filter = verify_filter(filter, raw_value, context)
813
818
  verified_filters[verified_filter[0]] = verified_filter[1]
@@ -1038,7 +1043,8 @@ module JSONAPI
1038
1043
  options = attrs.extract_options!
1039
1044
  options[:parent_resource] = self
1040
1045
 
1041
- attrs.each do |relationship_name|
1046
+ attrs.each do |name|
1047
+ relationship_name = name.to_sym
1042
1048
  check_reserved_relationship_name(relationship_name)
1043
1049
  check_duplicate_relationship_name(relationship_name)
1044
1050
 
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Resources
3
- VERSION = '0.9.0'
3
+ VERSION = '0.9.1.beta1'
4
4
  end
5
5
  end
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.9.0
4
+ version: 0.9.1.beta1
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: 2017-02-14 00:00:00.000000000 Z
12
+ date: 2017-05-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -212,12 +212,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
212
212
  version: '2.1'
213
213
  required_rubygems_version: !ruby/object:Gem::Requirement
214
214
  requirements:
215
- - - ">="
215
+ - - ">"
216
216
  - !ruby/object:Gem::Version
217
- version: '0'
217
+ version: 1.3.1
218
218
  requirements: []
219
219
  rubyforge_project:
220
- rubygems_version: 2.5.1
220
+ rubygems_version: 2.5.2
221
221
  signing_key:
222
222
  specification_version: 4
223
223
  summary: Easily support JSON API in Rails.