Module: JSONAPI::Utils::Response::Formatters
- Included in:
- JSONAPI::Utils::Response
- Defined in:
- lib/jsonapi/utils/response/formatters.rb
Instance Method Summary collapse
-
#jsonapi_format(object, options = {}) ⇒ Hash
(also: #jsonapi_serialize)
Helper method to format ActiveRecord or Hash objects into JSON API-compliant ones.
-
#jsonapi_format_errors(object) ⇒ Array
(also: #jsonapi_serialize_errors)
Helper method to format ActiveRecord or any object that responds to #errors into JSON API-compliant error response bodies.
Instance Method Details
#jsonapi_format(object, options = {}) ⇒ Hash Also known as: jsonapi_serialize
The return of this method represents what will actually be displayed in the response body.
It can also be called as #jsonapi_serialize due to backward compatibility issues.
Helper method to format ActiveRecord or Hash objects into JSON API-compliant ones.
33 34 35 36 37 38 39 40 |
# File 'lib/jsonapi/utils/response/formatters.rb', line 33 def jsonapi_format(object, = {}) if object.is_a?(Hash) hash = object.with_indifferent_access object = hash_to_active_record(hash[:data], [:model]) end (object) build_response_document(object, ).contents end |
#jsonapi_format_errors(object) ⇒ Array Also known as: jsonapi_serialize_errors
The return of this method represents what will actually be displayed in the response body.
It can also be called as #jsonapi_serialize_errors due to backward compatibility issues.
Helper method to format ActiveRecord or any object that responds to #errors into JSON API-compliant error response bodies.
57 58 59 60 61 62 63 |
# File 'lib/jsonapi/utils/response/formatters.rb', line 57 def jsonapi_format_errors(object) if active_record_obj?(object) object = JSONAPI::Utils::Exceptions::ActiveRecord.new(object, @request.resource_klass, context) end errors = object.respond_to?(:errors) ? object.errors : object JSONAPI::Utils::Support::Error.sanitize(errors).uniq end |