Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add handling each_serializer when expose a collection of object. #106

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions lib/rocket_pants/controller/respondable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ def self.extract_pagination(collection)
end

def self.normalise_object(object, options = {})
object = object.to_a if options[:each_serializer]

# First, prepare the object for serialization.
object = normalise_to_serializer object, options
# Convert the object using a standard grape-like lookup chain.

if object.is_a?(Array) || object.is_a?(Set)
object.map { |o| normalise_object o, options }
elsif object.respond_to?(:serializable_hash)
Expand All @@ -78,12 +81,32 @@ def self.normalise_object(object, options = {})

def self.normalise_to_serializer(object, options)
return object unless RocketPants.serializers_enabled?
if object.is_a?(Array)
serialize_collection object, options
else
serialize_object object, options
end
end

def self.serialize_object object, options
serializer = options.delete(:serializer)
serializer = object.active_model_serializer if object.respond_to?(:active_model_serializer) && serializer.nil?
return object unless serializer
SerializerWrapper.new serializer, object
end

def self.serialize_collection collection, options
serializer = options.delete(:each_serializer)
collection.map do |object|
serializer = object.active_model_serializer if object.respond_to?(:active_model_serializer)
if serializer
SerializerWrapper.new serializer, object
else
object
end
end
end

RENDERING_OPTIONS = [:status, :content_type]

private
Expand Down