@@ -37,7 +37,7 @@ def self.install!
3737 # @return [NilClass]
3838 def self . add_errors_renderer!
3939 ActionController ::Renderers . add ( :jsonapi_errors ) do |resource , options |
40- self . content_type || = Mime [ :jsonapi ]
40+ self . content_type = Mime [ :jsonapi ] if self . media_type . nil?
4141
4242 many = JSONAPI ::Rails . is_collection? ( resource , options [ :is_collection ] )
4343 resource = [ resource ] unless many
@@ -47,7 +47,7 @@ def self.add_errors_renderer!
4747 ) unless resource . is_a? ( ActiveModel ::Errors )
4848
4949 errors = [ ]
50- model = resource . instance_variable_get ( ' @base' )
50+ model = resource . instance_variable_get ( : @base)
5151
5252 if respond_to? ( :jsonapi_serializer_class , true )
5353 model_serializer = jsonapi_serializer_class ( model , false )
@@ -56,8 +56,8 @@ def self.add_errors_renderer!
5656 end
5757
5858 details = { }
59- if ::Rails :: VERSION :: MAJOR >= 6 && :: Rails :: VERSION :: MINOR >= 1
60- resource . map do |error |
59+ if ::Rails . gem_version >= Gem :: Version . new ( '6.1' )
60+ resource . each do |error |
6161 attr = error . attribute
6262 details [ attr ] ||= [ ]
6363 details [ attr ] << error . detail . merge ( message : error . message )
@@ -90,7 +90,7 @@ def self.add_errors_renderer!
9090 # @return [NilClass]
9191 def self . add_renderer!
9292 ActionController ::Renderers . add ( :jsonapi ) do |resource , options |
93- self . content_type || = Mime [ :jsonapi ]
93+ self . content_type = Mime [ :jsonapi ] if self . media_type . nil?
9494
9595 JSONAPI_METHODS_MAPPING . to_a [ 0 ..1 ] . each do |opt , method_name |
9696 next unless respond_to? ( method_name , true )
@@ -100,7 +100,7 @@ def self.add_renderer!
100100 # If it's an empty collection, return it directly.
101101 many = JSONAPI ::Rails . is_collection? ( resource , options [ :is_collection ] )
102102 if many && !resource . any?
103- return options . slice ( :meta , :links ) . merge ( data : [ ] ) . to_json
103+ return options . slice ( :meta , :links ) . compact . merge ( data : [ ] ) . to_json
104104 end
105105
106106 JSONAPI_METHODS_MAPPING . to_a [ 2 ..-1 ] . each do |opt , method_name |
@@ -121,15 +121,13 @@ def self.add_renderer!
121121
122122 # Checks if an object is a collection
123123 #
124- # Stolen from [JSONAPI::Serializer], instance method.
124+ # Basically forwards it to a [JSONAPI::Serializer] as there's no public API
125125 #
126126 # @param resource [Object] to check
127127 # @param force_is_collection [NilClass] flag to overwrite
128128 # @return [TrueClass] upon success
129129 def self . is_collection? ( resource , force_is_collection = nil )
130- return force_is_collection unless force_is_collection . nil?
131-
132- resource . respond_to? ( :size ) && !resource . respond_to? ( :each_pair )
130+ JSONAPI ::ErrorSerializer . is_collection? ( resource , force_is_collection )
133131 end
134132
135133 # Resolves resource serializer class
0 commit comments