Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'active_support/inflector'

module JsonApi
module Parameters
module Handlers
module DefaultHandlers
class EmptyToManyRelationHandler < BaseHandler
include ActiveSupport::Inflector

attr_reader :with_inclusion, :vals, :key

def handle
[
["#{pluralize(relationship_key)}_attributes".to_sym, []],
["#{singularize(relationship_key)}_ids".to_sym, []]
]
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'active_support/inflector'

module JsonApi
module Parameters
module Handlers
module DefaultHandlers
class EmptyToOneRelationHandler < BaseHandler
include ActiveSupport::Inflector

attr_reader :with_inclusion, :vals, :key

def handle
[
["#{singularize(relationship_key)}_attributes".to_sym, {}],
["#{singularize(relationship_key)}_id".to_sym, nil]
]
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def handle
end

# Handle with empty hash.
ToOneRelationHandler.new(relationship_key, {}, {}).handle
EmptyToOneRelationHandler.new(relationship_key, {}, {}).handle
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/jsonapi_parameters/handlers.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
require_relative 'default_handlers/nil_relation_handler'
require_relative 'default_handlers/to_many_relation_handler'
require_relative 'default_handlers/to_one_relation_handler'
require_relative 'default_handlers/empty_to_one_relation_handler'
require_relative 'default_handlers/empty_to_many_relation_handler'

module JsonApi
module Parameters
module Handlers
include DefaultHandlers

DEFAULT_HANDLER_SET = {
empty_to_many: EmptyToManyRelationHandler,
to_many: ToManyRelationHandler,
to_one: ToOneRelationHandler,
nil: NilRelationHandler
Expand Down
17 changes: 15 additions & 2 deletions lib/jsonapi_parameters/translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def handle_relationships(param, relationship_key, relationship_value)
Handlers.handlers[Handlers.resource_handlers[relationship_key]]
else
case relationship_value
when []
Handlers.handlers[:empty_to_many]
when Array
Handlers.handlers[:to_many]
when Hash
Expand All @@ -83,15 +85,26 @@ def handle_relationships(param, relationship_key, relationship_value)
end
end

key, val = handler.call(*handler_args)
handled_params = handler.call(*handler_args)

param[key] = handle_nested_relationships(val)
add_relationships_to_params(handled_params, param)

param
ensure
decrement_stack_level
end

def add_relationships_to_params(handled_params, param)
if handled_params.all?(Array)
handled_params.each do |key, val|
param[key] = handle_nested_relationships(val)
end
else
key, val = *handled_params
param[key] = handle_nested_relationships(val)
end
end

def handle_nested_relationships(val)
# We can only consider Hash relationships (which imply to-one relationship) and Array relationships (which imply to-many).
# Each type has a different handling method, though in both cases we end up passing the nested relationship recursively to handle_relationship
Expand Down
10 changes: 6 additions & 4 deletions spec/support/inputs_outputs_pairs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,8 @@ module JsonApi::Parameters::Testing
photo: {
title: 'Ember Hamster',
src: 'http://example.com/images/productivity.png',
photographer_ids: []
photographer_ids: [],
photographers_attributes: []
}
}
] },
Expand All @@ -505,7 +506,8 @@ module JsonApi::Parameters::Testing
account: {
name: 'Bob Loblaw',
profile_url: 'http://example.com/images/no-nonsense.png',
owner_id: nil
owner_id: nil,
owner_attributes: []
}
}
] },
Expand All @@ -526,7 +528,7 @@ module JsonApi::Parameters::Testing
},
{
user: {
name: 'Adam Joe', practice_area_ids: [],
name: 'Adam Joe', practice_area_ids: [], practice_areas_attributes: []
}
}
] },
Expand All @@ -546,7 +548,7 @@ module JsonApi::Parameters::Testing
},
{
user: {
name: 'Adam Joe', practice_area_ids: [],
name: 'Adam Joe', practice_area_ids: [], practice_areas_attributes: []
}
}
] },
Expand Down