@@ -346,19 +346,19 @@ def add_show_related_resources_operation(relationship_type)
346346 )
347347 end
348348
349- def parse_add_operation ( data )
350- Array . wrap ( data ) . each do |params |
351- verify_type ( params [ :type ] )
349+ def parse_add_operation ( params )
350+ fail JSONAPI ::Exceptions ::InvalidDataFormat unless params . respond_to? ( :each_pair )
352351
353- data = parse_params ( params , @resource_klass . creatable_fields ( @context ) )
354- @operations . push JSONAPI ::Operation . new ( :create_resource ,
355- @resource_klass ,
356- context : @context ,
357- data : data ,
358- fields : @fields ,
359- include_directives : @include_directives
360- )
361- end
352+ verify_type ( params [ :type ] )
353+
354+ data = parse_params ( params , @resource_klass . creatable_fields ( @context ) )
355+ @operations . push JSONAPI ::Operation . new ( :create_resource ,
356+ @resource_klass ,
357+ context : @context ,
358+ data : data ,
359+ fields : @fields ,
360+ include_directives : @include_directives
361+ )
362362 rescue JSONAPI ::Exceptions ::Error => e
363363 @errors . concat ( e . errors )
364364 end
@@ -558,7 +558,7 @@ def verify_permitted_params(params, allowed_fields)
558558
559559 def parse_add_relationship_operation ( verified_params , relationship , parent_key )
560560 if relationship . is_a? ( JSONAPI ::Relationship ::ToMany )
561- @operations . push JSONAPI ::Operation . new ( :create_to_many_relationship ,
561+ @operations . push JSONAPI ::Operation . new ( :create_to_many_relationships ,
562562 resource_klass ,
563563 context : @context ,
564564 resource_id : parent_key ,
@@ -590,13 +590,15 @@ def parse_update_relationship_operation(verified_params, relationship, parent_ke
590590 fail JSONAPI ::Exceptions ::ToManySetReplacementForbidden . new
591591 end
592592 options [ :data ] = verified_params [ :to_many ] . values [ 0 ]
593- operation_type = :replace_to_many_relationship
593+ operation_type = :replace_to_many_relationships
594594 end
595595
596596 @operations . push JSONAPI ::Operation . new ( operation_type , resource_klass , options )
597597 end
598598
599599 def parse_single_replace_operation ( data , keys , id_key_presence_check_required : true )
600+ fail JSONAPI ::Exceptions ::InvalidDataFormat unless data . respond_to? ( :each_pair )
601+
600602 fail JSONAPI ::Exceptions ::MissingKey . new if data [ :id ] . nil?
601603
602604 key = data [ :id ] . to_s
@@ -619,31 +621,16 @@ def parse_single_replace_operation(data, keys, id_key_presence_check_required: t
619621 end
620622
621623 def parse_replace_operation ( data , keys )
622- if data . is_a? ( Array )
623- fail JSONAPI ::Exceptions ::CountMismatch if keys . count != data . count
624-
625- data . each do |object_params |
626- parse_single_replace_operation ( object_params , keys )
627- end
628- else
629- parse_single_replace_operation ( data , [ keys ] ,
630- id_key_presence_check_required : keys . present? )
631- end
632-
624+ parse_single_replace_operation ( data , [ keys ] , id_key_presence_check_required : keys . present? )
633625 rescue JSONAPI ::Exceptions ::Error => e
634626 @errors . concat ( e . errors )
635627 end
636628
637629 def parse_remove_operation ( params )
638- keys = parse_key_array ( params . require ( :id ) )
639-
640- keys . each do |key |
641- @operations . push JSONAPI ::Operation . new ( :remove_resource ,
642- @resource_klass ,
643- context : @context ,
644- resource_id : key
645- )
646- end
630+ @operations . push JSONAPI ::Operation . new ( :remove_resource ,
631+ @resource_klass ,
632+ context : @context ,
633+ resource_id : @resource_klass . verify_key ( params . require ( :id ) , context ) )
647634 rescue JSONAPI ::Exceptions ::Error => e
648635 @errors . concat ( e . errors )
649636 end
@@ -656,25 +643,15 @@ def parse_remove_relationship_operation(params, relationship, parent_key)
656643 )
657644
658645 if relationship . is_a? ( JSONAPI ::Relationship ::ToMany )
646+ operation_args = operation_base_args . dup
659647 keys = params [ :to_many ] . values [ 0 ]
660- keys . each do |key |
661- operation_args = operation_base_args . dup
662- operation_args [ 1 ] = operation_args [ 1 ] . merge ( associated_key : key )
663- @operations . push JSONAPI ::Operation . new ( :remove_to_many_relationship ,
664- *operation_args
665- )
666- end
648+ operation_args [ 1 ] = operation_args [ 1 ] . merge ( associated_keys : keys )
649+ @operations . push JSONAPI ::Operation . new ( :remove_to_many_relationships , *operation_args )
667650 else
668- @operations . push JSONAPI ::Operation . new ( :remove_to_one_relationship ,
669- *operation_base_args
670- )
651+ @operations . push JSONAPI ::Operation . new ( :remove_to_one_relationship , *operation_base_args )
671652 end
672653 end
673654
674- def parse_key_array ( raw )
675- @resource_klass . verify_keys ( raw . split ( /,/ ) , context )
676- end
677-
678655 def format_key ( key )
679656 @key_formatter . format ( key )
680657 end
0 commit comments