mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
RFC: Json Api Errors (WIP)
- ActiveModelSerializers::JsonPointer - ActiveModel::Serializer::Adapter::JsonApi::Error - ActiveModel::Serializer::Adapter::JsonApi::Error.attributes - Fix rubocop config
This commit is contained in:
@@ -16,6 +16,19 @@ module ActiveModel
|
||||
@resource = resource
|
||||
@adapter_opts, @serializer_opts =
|
||||
options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
|
||||
|
||||
# TECHDEBT: clean up single vs. collection of resources
|
||||
if resource.respond_to?(:each)
|
||||
if resource.any? { |elem| elem.respond_to?(:errors) && !elem.errors.empty? }
|
||||
@serializer_opts[:serializer] = ActiveModel::Serializer::ErrorSerializer
|
||||
@adapter_opts[:adapter] = :'json_api/error'
|
||||
end
|
||||
else
|
||||
if resource.respond_to?(:errors) && !resource.errors.empty?
|
||||
@serializer_opts[:serializer] = ActiveModel::Serializer::ErrorSerializer
|
||||
@adapter_opts[:adapter] = :'json_api/error'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def serialization_scope=(scope)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
require 'thread_safe'
|
||||
require 'active_model/serializer/collection_serializer'
|
||||
require 'active_model/serializer/array_serializer'
|
||||
require 'active_model/serializer/error_serializer'
|
||||
require 'active_model/serializer/include_tree'
|
||||
require 'active_model/serializer/associations'
|
||||
require 'active_model/serializer/attributes'
|
||||
|
||||
2
lib/active_model/serializer/error_serializer.rb
Normal file
2
lib/active_model/serializer/error_serializer.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
class ActiveModel::Serializer::ErrorSerializer < ActiveModel::Serializer
|
||||
end
|
||||
@@ -129,6 +129,20 @@ module ActiveModel::Serializer::Lint
|
||||
assert_instance_of resource_class.model_name, ActiveModel::Name
|
||||
end
|
||||
|
||||
def test_active_model_errors
|
||||
assert_respond_to resource, :errors
|
||||
end
|
||||
|
||||
def test_active_model_errors_human_attribute_name
|
||||
assert_respond_to resource.class, :human_attribute_name
|
||||
assert_equal(-2, resource.class.method(:human_attribute_name).arity)
|
||||
end
|
||||
|
||||
def test_active_model_errors_lookup_ancestors
|
||||
assert_respond_to resource.class, :lookup_ancestors
|
||||
assert_equal 0, resource.class.method(:lookup_ancestors).arity
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def resource
|
||||
|
||||
Reference in New Issue
Block a user