mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
re: RuboCop - Use nested module/class definition instead of compact style.
This commit is contained in:
parent
8a2beacb6f
commit
85f417f8d2
@ -46,7 +46,7 @@ Style/AlignParameters:
|
|||||||
EnforcedStyle: with_fixed_indentation
|
EnforcedStyle: with_fixed_indentation
|
||||||
|
|
||||||
Style/ClassAndModuleChildren:
|
Style/ClassAndModuleChildren:
|
||||||
EnforcedStyle: compact
|
EnforcedStyle: nested
|
||||||
|
|
||||||
Style/Documentation:
|
Style/Documentation:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|||||||
@ -12,13 +12,6 @@ Lint/HandleExceptions:
|
|||||||
- 'Rakefile'
|
- 'Rakefile'
|
||||||
|
|
||||||
|
|
||||||
# Offense count: 271
|
|
||||||
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
|
||||||
# SupportedStyles: nested, compact
|
|
||||||
Style/ClassAndModuleChildren:
|
|
||||||
Enabled: false
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Offense count: 3
|
# Offense count: 3
|
||||||
# Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist.
|
# Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist.
|
||||||
|
|||||||
@ -1,9 +1,12 @@
|
|||||||
require 'active_model/serializer/collection_serializer'
|
require 'active_model/serializer/collection_serializer'
|
||||||
class ActiveModel::Serializer
|
|
||||||
class ArraySerializer < CollectionSerializer
|
module ActiveModel
|
||||||
class << self
|
class Serializer
|
||||||
extend ActiveModelSerializers::Deprecate
|
class ArraySerializer < CollectionSerializer
|
||||||
deprecate :new, 'ActiveModel::Serializer::CollectionSerializer.'
|
class << self
|
||||||
|
extend ActiveModelSerializers::Deprecate
|
||||||
|
deprecate :new, 'ActiveModel::Serializer::CollectionSerializer.'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,10 +1,14 @@
|
|||||||
class ActiveModel::Serializer::ErrorSerializer < ActiveModel::Serializer
|
module ActiveModel
|
||||||
# @return [Hash<field_name,Array<error_message>>]
|
class Serializer
|
||||||
def as_json
|
class ErrorSerializer < ActiveModel::Serializer
|
||||||
object.errors.messages
|
# @return [Hash<field_name,Array<error_message>>]
|
||||||
end
|
def as_json
|
||||||
|
object.errors.messages
|
||||||
|
end
|
||||||
|
|
||||||
def success?
|
def success?
|
||||||
false
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,27 +1,32 @@
|
|||||||
require 'active_model/serializer/error_serializer'
|
require 'active_model/serializer/error_serializer'
|
||||||
class ActiveModel::Serializer::ErrorsSerializer
|
|
||||||
include Enumerable
|
|
||||||
delegate :each, to: :@serializers
|
|
||||||
attr_reader :object, :root
|
|
||||||
|
|
||||||
def initialize(resources, options = {})
|
module ActiveModel
|
||||||
@root = options[:root]
|
class Serializer
|
||||||
@object = resources
|
class ErrorsSerializer
|
||||||
@serializers = resources.map do |resource|
|
include Enumerable
|
||||||
serializer_class = options.fetch(:serializer) { ActiveModel::Serializer::ErrorSerializer }
|
delegate :each, to: :@serializers
|
||||||
serializer_class.new(resource, options.except(:serializer))
|
attr_reader :object, :root
|
||||||
|
|
||||||
|
def initialize(resources, options = {})
|
||||||
|
@root = options[:root]
|
||||||
|
@object = resources
|
||||||
|
@serializers = resources.map do |resource|
|
||||||
|
serializer_class = options.fetch(:serializer) { ActiveModel::Serializer::ErrorSerializer }
|
||||||
|
serializer_class.new(resource, options.except(:serializer))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def success?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
def json_key
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
attr_reader :serializers
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def success?
|
|
||||||
false
|
|
||||||
end
|
|
||||||
|
|
||||||
def json_key
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
|
|
||||||
protected
|
|
||||||
|
|
||||||
attr_reader :serializers
|
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,146 +1,150 @@
|
|||||||
module ActiveModel::Serializer::Lint
|
module ActiveModel
|
||||||
# == Active \Model \Serializer \Lint \Tests
|
class Serializer
|
||||||
#
|
module Lint
|
||||||
# You can test whether an object is compliant with the Active \Model \Serializers
|
# == Active \Model \Serializer \Lint \Tests
|
||||||
# API by including <tt>ActiveModel::Serializer::Lint::Tests</tt> in your TestCase.
|
#
|
||||||
# It will include tests that tell you whether your object is fully compliant,
|
# You can test whether an object is compliant with the Active \Model \Serializers
|
||||||
# or if not, which aspects of the API are not implemented.
|
# API by including <tt>ActiveModel::Serializer::Lint::Tests</tt> in your TestCase.
|
||||||
#
|
# It will include tests that tell you whether your object is fully compliant,
|
||||||
# Note an object is not required to implement all APIs in order to work
|
# or if not, which aspects of the API are not implemented.
|
||||||
# with Active \Model \Serializers. This module only intends to provide guidance in case
|
#
|
||||||
# you want all features out of the box.
|
# Note an object is not required to implement all APIs in order to work
|
||||||
#
|
# with Active \Model \Serializers. This module only intends to provide guidance in case
|
||||||
# These tests do not attempt to determine the semantic correctness of the
|
# you want all features out of the box.
|
||||||
# returned values. For instance, you could implement <tt>serializable_hash</tt> to
|
#
|
||||||
# always return +{}+, and the tests would pass. It is up to you to ensure
|
# These tests do not attempt to determine the semantic correctness of the
|
||||||
# that the values are semantically meaningful.
|
# returned values. For instance, you could implement <tt>serializable_hash</tt> to
|
||||||
module Tests
|
# always return +{}+, and the tests would pass. It is up to you to ensure
|
||||||
# Passes if the object responds to <tt>serializable_hash</tt> and if it takes
|
# that the values are semantically meaningful.
|
||||||
# zero or one arguments.
|
module Tests
|
||||||
# Fails otherwise.
|
# Passes if the object responds to <tt>serializable_hash</tt> and if it takes
|
||||||
#
|
# zero or one arguments.
|
||||||
# <tt>serializable_hash</tt> returns a hash representation of a object's attributes.
|
# Fails otherwise.
|
||||||
# Typically, it is implemented by including ActiveModel::Serialization.
|
#
|
||||||
def test_serializable_hash
|
# <tt>serializable_hash</tt> returns a hash representation of a object's attributes.
|
||||||
assert_respond_to resource, :serializable_hash, 'The resource should respond to serializable_hash'
|
# Typically, it is implemented by including ActiveModel::Serialization.
|
||||||
resource.serializable_hash
|
def test_serializable_hash
|
||||||
resource.serializable_hash(nil)
|
assert_respond_to resource, :serializable_hash, 'The resource should respond to serializable_hash'
|
||||||
end
|
resource.serializable_hash
|
||||||
|
resource.serializable_hash(nil)
|
||||||
|
end
|
||||||
|
|
||||||
# Passes if the object responds to <tt>read_attribute_for_serialization</tt>
|
# Passes if the object responds to <tt>read_attribute_for_serialization</tt>
|
||||||
# and if it requires one argument (the attribute to be read).
|
# and if it requires one argument (the attribute to be read).
|
||||||
# Fails otherwise.
|
# Fails otherwise.
|
||||||
#
|
#
|
||||||
# <tt>read_attribute_for_serialization</tt> gets the attribute value for serialization
|
# <tt>read_attribute_for_serialization</tt> gets the attribute value for serialization
|
||||||
# Typically, it is implemented by including ActiveModel::Serialization.
|
# Typically, it is implemented by including ActiveModel::Serialization.
|
||||||
def test_read_attribute_for_serialization
|
def test_read_attribute_for_serialization
|
||||||
assert_respond_to resource, :read_attribute_for_serialization, 'The resource should respond to read_attribute_for_serialization'
|
assert_respond_to resource, :read_attribute_for_serialization, 'The resource should respond to read_attribute_for_serialization'
|
||||||
actual_arity = resource.method(:read_attribute_for_serialization).arity
|
actual_arity = resource.method(:read_attribute_for_serialization).arity
|
||||||
# using absolute value since arity is:
|
# using absolute value since arity is:
|
||||||
# 1 for def read_attribute_for_serialization(name); end
|
# 1 for def read_attribute_for_serialization(name); end
|
||||||
# -1 for alias :read_attribute_for_serialization :send
|
# -1 for alias :read_attribute_for_serialization :send
|
||||||
assert_equal 1, actual_arity.abs, "expected #{actual_arity.inspect}.abs to be 1 or -1"
|
assert_equal 1, actual_arity.abs, "expected #{actual_arity.inspect}.abs to be 1 or -1"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Passes if the object responds to <tt>as_json</tt> and if it takes
|
# Passes if the object responds to <tt>as_json</tt> and if it takes
|
||||||
# zero or one arguments.
|
# zero or one arguments.
|
||||||
# Fails otherwise.
|
# Fails otherwise.
|
||||||
#
|
#
|
||||||
# <tt>as_json</tt> returns a hash representation of a serialized object.
|
# <tt>as_json</tt> returns a hash representation of a serialized object.
|
||||||
# It may delegate to <tt>serializable_hash</tt>
|
# It may delegate to <tt>serializable_hash</tt>
|
||||||
# Typically, it is implemented either by including ActiveModel::Serialization
|
# Typically, it is implemented either by including ActiveModel::Serialization
|
||||||
# which includes ActiveModel::Serializers::JSON.
|
# which includes ActiveModel::Serializers::JSON.
|
||||||
# or by the JSON gem when required.
|
# or by the JSON gem when required.
|
||||||
def test_as_json
|
def test_as_json
|
||||||
assert_respond_to resource, :as_json
|
assert_respond_to resource, :as_json
|
||||||
resource.as_json
|
resource.as_json
|
||||||
resource.as_json(nil)
|
resource.as_json(nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Passes if the object responds to <tt>to_json</tt> and if it takes
|
# Passes if the object responds to <tt>to_json</tt> and if it takes
|
||||||
# zero or one arguments.
|
# zero or one arguments.
|
||||||
# Fails otherwise.
|
# Fails otherwise.
|
||||||
#
|
#
|
||||||
# <tt>to_json</tt> returns a string representation (JSON) of a serialized object.
|
# <tt>to_json</tt> returns a string representation (JSON) of a serialized object.
|
||||||
# It may be called on the result of <tt>as_json</tt>.
|
# It may be called on the result of <tt>as_json</tt>.
|
||||||
# Typically, it is implemented on all objects when the JSON gem is required.
|
# Typically, it is implemented on all objects when the JSON gem is required.
|
||||||
def test_to_json
|
def test_to_json
|
||||||
assert_respond_to resource, :to_json
|
assert_respond_to resource, :to_json
|
||||||
resource.to_json
|
resource.to_json
|
||||||
resource.to_json(nil)
|
resource.to_json(nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Passes if the object responds to <tt>cache_key</tt>
|
# Passes if the object responds to <tt>cache_key</tt>
|
||||||
# Fails otherwise.
|
# Fails otherwise.
|
||||||
#
|
#
|
||||||
# <tt>cache_key</tt> returns a (self-expiring) unique key for the object,
|
# <tt>cache_key</tt> returns a (self-expiring) unique key for the object,
|
||||||
# and is part of the (self-expiring) cache_key, which is used by the
|
# and is part of the (self-expiring) cache_key, which is used by the
|
||||||
# adapter. It is not required unless caching is enabled.
|
# adapter. It is not required unless caching is enabled.
|
||||||
def test_cache_key
|
def test_cache_key
|
||||||
assert_respond_to resource, :cache_key
|
assert_respond_to resource, :cache_key
|
||||||
actual_arity = resource.method(:cache_key).arity
|
actual_arity = resource.method(:cache_key).arity
|
||||||
assert_includes [-1, 0], actual_arity, "expected #{actual_arity.inspect} to be 0 or -1"
|
assert_includes [-1, 0], actual_arity, "expected #{actual_arity.inspect} to be 0 or -1"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Passes if the object responds to <tt>updated_at</tt> and if it takes no
|
# Passes if the object responds to <tt>updated_at</tt> and if it takes no
|
||||||
# arguments.
|
# arguments.
|
||||||
# Fails otherwise.
|
# Fails otherwise.
|
||||||
#
|
#
|
||||||
# <tt>updated_at</tt> returns a Time object or iso8601 string and
|
# <tt>updated_at</tt> returns a Time object or iso8601 string and
|
||||||
# is part of the (self-expiring) cache_key, which is used by the adapter.
|
# is part of the (self-expiring) cache_key, which is used by the adapter.
|
||||||
# It is not required unless caching is enabled.
|
# It is not required unless caching is enabled.
|
||||||
def test_updated_at
|
def test_updated_at
|
||||||
assert_respond_to resource, :updated_at
|
assert_respond_to resource, :updated_at
|
||||||
actual_arity = resource.method(:updated_at).arity
|
actual_arity = resource.method(:updated_at).arity
|
||||||
assert_equal 0, actual_arity
|
assert_equal 0, actual_arity
|
||||||
end
|
end
|
||||||
|
|
||||||
# Passes if the object responds to <tt>id</tt> and if it takes no
|
# Passes if the object responds to <tt>id</tt> and if it takes no
|
||||||
# arguments.
|
# arguments.
|
||||||
# Fails otherwise.
|
# Fails otherwise.
|
||||||
#
|
#
|
||||||
# <tt>id</tt> returns a unique identifier for the object.
|
# <tt>id</tt> returns a unique identifier for the object.
|
||||||
# It is not required unless caching is enabled.
|
# It is not required unless caching is enabled.
|
||||||
def test_id
|
def test_id
|
||||||
assert_respond_to resource, :id
|
assert_respond_to resource, :id
|
||||||
assert_equal 0, resource.method(:id).arity
|
assert_equal 0, resource.method(:id).arity
|
||||||
end
|
end
|
||||||
|
|
||||||
# Passes if the object's class responds to <tt>model_name</tt> and if it
|
# Passes if the object's class responds to <tt>model_name</tt> and if it
|
||||||
# is in an instance of +ActiveModel::Name+.
|
# is in an instance of +ActiveModel::Name+.
|
||||||
# Fails otherwise.
|
# Fails otherwise.
|
||||||
#
|
#
|
||||||
# <tt>model_name</tt> returns an ActiveModel::Name instance.
|
# <tt>model_name</tt> returns an ActiveModel::Name instance.
|
||||||
# It is used by the serializer to identify the object's type.
|
# It is used by the serializer to identify the object's type.
|
||||||
# It is not required unless caching is enabled.
|
# It is not required unless caching is enabled.
|
||||||
def test_model_name
|
def test_model_name
|
||||||
resource_class = resource.class
|
resource_class = resource.class
|
||||||
assert_respond_to resource_class, :model_name
|
assert_respond_to resource_class, :model_name
|
||||||
assert_instance_of resource_class.model_name, ActiveModel::Name
|
assert_instance_of resource_class.model_name, ActiveModel::Name
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_active_model_errors
|
def test_active_model_errors
|
||||||
assert_respond_to resource, :errors
|
assert_respond_to resource, :errors
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_active_model_errors_human_attribute_name
|
def test_active_model_errors_human_attribute_name
|
||||||
assert_respond_to resource.class, :human_attribute_name
|
assert_respond_to resource.class, :human_attribute_name
|
||||||
assert_equal(-2, resource.class.method(:human_attribute_name).arity)
|
assert_equal(-2, resource.class.method(:human_attribute_name).arity)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_active_model_errors_lookup_ancestors
|
def test_active_model_errors_lookup_ancestors
|
||||||
assert_respond_to resource.class, :lookup_ancestors
|
assert_respond_to resource.class, :lookup_ancestors
|
||||||
assert_equal 0, resource.class.method(:lookup_ancestors).arity
|
assert_equal 0, resource.class.method(:lookup_ancestors).arity
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def resource
|
def resource
|
||||||
@resource or fail "'@resource' must be set as the linted object"
|
@resource or fail "'@resource' must be set as the linted object"
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_instance_of(result, name)
|
def assert_instance_of(result, name)
|
||||||
assert result.instance_of?(name), "#{result} should be an instance of #{name}"
|
assert result.instance_of?(name), "#{result} should be an instance of #{name}"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -22,49 +22,51 @@
|
|||||||
# render jsonapi: model
|
# render jsonapi: model
|
||||||
#
|
#
|
||||||
# No wrapper format needed as it does not apply (i.e. no `wrap_parameters format: [jsonapi]`)
|
# No wrapper format needed as it does not apply (i.e. no `wrap_parameters format: [jsonapi]`)
|
||||||
module ActiveModelSerializers::Jsonapi
|
module ActiveModelSerializers
|
||||||
MEDIA_TYPE = 'application/vnd.api+json'.freeze
|
module Jsonapi
|
||||||
HEADERS = {
|
MEDIA_TYPE = 'application/vnd.api+json'.freeze
|
||||||
response: { 'CONTENT_TYPE'.freeze => MEDIA_TYPE },
|
HEADERS = {
|
||||||
request: { 'ACCEPT'.freeze => MEDIA_TYPE }
|
response: { 'CONTENT_TYPE'.freeze => MEDIA_TYPE },
|
||||||
}.freeze
|
request: { 'ACCEPT'.freeze => MEDIA_TYPE }
|
||||||
|
}.freeze
|
||||||
|
|
||||||
def self.install
|
def self.install
|
||||||
# actionpack/lib/action_dispatch/http/mime_types.rb
|
# actionpack/lib/action_dispatch/http/mime_types.rb
|
||||||
Mime::Type.register MEDIA_TYPE, :jsonapi
|
Mime::Type.register MEDIA_TYPE, :jsonapi
|
||||||
|
|
||||||
if Rails::VERSION::MAJOR >= 5
|
if Rails::VERSION::MAJOR >= 5
|
||||||
ActionDispatch::Request.parameter_parsers[:jsonapi] = parser
|
ActionDispatch::Request.parameter_parsers[:jsonapi] = parser
|
||||||
else
|
else
|
||||||
ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = parser
|
ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = parser
|
||||||
end
|
end
|
||||||
|
|
||||||
# ref https://github.com/rails/rails/pull/21496
|
# ref https://github.com/rails/rails/pull/21496
|
||||||
ActionController::Renderers.add :jsonapi do |json, options|
|
ActionController::Renderers.add :jsonapi do |json, options|
|
||||||
json = serialize_jsonapi(json, options).to_json(options) unless json.is_a?(String)
|
json = serialize_jsonapi(json, options).to_json(options) unless json.is_a?(String)
|
||||||
self.content_type ||= Mime[:jsonapi]
|
self.content_type ||= Mime[:jsonapi]
|
||||||
self.response_body = json
|
self.response_body = json
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Proposal: should actually deserialize the JSON API params
|
# Proposal: should actually deserialize the JSON API params
|
||||||
# to the hash format expected by `ActiveModel::Serializers::JSON`
|
# to the hash format expected by `ActiveModel::Serializers::JSON`
|
||||||
# actionpack/lib/action_dispatch/http/parameters.rb
|
# actionpack/lib/action_dispatch/http/parameters.rb
|
||||||
def self.parser
|
def self.parser
|
||||||
lambda do |body|
|
lambda do |body|
|
||||||
data = JSON.parse(body)
|
data = JSON.parse(body)
|
||||||
data = { _json: data } unless data.is_a?(Hash)
|
data = { _json: data } unless data.is_a?(Hash)
|
||||||
data.with_indifferent_access
|
data.with_indifferent_access
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module ControllerSupport
|
module ControllerSupport
|
||||||
def serialize_jsonapi(json, options)
|
def serialize_jsonapi(json, options)
|
||||||
options[:adapter] = :json_api
|
options[:adapter] = :json_api
|
||||||
options.fetch(:serialization_context) do
|
options.fetch(:serialization_context) do
|
||||||
options[:serialization_context] = ActiveModelSerializers::SerializationContext.new(request)
|
options[:serialization_context] = ActiveModelSerializers::SerializationContext.new(request)
|
||||||
|
end
|
||||||
|
get_serializer(json, options)
|
||||||
end
|
end
|
||||||
get_serializer(json, options)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -4,11 +4,13 @@ require 'active_model_serializers'
|
|||||||
require 'grape/formatters/active_model_serializers'
|
require 'grape/formatters/active_model_serializers'
|
||||||
require 'grape/helpers/active_model_serializers'
|
require 'grape/helpers/active_model_serializers'
|
||||||
|
|
||||||
module Grape::ActiveModelSerializers
|
module Grape
|
||||||
extend ActiveSupport::Concern
|
module ActiveModelSerializers
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
included do
|
included do
|
||||||
formatter :json, Grape::Formatters::ActiveModelSerializers
|
formatter :json, Grape::Formatters::ActiveModelSerializers
|
||||||
helpers Grape::Helpers::ActiveModelSerializers
|
helpers Grape::Helpers::ActiveModelSerializers
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,20 +1,22 @@
|
|||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class ActiveModelSerializers::JsonPointerTest < ActiveSupport::TestCase
|
module ActiveModelSerializers
|
||||||
def test_attribute_pointer
|
class JsonPointerTest < ActiveSupport::TestCase
|
||||||
attribute_name = 'title'
|
def test_attribute_pointer
|
||||||
pointer = ActiveModelSerializers::JsonPointer.new(:attribute, attribute_name)
|
attribute_name = 'title'
|
||||||
assert_equal '/data/attributes/title', pointer
|
pointer = ActiveModelSerializers::JsonPointer.new(:attribute, attribute_name)
|
||||||
end
|
assert_equal '/data/attributes/title', pointer
|
||||||
|
end
|
||||||
|
|
||||||
def test_primary_data_pointer
|
def test_primary_data_pointer
|
||||||
pointer = ActiveModelSerializers::JsonPointer.new(:primary_data)
|
pointer = ActiveModelSerializers::JsonPointer.new(:primary_data)
|
||||||
assert_equal '/data', pointer
|
assert_equal '/data', pointer
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_unkown_data_pointer
|
def test_unkown_data_pointer
|
||||||
assert_raises(TypeError) do
|
assert_raises(TypeError) do
|
||||||
ActiveModelSerializers::JsonPointer.new(:unknown)
|
ActiveModelSerializers::JsonPointer.new(:unknown)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,263 +1,265 @@
|
|||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class ActiveModelSerializers::KeyTransformTest < ActiveSupport::TestCase
|
module ActiveModelSerializers
|
||||||
def test_camel
|
class KeyTransformTest < ActiveSupport::TestCase
|
||||||
obj = Object.new
|
def test_camel
|
||||||
scenarios = [
|
obj = Object.new
|
||||||
{
|
scenarios = [
|
||||||
value: { "some-key": 'value' },
|
{
|
||||||
expected: { SomeKey: 'value' }
|
value: { "some-key": 'value' },
|
||||||
},
|
expected: { SomeKey: 'value' }
|
||||||
{
|
},
|
||||||
value: { someKey: 'value' },
|
{
|
||||||
expected: { SomeKey: 'value' }
|
value: { someKey: 'value' },
|
||||||
},
|
expected: { SomeKey: 'value' }
|
||||||
{
|
},
|
||||||
value: { some_key: 'value' },
|
{
|
||||||
expected: { SomeKey: 'value' }
|
value: { some_key: 'value' },
|
||||||
},
|
expected: { SomeKey: 'value' }
|
||||||
{
|
},
|
||||||
value: { 'some-key' => 'value' },
|
{
|
||||||
expected: { 'SomeKey' => 'value' }
|
value: { 'some-key' => 'value' },
|
||||||
},
|
expected: { 'SomeKey' => 'value' }
|
||||||
{
|
},
|
||||||
value: { 'someKey' => 'value' },
|
{
|
||||||
expected: { 'SomeKey' => 'value' }
|
value: { 'someKey' => 'value' },
|
||||||
},
|
expected: { 'SomeKey' => 'value' }
|
||||||
{
|
},
|
||||||
value: { 'some_key' => 'value' },
|
{
|
||||||
expected: { 'SomeKey' => 'value' }
|
value: { 'some_key' => 'value' },
|
||||||
},
|
expected: { 'SomeKey' => 'value' }
|
||||||
{
|
},
|
||||||
value: :"some-value",
|
{
|
||||||
expected: :SomeValue
|
value: :"some-value",
|
||||||
},
|
expected: :SomeValue
|
||||||
{
|
},
|
||||||
value: :some_value,
|
{
|
||||||
expected: :SomeValue
|
value: :some_value,
|
||||||
},
|
expected: :SomeValue
|
||||||
{
|
},
|
||||||
value: :someValue,
|
{
|
||||||
expected: :SomeValue
|
value: :someValue,
|
||||||
},
|
expected: :SomeValue
|
||||||
{
|
},
|
||||||
value: 'some-value',
|
{
|
||||||
expected: 'SomeValue'
|
value: 'some-value',
|
||||||
},
|
expected: 'SomeValue'
|
||||||
{
|
},
|
||||||
value: 'someValue',
|
{
|
||||||
expected: 'SomeValue'
|
value: 'someValue',
|
||||||
},
|
expected: 'SomeValue'
|
||||||
{
|
},
|
||||||
value: 'some_value',
|
{
|
||||||
expected: 'SomeValue'
|
value: 'some_value',
|
||||||
},
|
expected: 'SomeValue'
|
||||||
{
|
},
|
||||||
value: obj,
|
{
|
||||||
expected: obj
|
value: obj,
|
||||||
},
|
expected: obj
|
||||||
{
|
},
|
||||||
value: nil,
|
{
|
||||||
expected: nil
|
value: nil,
|
||||||
}
|
expected: nil
|
||||||
]
|
}
|
||||||
scenarios.each do |s|
|
]
|
||||||
result = ActiveModelSerializers::KeyTransform.camel(s[:value])
|
scenarios.each do |s|
|
||||||
assert_equal s[:expected], result
|
result = ActiveModelSerializers::KeyTransform.camel(s[:value])
|
||||||
|
assert_equal s[:expected], result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def test_camel_lower
|
def test_camel_lower
|
||||||
obj = Object.new
|
obj = Object.new
|
||||||
scenarios = [
|
scenarios = [
|
||||||
{
|
{
|
||||||
value: { "some-key": 'value' },
|
value: { "some-key": 'value' },
|
||||||
expected: { someKey: 'value' }
|
expected: { someKey: 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: { SomeKey: 'value' },
|
value: { SomeKey: 'value' },
|
||||||
expected: { someKey: 'value' }
|
expected: { someKey: 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: { some_key: 'value' },
|
value: { some_key: 'value' },
|
||||||
expected: { someKey: 'value' }
|
expected: { someKey: 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: { 'some-key' => 'value' },
|
value: { 'some-key' => 'value' },
|
||||||
expected: { 'someKey' => 'value' }
|
expected: { 'someKey' => 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: { 'SomeKey' => 'value' },
|
value: { 'SomeKey' => 'value' },
|
||||||
expected: { 'someKey' => 'value' }
|
expected: { 'someKey' => 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: { 'some_key' => 'value' },
|
value: { 'some_key' => 'value' },
|
||||||
expected: { 'someKey' => 'value' }
|
expected: { 'someKey' => 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: :"some-value",
|
value: :"some-value",
|
||||||
expected: :someValue
|
expected: :someValue
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: :SomeValue,
|
value: :SomeValue,
|
||||||
expected: :someValue
|
expected: :someValue
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: :some_value,
|
value: :some_value,
|
||||||
expected: :someValue
|
expected: :someValue
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: 'some-value',
|
value: 'some-value',
|
||||||
expected: 'someValue'
|
expected: 'someValue'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: 'SomeValue',
|
value: 'SomeValue',
|
||||||
expected: 'someValue'
|
expected: 'someValue'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: 'some_value',
|
value: 'some_value',
|
||||||
expected: 'someValue'
|
expected: 'someValue'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: obj,
|
value: obj,
|
||||||
expected: obj
|
expected: obj
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: nil,
|
value: nil,
|
||||||
expected: nil
|
expected: nil
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
scenarios.each do |s|
|
scenarios.each do |s|
|
||||||
result = ActiveModelSerializers::KeyTransform.camel_lower(s[:value])
|
result = ActiveModelSerializers::KeyTransform.camel_lower(s[:value])
|
||||||
assert_equal s[:expected], result
|
assert_equal s[:expected], result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def test_dash
|
def test_dash
|
||||||
obj = Object.new
|
obj = Object.new
|
||||||
scenarios = [
|
scenarios = [
|
||||||
{
|
{
|
||||||
value: { some_key: 'value' },
|
value: { some_key: 'value' },
|
||||||
expected: { "some-key": 'value' }
|
expected: { "some-key": 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: { 'some_key' => 'value' },
|
value: { 'some_key' => 'value' },
|
||||||
expected: { 'some-key' => 'value' }
|
expected: { 'some-key' => 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: { SomeKey: 'value' },
|
value: { SomeKey: 'value' },
|
||||||
expected: { "some-key": 'value' }
|
expected: { "some-key": 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: { 'SomeKey' => 'value' },
|
value: { 'SomeKey' => 'value' },
|
||||||
expected: { 'some-key' => 'value' }
|
expected: { 'some-key' => 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: { someKey: 'value' },
|
value: { someKey: 'value' },
|
||||||
expected: { "some-key": 'value' }
|
expected: { "some-key": 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: { 'someKey' => 'value' },
|
value: { 'someKey' => 'value' },
|
||||||
expected: { 'some-key' => 'value' }
|
expected: { 'some-key' => 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: :some_value,
|
value: :some_value,
|
||||||
expected: :"some-value"
|
expected: :"some-value"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: :SomeValue,
|
value: :SomeValue,
|
||||||
expected: :"some-value"
|
expected: :"some-value"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: 'SomeValue',
|
value: 'SomeValue',
|
||||||
expected: 'some-value'
|
expected: 'some-value'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: :someValue,
|
value: :someValue,
|
||||||
expected: :"some-value"
|
expected: :"some-value"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: 'someValue',
|
value: 'someValue',
|
||||||
expected: 'some-value'
|
expected: 'some-value'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: obj,
|
value: obj,
|
||||||
expected: obj
|
expected: obj
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: nil,
|
value: nil,
|
||||||
expected: nil
|
expected: nil
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
scenarios.each do |s|
|
scenarios.each do |s|
|
||||||
result = ActiveModelSerializers::KeyTransform.dash(s[:value])
|
result = ActiveModelSerializers::KeyTransform.dash(s[:value])
|
||||||
assert_equal s[:expected], result
|
assert_equal s[:expected], result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def test_underscore
|
def test_underscore
|
||||||
obj = Object.new
|
obj = Object.new
|
||||||
scenarios = [
|
scenarios = [
|
||||||
{
|
{
|
||||||
value: { "some-key": 'value' },
|
value: { "some-key": 'value' },
|
||||||
expected: { some_key: 'value' }
|
expected: { some_key: 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: { 'some-key' => 'value' },
|
value: { 'some-key' => 'value' },
|
||||||
expected: { 'some_key' => 'value' }
|
expected: { 'some_key' => 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: { SomeKey: 'value' },
|
value: { SomeKey: 'value' },
|
||||||
expected: { some_key: 'value' }
|
expected: { some_key: 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: { 'SomeKey' => 'value' },
|
value: { 'SomeKey' => 'value' },
|
||||||
expected: { 'some_key' => 'value' }
|
expected: { 'some_key' => 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: { someKey: 'value' },
|
value: { someKey: 'value' },
|
||||||
expected: { some_key: 'value' }
|
expected: { some_key: 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: { 'someKey' => 'value' },
|
value: { 'someKey' => 'value' },
|
||||||
expected: { 'some_key' => 'value' }
|
expected: { 'some_key' => 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: :"some-value",
|
value: :"some-value",
|
||||||
expected: :some_value
|
expected: :some_value
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: :SomeValue,
|
value: :SomeValue,
|
||||||
expected: :some_value
|
expected: :some_value
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: :someValue,
|
value: :someValue,
|
||||||
expected: :some_value
|
expected: :some_value
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: 'some-value',
|
value: 'some-value',
|
||||||
expected: 'some_value'
|
expected: 'some_value'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: 'SomeValue',
|
value: 'SomeValue',
|
||||||
expected: 'some_value'
|
expected: 'some_value'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: 'someValue',
|
value: 'someValue',
|
||||||
expected: 'some_value'
|
expected: 'some_value'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: obj,
|
value: obj,
|
||||||
expected: obj
|
expected: obj
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: nil,
|
value: nil,
|
||||||
expected: nil
|
expected: nil
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
scenarios.each do |s|
|
scenarios.each do |s|
|
||||||
result = ActiveModelSerializers::KeyTransform.underscore(s[:value])
|
result = ActiveModelSerializers::KeyTransform.underscore(s[:value])
|
||||||
assert_equal s[:expected], result
|
assert_equal s[:expected], result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class ActiveModelSerializers::ModelTest < ActiveSupport::TestCase
|
module ActiveModelSerializers
|
||||||
include ActiveModel::Serializer::Lint::Tests
|
class ModelTest < ActiveSupport::TestCase
|
||||||
|
include ActiveModel::Serializer::Lint::Tests
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@resource = ActiveModelSerializers::Model.new
|
@resource = ActiveModelSerializers::Model.new
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -5,172 +5,174 @@ require 'kaminari'
|
|||||||
require 'kaminari/hooks'
|
require 'kaminari/hooks'
|
||||||
::Kaminari::Hooks.init
|
::Kaminari::Hooks.init
|
||||||
|
|
||||||
class ActiveModelSerializers::GrapeTest < ActiveSupport::TestCase
|
module ActiveModelSerializers
|
||||||
include Rack::Test::Methods
|
class GrapeTest < ActiveSupport::TestCase
|
||||||
module Models
|
include Rack::Test::Methods
|
||||||
def self.model1
|
module Models
|
||||||
ARModels::Post.new(id: 1, title: 'Dummy Title', body: 'Lorem Ipsum')
|
def self.model1
|
||||||
|
ARModels::Post.new(id: 1, title: 'Dummy Title', body: 'Lorem Ipsum')
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.model2
|
||||||
|
ARModels::Post.new(id: 2, title: 'Second Dummy Title', body: 'Second Lorem Ipsum')
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.all
|
||||||
|
@all ||=
|
||||||
|
begin
|
||||||
|
model1.save!
|
||||||
|
model2.save!
|
||||||
|
ARModels::Post.all
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.reset_all
|
||||||
|
ARModels::Post.delete_all
|
||||||
|
@all = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.collection_per
|
||||||
|
2
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.collection
|
||||||
|
@collection ||=
|
||||||
|
begin
|
||||||
|
Kaminari.paginate_array(
|
||||||
|
[
|
||||||
|
Profile.new(id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
|
||||||
|
Profile.new(id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2'),
|
||||||
|
Profile.new(id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3'),
|
||||||
|
Profile.new(id: 4, name: 'Name 4', description: 'Description 4', comments: 'Comments 4'),
|
||||||
|
Profile.new(id: 5, name: 'Name 5', description: 'Description 5', comments: 'Comments 5')
|
||||||
|
]
|
||||||
|
).page(1).per(collection_per)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.model2
|
class GrapeTest < Grape::API
|
||||||
ARModels::Post.new(id: 2, title: 'Second Dummy Title', body: 'Second Lorem Ipsum')
|
format :json
|
||||||
end
|
include Grape::ActiveModelSerializers
|
||||||
|
|
||||||
def self.all
|
resources :grape do
|
||||||
@all ||=
|
get '/render' do
|
||||||
begin
|
render Models.model1
|
||||||
model1.save!
|
|
||||||
model2.save!
|
|
||||||
ARModels::Post.all
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def self.reset_all
|
get '/render_with_json_api' do
|
||||||
ARModels::Post.delete_all
|
post = Models.model1
|
||||||
@all = nil
|
render post, meta: { page: 1, total_pages: 2 }, adapter: :json_api
|
||||||
end
|
|
||||||
|
|
||||||
def self.collection_per
|
|
||||||
2
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.collection
|
|
||||||
@collection ||=
|
|
||||||
begin
|
|
||||||
Kaminari.paginate_array(
|
|
||||||
[
|
|
||||||
Profile.new(id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
|
|
||||||
Profile.new(id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2'),
|
|
||||||
Profile.new(id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3'),
|
|
||||||
Profile.new(id: 4, name: 'Name 4', description: 'Description 4', comments: 'Comments 4'),
|
|
||||||
Profile.new(id: 5, name: 'Name 5', description: 'Description 5', comments: 'Comments 5')
|
|
||||||
]
|
|
||||||
).page(1).per(collection_per)
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class GrapeTest < Grape::API
|
get '/render_array_with_json_api' do
|
||||||
format :json
|
posts = Models.all
|
||||||
include Grape::ActiveModelSerializers
|
render posts, adapter: :json_api
|
||||||
|
end
|
||||||
|
|
||||||
resources :grape do
|
get '/render_collection_with_json_api' do
|
||||||
get '/render' do
|
posts = Models.collection
|
||||||
render Models.model1
|
render posts, adapter: :json_api
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/render_with_json_api' do
|
get '/render_with_implicit_formatter' do
|
||||||
post = Models.model1
|
Models.model1
|
||||||
render post, meta: { page: 1, total_pages: 2 }, adapter: :json_api
|
end
|
||||||
end
|
|
||||||
|
|
||||||
get '/render_array_with_json_api' do
|
get '/render_array_with_implicit_formatter' do
|
||||||
posts = Models.all
|
Models.all
|
||||||
render posts, adapter: :json_api
|
end
|
||||||
end
|
|
||||||
|
|
||||||
get '/render_collection_with_json_api' do
|
get '/render_collection_with_implicit_formatter' do
|
||||||
posts = Models.collection
|
Models.collection
|
||||||
render posts, adapter: :json_api
|
end
|
||||||
end
|
|
||||||
|
|
||||||
get '/render_with_implicit_formatter' do
|
|
||||||
Models.model1
|
|
||||||
end
|
|
||||||
|
|
||||||
get '/render_array_with_implicit_formatter' do
|
|
||||||
Models.all
|
|
||||||
end
|
|
||||||
|
|
||||||
get '/render_collection_with_implicit_formatter' do
|
|
||||||
Models.collection
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def app
|
def app
|
||||||
Grape::Middleware::Globals.new(GrapeTest.new)
|
Grape::Middleware::Globals.new(GrapeTest.new)
|
||||||
end
|
|
||||||
|
|
||||||
def test_formatter_returns_json
|
|
||||||
get '/grape/render'
|
|
||||||
|
|
||||||
post = Models.model1
|
|
||||||
serializable_resource = serializable(post)
|
|
||||||
|
|
||||||
assert last_response.ok?
|
|
||||||
assert_equal serializable_resource.to_json, last_response.body
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_render_helper_passes_through_options_correctly
|
|
||||||
get '/grape/render_with_json_api'
|
|
||||||
|
|
||||||
post = Models.model1
|
|
||||||
serializable_resource = serializable(post, serializer: ARModels::PostSerializer, adapter: :json_api, meta: { page: 1, total_pages: 2 })
|
|
||||||
|
|
||||||
assert last_response.ok?
|
|
||||||
assert_equal serializable_resource.to_json, last_response.body
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_formatter_handles_arrays
|
|
||||||
get '/grape/render_array_with_json_api'
|
|
||||||
|
|
||||||
posts = Models.all
|
|
||||||
serializable_resource = serializable(posts, adapter: :json_api)
|
|
||||||
|
|
||||||
assert last_response.ok?
|
|
||||||
assert_equal serializable_resource.to_json, last_response.body
|
|
||||||
ensure
|
|
||||||
Models.reset_all
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_formatter_handles_collections
|
|
||||||
get '/grape/render_collection_with_json_api'
|
|
||||||
assert last_response.ok?
|
|
||||||
|
|
||||||
representation = JSON.parse(last_response.body)
|
|
||||||
assert representation.include?('data')
|
|
||||||
assert representation['data'].count == Models.collection_per
|
|
||||||
assert representation.include?('links')
|
|
||||||
assert representation['links'].count > 0
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_implicit_formatter
|
|
||||||
post = Models.model1
|
|
||||||
serializable_resource = serializable(post, adapter: :json_api)
|
|
||||||
|
|
||||||
with_adapter :json_api do
|
|
||||||
get '/grape/render_with_implicit_formatter'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
assert last_response.ok?
|
def test_formatter_returns_json
|
||||||
assert_equal serializable_resource.to_json, last_response.body
|
get '/grape/render'
|
||||||
end
|
|
||||||
|
|
||||||
def test_implicit_formatter_handles_arrays
|
post = Models.model1
|
||||||
posts = Models.all
|
serializable_resource = serializable(post)
|
||||||
serializable_resource = serializable(posts, adapter: :json_api)
|
|
||||||
|
|
||||||
with_adapter :json_api do
|
assert last_response.ok?
|
||||||
get '/grape/render_array_with_implicit_formatter'
|
assert_equal serializable_resource.to_json, last_response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
assert last_response.ok?
|
def test_render_helper_passes_through_options_correctly
|
||||||
assert_equal serializable_resource.to_json, last_response.body
|
get '/grape/render_with_json_api'
|
||||||
ensure
|
|
||||||
Models.reset_all
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_implicit_formatter_handles_collections
|
post = Models.model1
|
||||||
with_adapter :json_api do
|
serializable_resource = serializable(post, serializer: ARModels::PostSerializer, adapter: :json_api, meta: { page: 1, total_pages: 2 })
|
||||||
get '/grape/render_collection_with_implicit_formatter'
|
|
||||||
|
assert last_response.ok?
|
||||||
|
assert_equal serializable_resource.to_json, last_response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
representation = JSON.parse(last_response.body)
|
def test_formatter_handles_arrays
|
||||||
assert last_response.ok?
|
get '/grape/render_array_with_json_api'
|
||||||
assert representation.include?('data')
|
|
||||||
assert representation['data'].count == Models.collection_per
|
posts = Models.all
|
||||||
assert representation.include?('links')
|
serializable_resource = serializable(posts, adapter: :json_api)
|
||||||
assert representation['links'].count > 0
|
|
||||||
|
assert last_response.ok?
|
||||||
|
assert_equal serializable_resource.to_json, last_response.body
|
||||||
|
ensure
|
||||||
|
Models.reset_all
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_formatter_handles_collections
|
||||||
|
get '/grape/render_collection_with_json_api'
|
||||||
|
assert last_response.ok?
|
||||||
|
|
||||||
|
representation = JSON.parse(last_response.body)
|
||||||
|
assert representation.include?('data')
|
||||||
|
assert representation['data'].count == Models.collection_per
|
||||||
|
assert representation.include?('links')
|
||||||
|
assert representation['links'].count > 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_implicit_formatter
|
||||||
|
post = Models.model1
|
||||||
|
serializable_resource = serializable(post, adapter: :json_api)
|
||||||
|
|
||||||
|
with_adapter :json_api do
|
||||||
|
get '/grape/render_with_implicit_formatter'
|
||||||
|
end
|
||||||
|
|
||||||
|
assert last_response.ok?
|
||||||
|
assert_equal serializable_resource.to_json, last_response.body
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_implicit_formatter_handles_arrays
|
||||||
|
posts = Models.all
|
||||||
|
serializable_resource = serializable(posts, adapter: :json_api)
|
||||||
|
|
||||||
|
with_adapter :json_api do
|
||||||
|
get '/grape/render_array_with_implicit_formatter'
|
||||||
|
end
|
||||||
|
|
||||||
|
assert last_response.ok?
|
||||||
|
assert_equal serializable_resource.to_json, last_response.body
|
||||||
|
ensure
|
||||||
|
Models.reset_all
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_implicit_formatter_handles_collections
|
||||||
|
with_adapter :json_api do
|
||||||
|
get '/grape/render_collection_with_implicit_formatter'
|
||||||
|
end
|
||||||
|
|
||||||
|
representation = JSON.parse(last_response.body)
|
||||||
|
assert last_response.ok?
|
||||||
|
assert representation.include?('data')
|
||||||
|
assert representation['data'].count == Models.collection_per
|
||||||
|
assert representation.include?('links')
|
||||||
|
assert representation['links'].count > 0
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,18 +1,20 @@
|
|||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class ActiveModelSerializers::LoggerTest < ActiveSupport::TestCase
|
module ActiveModelSerializers
|
||||||
def test_logger_is_set_to_action_controller_logger_when_initializer_runs
|
class LoggerTest < ActiveSupport::TestCase
|
||||||
assert_equal $action_controller_logger, ActionController::Base.logger # rubocop:disable Style/GlobalVars
|
def test_logger_is_set_to_action_controller_logger_when_initializer_runs
|
||||||
end
|
assert_equal $action_controller_logger, ActionController::Base.logger # rubocop:disable Style/GlobalVars
|
||||||
|
end
|
||||||
|
|
||||||
def test_logger_can_be_set
|
def test_logger_can_be_set
|
||||||
original_logger = ActiveModelSerializers.logger
|
original_logger = ActiveModelSerializers.logger
|
||||||
logger = Logger.new(STDOUT)
|
logger = Logger.new(STDOUT)
|
||||||
|
|
||||||
ActiveModelSerializers.logger = logger
|
ActiveModelSerializers.logger = logger
|
||||||
|
|
||||||
assert_equal ActiveModelSerializers.logger, logger
|
assert_equal ActiveModelSerializers.logger, logger
|
||||||
ensure
|
ensure
|
||||||
ActiveModelSerializers.logger = original_logger
|
ActiveModelSerializers.logger = original_logger
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -75,6 +75,8 @@ module TestHelpers
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class ActiveSupport::TestCase
|
module ActiveSupport
|
||||||
include TestHelpers::Generation
|
class TestCase
|
||||||
|
include TestHelpers::Generation
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -44,10 +44,12 @@ module SerializationTesting
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Minitest::Test
|
module Minitest
|
||||||
def before_setup
|
class Test
|
||||||
ActionController::Base.cache_store.clear
|
def before_setup
|
||||||
end
|
ActionController::Base.cache_store.clear
|
||||||
|
end
|
||||||
|
|
||||||
include SerializationTesting
|
include SerializationTesting
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user