Don't pluralize the CollectionSerializer#root for #json_key

One of three constituents is used to provide the
CollectionSerializer's #json_key:

1) the :root option - controlled by the caller
2) the #name of the first resource serializer - the root or
   underscored model name
3) the underscored #name of the resources object - generally
   equivalent to the underscored model name of #2

Of the three, only the latter 2 are out of the callers control, and
only the latter two are expected to be singular by default. Not
pluralizing the root gives the caller additional flexibility in
defining the desired root, whether conventionally plural,
unconventionally plural (e.g. objects_received:) or singular.
This commit is contained in:
Ben Woosley
2016-01-07 11:32:10 -08:00
parent 34e5faa1c4
commit 251e33a0a1
4 changed files with 22 additions and 10 deletions

View File

@@ -23,8 +23,7 @@ module ActiveModel
end
def json_key
key = root || serializers.first.try(:json_key) || object.try(:name).try(:underscore)
key.try(:pluralize)
root || derived_root
end
def paginated?
@@ -36,6 +35,13 @@ module ActiveModel
protected
attr_reader :serializers
private
def derived_root
key = serializers.first.try(:json_key) || object.try(:name).try(:underscore)
key.try(:pluralize)
end
end
end
end