mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Merge pull request #1815 from dubadub/making-rubocop-happy
Fix code-styling issues from .rubocop_todo.yml * re: RuboCop: Bulk minor style corrections * re: RuboCop - hash indention corrections * re: RuboCop - replace rocket style hashes * re: RuboCop - get rid of redundant curly braces around a hash parameter * re: RuboCop - Align the elements of a hash literal if they span more than one line. * re: RuboCop - Use nested module/class definition instead of compact style. * re: RuboCop - Suppress of handling LoadError for optional dependencies * re: RuboCop - use include_ prefix instead of has_ * re: RuboCop - Disable Style/PredicateName rule for public API methods * re: RuboCop - Remove empty .rubocop_todo.yml * re: RuboCop - replace rocket style hashes
This commit is contained in:
commit
f4e00dc65d
@ -1,7 +1,5 @@
|
|||||||
inherit_from: .rubocop_todo.yml
|
|
||||||
|
|
||||||
AllCops:
|
AllCops:
|
||||||
TargetRubyVersion: 2.2
|
TargetRubyVersion: 2.1
|
||||||
Exclude:
|
Exclude:
|
||||||
- config/initializers/forbidden_yaml.rb
|
- config/initializers/forbidden_yaml.rb
|
||||||
- !ruby/regexp /(vendor|bundle|bin|db|tmp)\/.*/
|
- !ruby/regexp /(vendor|bundle|bin|db|tmp)\/.*/
|
||||||
@ -46,7 +44,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
|
||||||
|
|||||||
@ -1,167 +0,0 @@
|
|||||||
# This configuration was generated by
|
|
||||||
# `rubocop --auto-gen-config`
|
|
||||||
# on 2016-03-08 22:29:52 +0100 using RuboCop version 0.37.2.
|
|
||||||
# The point is for the user to remove these configuration records
|
|
||||||
# one by one as the offenses are removed from the code base.
|
|
||||||
# Note that changes in the inspected code, or installation of new
|
|
||||||
# versions of RuboCop, may require this file to be generated again.
|
|
||||||
|
|
||||||
# Offense count: 2
|
|
||||||
Lint/HandleExceptions:
|
|
||||||
Exclude:
|
|
||||||
- 'Rakefile'
|
|
||||||
|
|
||||||
# Offense count: 2
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
# Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods.
|
|
||||||
Lint/UnusedMethodArgument:
|
|
||||||
Exclude:
|
|
||||||
- 'test/lint_test.rb'
|
|
||||||
|
|
||||||
# Offense count: 4
|
|
||||||
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
|
||||||
# SupportedStyles: strict, flexible
|
|
||||||
Rails/TimeZone:
|
|
||||||
Exclude:
|
|
||||||
- 'test/action_controller/serialization_test.rb'
|
|
||||||
- 'test/serializers/cache_test.rb'
|
|
||||||
|
|
||||||
# Offense count: 16
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
# Configuration parameters: EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle, SupportedLastArgumentHashStyles.
|
|
||||||
# SupportedLastArgumentHashStyles: always_inspect, always_ignore, ignore_implicit, ignore_explicit
|
|
||||||
Style/AlignHash:
|
|
||||||
Exclude:
|
|
||||||
- 'test/action_controller/json_api/pagination_test.rb'
|
|
||||||
|
|
||||||
# Offense count: 27
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
|
||||||
# SupportedStyles: braces, no_braces, context_dependent
|
|
||||||
Style/BracesAroundHashParameters:
|
|
||||||
Exclude:
|
|
||||||
- 'test/action_controller/adapter_selector_test.rb'
|
|
||||||
- 'test/action_controller/json_api/pagination_test.rb'
|
|
||||||
- 'test/adapter/json_api/linked_test.rb'
|
|
||||||
- 'test/adapter/json_api/pagination_links_test.rb'
|
|
||||||
- 'test/adapter/null_test.rb'
|
|
||||||
- 'test/adapter_test.rb'
|
|
||||||
- 'test/collection_serializer_test.rb'
|
|
||||||
- 'test/serializable_resource_test.rb'
|
|
||||||
- 'test/serializers/associations_test.rb'
|
|
||||||
- 'test/serializers/attributes_test.rb'
|
|
||||||
- 'test/serializers/root_test.rb'
|
|
||||||
|
|
||||||
# Offense count: 271
|
|
||||||
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
|
||||||
# SupportedStyles: nested, compact
|
|
||||||
Style/ClassAndModuleChildren:
|
|
||||||
Enabled: false
|
|
||||||
|
|
||||||
# Offense count: 6
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
Style/CommentIndentation:
|
|
||||||
Exclude:
|
|
||||||
- 'active_model_serializers.gemspec'
|
|
||||||
|
|
||||||
# Offense count: 1
|
|
||||||
Style/DoubleNegation:
|
|
||||||
Exclude:
|
|
||||||
- 'lib/active_model/serializable_resource.rb'
|
|
||||||
|
|
||||||
# Offense count: 1
|
|
||||||
# Configuration parameters: MinBodyLength.
|
|
||||||
Style/GuardClause:
|
|
||||||
Exclude:
|
|
||||||
- 'lib/active_model/serializer.rb'
|
|
||||||
|
|
||||||
# Offense count: 58
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues.
|
|
||||||
# SupportedStyles: ruby19, ruby19_no_mixed_keys, hash_rockets
|
|
||||||
Style/HashSyntax:
|
|
||||||
Enabled: false
|
|
||||||
|
|
||||||
# Offense count: 4
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
# Configuration parameters: EnforcedStyle, SupportedStyles, IndentationWidth.
|
|
||||||
# SupportedStyles: special_inside_parentheses, consistent, align_brackets
|
|
||||||
Style/IndentArray:
|
|
||||||
Enabled: false
|
|
||||||
|
|
||||||
# Offense count: 10
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
# Configuration parameters: EnforcedStyle, SupportedStyles, IndentationWidth.
|
|
||||||
# SupportedStyles: special_inside_parentheses, consistent, align_braces
|
|
||||||
Style/IndentHash:
|
|
||||||
Enabled: false
|
|
||||||
|
|
||||||
# Offense count: 1
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
Style/Lambda:
|
|
||||||
Exclude:
|
|
||||||
- 'lib/active_model/serializer.rb'
|
|
||||||
|
|
||||||
# Offense count: 1
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
|
||||||
# SupportedStyles: require_parentheses, require_no_parentheses, require_no_parentheses_except_multiline
|
|
||||||
Style/MethodDefParentheses:
|
|
||||||
Enabled: false
|
|
||||||
|
|
||||||
# Offense count: 1
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
# Configuration parameters: EnforcedStyle, SupportedStyles, IndentationWidth.
|
|
||||||
# SupportedStyles: aligned, indented
|
|
||||||
Style/MultilineOperationIndentation:
|
|
||||||
Enabled: false
|
|
||||||
|
|
||||||
# Offense count: 1
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
Style/NegatedIf:
|
|
||||||
Exclude:
|
|
||||||
- 'lib/action_controller/serialization.rb'
|
|
||||||
|
|
||||||
# Offense count: 1
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
Style/PerlBackrefs:
|
|
||||||
Exclude:
|
|
||||||
- 'test/fixtures/poro.rb'
|
|
||||||
|
|
||||||
# Offense count: 3
|
|
||||||
# Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist.
|
|
||||||
# NamePrefix: is_, has_, have_
|
|
||||||
# NamePrefixBlacklist: is_, has_, have_
|
|
||||||
# NameWhitelist: is_a?
|
|
||||||
Style/PredicateName:
|
|
||||||
Exclude:
|
|
||||||
- 'lib/active_model/serializer/associations.rb'
|
|
||||||
- 'test/action_controller/json_api/linked_test.rb'
|
|
||||||
|
|
||||||
# Offense count: 1
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
Style/RedundantSelf:
|
|
||||||
Exclude:
|
|
||||||
- 'test/fixtures/poro.rb'
|
|
||||||
|
|
||||||
# Offense count: 1
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
# Configuration parameters: AllowIfMethodIsEmpty.
|
|
||||||
Style/SingleLineMethods:
|
|
||||||
Exclude:
|
|
||||||
- 'test/serializers/serializer_for_test.rb'
|
|
||||||
|
|
||||||
# Offense count: 4
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
|
||||||
# SupportedStyles: single_quotes, double_quotes
|
|
||||||
Style/StringLiteralsInInterpolation:
|
|
||||||
Enabled: false
|
|
||||||
|
|
||||||
# Offense count: 1
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
|
||||||
# SupportedStyles: final_newline, final_blank_line
|
|
||||||
Style/TrailingBlankLines:
|
|
||||||
Exclude:
|
|
||||||
- 'test/adapter/null_test.rb'
|
|
||||||
6
Rakefile
6
Rakefile
@ -5,7 +5,7 @@ rescue LoadError
|
|||||||
end
|
end
|
||||||
begin
|
begin
|
||||||
require 'simplecov'
|
require 'simplecov'
|
||||||
rescue LoadError
|
rescue LoadError # rubocop:disable Lint/HandleExceptions
|
||||||
end
|
end
|
||||||
|
|
||||||
Bundler::GemHelper.install_tasks
|
Bundler::GemHelper.install_tasks
|
||||||
@ -33,7 +33,7 @@ end
|
|||||||
begin
|
begin
|
||||||
require 'rubocop'
|
require 'rubocop'
|
||||||
require 'rubocop/rake_task'
|
require 'rubocop/rake_task'
|
||||||
rescue LoadError
|
rescue LoadError # rubocop:disable Lint/HandleExceptions
|
||||||
else
|
else
|
||||||
Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop)
|
Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop)
|
||||||
require 'rbconfig'
|
require 'rbconfig'
|
||||||
@ -100,4 +100,4 @@ else
|
|||||||
end
|
end
|
||||||
|
|
||||||
desc 'CI test task'
|
desc 'CI test task'
|
||||||
task :ci => [:default]
|
task ci: [:default]
|
||||||
|
|||||||
@ -19,12 +19,13 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def serialization_scope
|
def serialization_scope
|
||||||
send(_serialization_scope) if _serialization_scope &&
|
return unless _serialization_scope && respond_to?(_serialization_scope, true)
|
||||||
respond_to?(_serialization_scope, true)
|
|
||||||
|
send(_serialization_scope)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_serializer(resource, options = {})
|
def get_serializer(resource, options = {})
|
||||||
if !use_adapter?
|
unless use_adapter?
|
||||||
warn 'ActionController::Serialization#use_adapter? has been removed. '\
|
warn 'ActionController::Serialization#use_adapter? has been removed. '\
|
||||||
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource.new"
|
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource.new"
|
||||||
options[:adapter] = false
|
options[:adapter] = false
|
||||||
|
|||||||
@ -125,10 +125,9 @@ module ActiveModel
|
|||||||
self.root = instance_options[:root]
|
self.root = instance_options[:root]
|
||||||
self.scope = instance_options[:scope]
|
self.scope = instance_options[:scope]
|
||||||
|
|
||||||
scope_name = instance_options[:scope_name]
|
return if !(scope_name = instance_options[:scope_name]) || respond_to?(scope_name)
|
||||||
if scope_name && !respond_to?(scope_name)
|
|
||||||
define_singleton_method scope_name, lambda { scope }
|
define_singleton_method scope_name, -> { scope }
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def success?
|
def success?
|
||||||
|
|||||||
@ -1,9 +1,12 @@
|
|||||||
require 'active_model/serializer/collection_serializer'
|
require 'active_model/serializer/collection_serializer'
|
||||||
class ActiveModel::Serializer
|
|
||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
class ArraySerializer < CollectionSerializer
|
class ArraySerializer < CollectionSerializer
|
||||||
class << self
|
class << self
|
||||||
extend ActiveModelSerializers::Deprecate
|
extend ActiveModelSerializers::Deprecate
|
||||||
deprecate :new, 'ActiveModel::Serializer::CollectionSerializer.'
|
deprecate :new, 'ActiveModel::Serializer::CollectionSerializer.'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -39,7 +39,7 @@ module ActiveModel
|
|||||||
# @example
|
# @example
|
||||||
# has_many :comments, serializer: CommentSummarySerializer
|
# has_many :comments, serializer: CommentSummarySerializer
|
||||||
#
|
#
|
||||||
def has_many(name, options = {}, &block)
|
def has_many(name, options = {}, &block) # rubocop:disable Style/PredicateName
|
||||||
associate(HasManyReflection.new(name, options, block))
|
associate(HasManyReflection.new(name, options, block))
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -61,7 +61,7 @@ module ActiveModel
|
|||||||
# @example
|
# @example
|
||||||
# has_one :author, serializer: AuthorSerializer
|
# has_one :author, serializer: AuthorSerializer
|
||||||
#
|
#
|
||||||
def has_one(name, options = {}, &block)
|
def has_one(name, options = {}, &block) # rubocop:disable Style/PredicateName
|
||||||
associate(HasOneReflection.new(name, options, block))
|
associate(HasOneReflection.new(name, options, block))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
class ActiveModel::Serializer::ErrorSerializer < ActiveModel::Serializer
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
class ErrorSerializer < ActiveModel::Serializer
|
||||||
# @return [Hash<field_name,Array<error_message>>]
|
# @return [Hash<field_name,Array<error_message>>]
|
||||||
def as_json
|
def as_json
|
||||||
object.errors.messages
|
object.errors.messages
|
||||||
@ -7,4 +9,6 @@ class ActiveModel::Serializer::ErrorSerializer < ActiveModel::Serializer
|
|||||||
def success?
|
def success?
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
require 'active_model/serializer/error_serializer'
|
require 'active_model/serializer/error_serializer'
|
||||||
class ActiveModel::Serializer::ErrorsSerializer
|
|
||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
class ErrorsSerializer
|
||||||
include Enumerable
|
include Enumerable
|
||||||
delegate :each, to: :@serializers
|
delegate :each, to: :@serializers
|
||||||
attr_reader :object, :root
|
attr_reader :object, :root
|
||||||
@ -24,4 +27,6 @@ class ActiveModel::Serializer::ErrorsSerializer
|
|||||||
protected
|
protected
|
||||||
|
|
||||||
attr_reader :serializers
|
attr_reader :serializers
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
module ActiveModel::Serializer::Lint
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
module Lint
|
||||||
# == Active \Model \Serializer \Lint \Tests
|
# == Active \Model \Serializer \Lint \Tests
|
||||||
#
|
#
|
||||||
# You can test whether an object is compliant with the Active \Model \Serializers
|
# You can test whether an object is compliant with the Active \Model \Serializers
|
||||||
@ -143,4 +145,6 @@ module ActiveModel::Serializer::Lint
|
|||||||
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
|
||||||
|
|||||||
@ -36,7 +36,7 @@ module ActiveModelSerializers
|
|||||||
target = is_a?(Module) ? "#{self}." : "#{self.class}#"
|
target = is_a?(Module) ? "#{self}." : "#{self.class}#"
|
||||||
msg = ["NOTE: #{target}#{name} is deprecated",
|
msg = ["NOTE: #{target}#{name} is deprecated",
|
||||||
replacement == :none ? ' with no replacement' : "; use #{replacement} instead",
|
replacement == :none ? ' with no replacement' : "; use #{replacement} instead",
|
||||||
"\n#{target}#{name} called from #{ActiveModelSerializers.location_of_caller.join(":")}"]
|
"\n#{target}#{name} called from #{ActiveModelSerializers.location_of_caller.join(':')}"]
|
||||||
warn "#{msg.join}."
|
warn "#{msg.join}."
|
||||||
send old, *args, &block
|
send old, *args, &block
|
||||||
end
|
end
|
||||||
|
|||||||
@ -21,7 +21,7 @@ module ActiveModelSerializers
|
|||||||
|
|
||||||
# Defaults to the downcased model name and updated_at
|
# Defaults to the downcased model name and updated_at
|
||||||
def cache_key
|
def cache_key
|
||||||
attributes.fetch(:cache_key) { "#{self.class.name.downcase}/#{id}-#{updated_at.strftime("%Y%m%d%H%M%S%9N")}" }
|
attributes.fetch(:cache_key) { "#{self.class.name.downcase}/#{id}-#{updated_at.strftime('%Y%m%d%H%M%S%9N')}" }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Defaults to the time the serializer file was modified.
|
# Defaults to the time the serializer file was modified.
|
||||||
|
|||||||
@ -23,7 +23,7 @@ module ActiveModelSerializers
|
|||||||
# This hook is run after the action_controller railtie has set the configuration
|
# This hook is run after the action_controller railtie has set the configuration
|
||||||
# based on the *environment* configuration and before any config/initializers are run
|
# based on the *environment* configuration and before any config/initializers are run
|
||||||
# and also before eager_loading (if enabled).
|
# and also before eager_loading (if enabled).
|
||||||
initializer 'active_model_serializers.set_configs', :after => 'action_controller.set_configs' do
|
initializer 'active_model_serializers.set_configs', after: 'action_controller.set_configs' do
|
||||||
ActiveModelSerializers.logger = Rails.configuration.action_controller.logger
|
ActiveModelSerializers.logger = Rails.configuration.action_controller.logger
|
||||||
ActiveModelSerializers.config.perform_caching = Rails.configuration.action_controller.perform_caching
|
ActiveModelSerializers.config.perform_caching = Rails.configuration.action_controller.perform_caching
|
||||||
# We want this hook to run after the config has been set, even if ActionController has already loaded.
|
# We want this hook to run after the config has been set, even if ActionController has already loaded.
|
||||||
|
|||||||
@ -22,7 +22,8 @@
|
|||||||
# 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
|
||||||
|
module Jsonapi
|
||||||
MEDIA_TYPE = 'application/vnd.api+json'.freeze
|
MEDIA_TYPE = 'application/vnd.api+json'.freeze
|
||||||
HEADERS = {
|
HEADERS = {
|
||||||
response: { 'CONTENT_TYPE'.freeze => MEDIA_TYPE },
|
response: { 'CONTENT_TYPE'.freeze => MEDIA_TYPE },
|
||||||
@ -53,7 +54,7 @@ module ActiveModelSerializers::Jsonapi
|
|||||||
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
|
||||||
@ -67,6 +68,7 @@ module ActiveModelSerializers::Jsonapi
|
|||||||
get_serializer(json, options)
|
get_serializer(json, options)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ActiveModelSerializers::Jsonapi.install
|
ActiveModelSerializers::Jsonapi.install
|
||||||
|
|||||||
@ -2,11 +2,11 @@ module Rails
|
|||||||
module Generators
|
module Generators
|
||||||
class SerializerGenerator < NamedBase
|
class SerializerGenerator < NamedBase
|
||||||
source_root File.expand_path('../templates', __FILE__)
|
source_root File.expand_path('../templates', __FILE__)
|
||||||
check_class_collision :suffix => 'Serializer'
|
check_class_collision suffix: 'Serializer'
|
||||||
|
|
||||||
argument :attributes, :type => :array, :default => [], :banner => 'field:type field:type'
|
argument :attributes, type: :array, default: [], banner: 'field:type field:type'
|
||||||
|
|
||||||
class_option :parent, :type => :string, :desc => 'The parent class for the generated serializer'
|
class_option :parent, type: :string, desc: 'The parent class for the generated serializer'
|
||||||
|
|
||||||
def create_serializer_file
|
def create_serializer_file
|
||||||
template 'serializer.rb.erb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
|
template 'serializer.rb.erb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
|
||||||
|
|||||||
@ -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
|
||||||
|
module ActiveModelSerializers
|
||||||
extend ActiveSupport::Concern
|
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
|
||||||
|
|||||||
@ -5,17 +5,17 @@ module ActionController
|
|||||||
class AdapterSelectorTest < ActionController::TestCase
|
class AdapterSelectorTest < ActionController::TestCase
|
||||||
class AdapterSelectorTestController < ActionController::Base
|
class AdapterSelectorTestController < ActionController::Base
|
||||||
def render_using_default_adapter
|
def render_using_default_adapter
|
||||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||||
render json: @profile
|
render json: @profile
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_using_adapter_override
|
def render_using_adapter_override
|
||||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||||
render json: @profile, adapter: :json_api
|
render json: @profile, adapter: :json_api
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_skipping_adapter
|
def render_skipping_adapter
|
||||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||||
render json: @profile, adapter: false
|
render json: @profile, adapter: false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -8,11 +8,10 @@ module ActionController
|
|||||||
get :render_resource_with_errors
|
get :render_resource_with_errors
|
||||||
|
|
||||||
expected_errors_object = {
|
expected_errors_object = {
|
||||||
:errors =>
|
errors: [
|
||||||
[
|
{ source: { pointer: '/data/attributes/name' }, detail: 'cannot be nil' },
|
||||||
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'cannot be nil' },
|
{ source: { pointer: '/data/attributes/name' }, detail: 'must be longer' },
|
||||||
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'must be longer' },
|
{ source: { pointer: '/data/attributes/id' }, detail: 'must be a uuid' }
|
||||||
{ :source => { :pointer => '/data/attributes/id' }, :detail => 'must be a uuid' }
|
|
||||||
]
|
]
|
||||||
}.to_json
|
}.to_json
|
||||||
assert_equal json_reponse_body.to_json, expected_errors_object
|
assert_equal json_reponse_body.to_json, expected_errors_object
|
||||||
|
|||||||
@ -87,7 +87,7 @@ module ActionController
|
|||||||
setup do
|
setup do
|
||||||
@routes = Rails.application.routes.draw do
|
@routes = Rails.application.routes.draw do
|
||||||
ActiveSupport::Deprecation.silence do
|
ActiveSupport::Deprecation.silence do
|
||||||
match ':action', :to => LinkedTestController, via: [:get, :post]
|
match ':action', to: LinkedTestController, via: [:get, :post]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -183,17 +183,17 @@ module ActionController
|
|||||||
get '/render_resource_with_missing_nested_has_many_include'
|
get '/render_resource_with_missing_nested_has_many_include'
|
||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
assert response.key? 'included'
|
assert response.key? 'included'
|
||||||
refute has_type?(response['included'], 'roles')
|
refute include_type?(response['included'], 'roles')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_collection_with_missing_nested_has_many_include
|
def test_render_collection_with_missing_nested_has_many_include
|
||||||
get '/render_collection_with_missing_nested_has_many_include'
|
get '/render_collection_with_missing_nested_has_many_include'
|
||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
assert response.key? 'included'
|
assert response.key? 'included'
|
||||||
assert has_type?(response['included'], 'roles')
|
assert include_type?(response['included'], 'roles')
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_type?(collection, value)
|
def include_type?(collection, value)
|
||||||
collection.detect { |i| i['type'] == value }
|
collection.detect { |i| i['type'] == value }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -14,9 +14,9 @@ module ActionController
|
|||||||
class PaginationTestController < ActionController::Base
|
class PaginationTestController < ActionController::Base
|
||||||
def setup
|
def setup
|
||||||
@array = [
|
@array = [
|
||||||
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
|
||||||
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
|
Profile.new(name: 'Name 2', description: 'Description 2', comments: 'Comments 2'),
|
||||||
Profile.new({ name: 'Name 3', description: 'Description 3', comments: 'Comments 3' })
|
Profile.new(name: 'Name 3', description: 'Description 3', comments: 'Comments 3')
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -74,7 +74,7 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update_and_render_object_with_cache_enabled
|
def update_and_render_object_with_cache_enabled
|
||||||
@post.updated_at = Time.now
|
@post.updated_at = Time.zone.now
|
||||||
|
|
||||||
generate_cached_serializer(@post)
|
generate_cached_serializer(@post)
|
||||||
render json: @post
|
render json: @post
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class ActiveModelSerializers::JsonPointerTest < ActiveSupport::TestCase
|
module ActiveModelSerializers
|
||||||
|
class JsonPointerTest < ActiveSupport::TestCase
|
||||||
def test_attribute_pointer
|
def test_attribute_pointer
|
||||||
attribute_name = 'title'
|
attribute_name = 'title'
|
||||||
pointer = ActiveModelSerializers::JsonPointer.new(:attribute, attribute_name)
|
pointer = ActiveModelSerializers::JsonPointer.new(:attribute, attribute_name)
|
||||||
@ -17,4 +18,5 @@ class ActiveModelSerializers::JsonPointerTest < ActiveSupport::TestCase
|
|||||||
ActiveModelSerializers::JsonPointer.new(:unknown)
|
ActiveModelSerializers::JsonPointer.new(:unknown)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,20 +1,21 @@
|
|||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class ActiveModelSerializers::KeyTransformTest < ActiveSupport::TestCase
|
module ActiveModelSerializers
|
||||||
|
class KeyTransformTest < ActiveSupport::TestCase
|
||||||
def test_camel
|
def test_camel
|
||||||
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' },
|
||||||
@ -72,15 +73,15 @@ class ActiveModelSerializers::KeyTransformTest < ActiveSupport::TestCase
|
|||||||
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' },
|
||||||
@ -137,7 +138,7 @@ class ActiveModelSerializers::KeyTransformTest < ActiveSupport::TestCase
|
|||||||
obj = Object.new
|
obj = Object.new
|
||||||
scenarios = [
|
scenarios = [
|
||||||
{
|
{
|
||||||
value: { :some_key => 'value' },
|
value: { some_key: 'value' },
|
||||||
expected: { :"some-key" => 'value' }
|
expected: { :"some-key" => 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -145,7 +146,7 @@ class ActiveModelSerializers::KeyTransformTest < ActiveSupport::TestCase
|
|||||||
expected: { 'some-key' => 'value' }
|
expected: { 'some-key' => 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: { :SomeKey => 'value' },
|
value: { SomeKey: 'value' },
|
||||||
expected: { :"some-key" => 'value' }
|
expected: { :"some-key" => 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -153,7 +154,7 @@ class ActiveModelSerializers::KeyTransformTest < ActiveSupport::TestCase
|
|||||||
expected: { 'some-key' => 'value' }
|
expected: { 'some-key' => 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
value: { :someKey => 'value' },
|
value: { someKey: 'value' },
|
||||||
expected: { :"some-key" => 'value' }
|
expected: { :"some-key" => 'value' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -200,23 +201,23 @@ class ActiveModelSerializers::KeyTransformTest < ActiveSupport::TestCase
|
|||||||
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' },
|
||||||
@ -260,4 +261,5 @@ class ActiveModelSerializers::KeyTransformTest < ActiveSupport::TestCase
|
|||||||
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
|
||||||
|
class ModelTest < ActiveSupport::TestCase
|
||||||
include ActiveModel::Serializer::Lint::Tests
|
include ActiveModel::Serializer::Lint::Tests
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@resource = ActiveModelSerializers::Model.new
|
@resource = ActiveModelSerializers::Model.new
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -44,7 +44,7 @@ class JsonApiRendererTest < ActionDispatch::IntegrationTest
|
|||||||
|
|
||||||
Rails.application.routes.draw do
|
Rails.application.routes.draw do
|
||||||
ActiveSupport::Deprecation.silence do
|
ActiveSupport::Deprecation.silence do
|
||||||
match ':action', :to => TestController, via: [:get, :post]
|
match ':action', to: TestController, via: [:get, :post]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -95,7 +95,7 @@ class JsonApiRendererTest < ActionDispatch::IntegrationTest
|
|||||||
|
|
||||||
Rails.application.routes.draw do
|
Rails.application.routes.draw do
|
||||||
ActiveSupport::Deprecation.silence do
|
ActiveSupport::Deprecation.silence do
|
||||||
match ':action', :to => TestController, via: [:get, :post]
|
match ':action', to: TestController, via: [:get, :post]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -23,7 +23,7 @@ module ActiveModelSerializers
|
|||||||
assert_equal serializable_resource.serializer_instance.object, @resource
|
assert_equal serializable_resource.serializer_instance.object, @resource
|
||||||
|
|
||||||
expected_errors_object = {
|
expected_errors_object = {
|
||||||
:errors => [
|
errors: [
|
||||||
{
|
{
|
||||||
source: { pointer: '/data/attributes/name' },
|
source: { pointer: '/data/attributes/name' },
|
||||||
detail: 'cannot be nil'
|
detail: 'cannot be nil'
|
||||||
@ -48,10 +48,10 @@ module ActiveModelSerializers
|
|||||||
assert_equal serializable_resource.serializer_instance.object, @resource
|
assert_equal serializable_resource.serializer_instance.object, @resource
|
||||||
|
|
||||||
expected_errors_object = {
|
expected_errors_object = {
|
||||||
:errors => [
|
errors: [
|
||||||
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'cannot be nil' },
|
{ source: { pointer: '/data/attributes/name' }, detail: 'cannot be nil' },
|
||||||
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'must be longer' },
|
{ source: { pointer: '/data/attributes/name' }, detail: 'must be longer' },
|
||||||
{ :source => { :pointer => '/data/attributes/id' }, :detail => 'must be a uuid' }
|
{ source: { pointer: '/data/attributes/id' }, detail: 'must be a uuid' }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
assert_equal serializable_resource.as_json, expected_errors_object
|
assert_equal serializable_resource.as_json, expected_errors_object
|
||||||
|
|||||||
@ -22,10 +22,8 @@ module ActiveModelSerializers
|
|||||||
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
|
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
reviews: { data: [
|
reviews: { data: [{ type: 'comments', id: '1' },
|
||||||
{ type: 'comments', id: '1' },
|
{ type: 'comments', id: '2' }] },
|
||||||
{ type: 'comments', id: '2' }
|
|
||||||
] },
|
|
||||||
writer: { data: { type: 'authors', id: '1' } },
|
writer: { data: { type: 'authors', id: '1' } },
|
||||||
site: { data: { type: 'blogs', id: '1' } }
|
site: { data: { type: 'blogs', id: '1' } }
|
||||||
}, adapter.serializable_hash[:data][:relationships])
|
}, adapter.serializable_hash[:data][:relationships])
|
||||||
|
|||||||
@ -17,7 +17,7 @@ module ActiveModelSerializers
|
|||||||
@first_post = Post.new(id: 10, title: 'Hello!!', body: 'Hello, world!!')
|
@first_post = Post.new(id: 10, title: 'Hello!!', body: 'Hello, world!!')
|
||||||
@second_post = Post.new(id: 20, title: 'New Post', body: 'Body')
|
@second_post = Post.new(id: 20, title: 'New Post', body: 'Body')
|
||||||
@third_post = Post.new(id: 30, title: 'Yet Another Post', body: 'Body')
|
@third_post = Post.new(id: 30, title: 'Yet Another Post', body: 'Body')
|
||||||
@blog = Blog.new({ name: 'AMS Blog' })
|
@blog = Blog.new(name: 'AMS Blog')
|
||||||
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||||
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
||||||
@first_post.blog = @blog
|
@first_post.blog = @blog
|
||||||
|
|||||||
@ -13,11 +13,11 @@ module ActiveModelSerializers
|
|||||||
def setup
|
def setup
|
||||||
ActionController::Base.cache_store.clear
|
ActionController::Base.cache_store.clear
|
||||||
@array = [
|
@array = [
|
||||||
Profile.new({ id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
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: 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: 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: 4, name: 'Name 4', description: 'Description 4', comments: 'Comments 4'),
|
||||||
Profile.new({ id: 5, name: 'Name 5', description: 'Description 5', comments: 'Comments 5' })
|
Profile.new(id: 5, name: 'Name 5', description: 'Description 5', comments: 'Comments 5')
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -122,7 +122,7 @@ module ActiveModelSerializers
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_pagination_links_with_additional_params
|
def test_pagination_links_with_additional_params
|
||||||
adapter = load_adapter(using_will_paginate, mock_request({ test: 'test' }))
|
adapter = load_adapter(using_will_paginate, mock_request(test: 'test'))
|
||||||
|
|
||||||
assert_equal expected_response_with_pagination_links_and_additional_params,
|
assert_equal expected_response_with_pagination_links_and_additional_params,
|
||||||
adapter.serializable_hash
|
adapter.serializable_hash
|
||||||
|
|||||||
@ -68,9 +68,9 @@ module ActiveModel
|
|||||||
adapter: :json_api
|
adapter: :json_api
|
||||||
).serializable_hash
|
).serializable_hash
|
||||||
expected = {
|
expected = {
|
||||||
:data => [
|
data: [
|
||||||
{ :id => '1337', :type => 'posts', :meta => { :"comments-count" => 0 } },
|
{ id: '1337', type: 'posts', meta: { :"comments-count" => 0 } },
|
||||||
{ :id => '1339', :type => 'posts', :meta => { :"comments-count" => 1 } }
|
{ id: '1339', type: 'posts', meta: { :"comments-count" => 1 } }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
assert_equal(expected, hash)
|
assert_equal(expected, hash)
|
||||||
|
|||||||
@ -332,14 +332,14 @@ module ActiveModelSerializers
|
|||||||
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
|
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
|
||||||
result = adapter.serializable_hash
|
result = adapter.serializable_hash
|
||||||
expected_errors_object = {
|
expected_errors_object = {
|
||||||
:errors => [
|
errors: [
|
||||||
{
|
{
|
||||||
:source => { :pointer => '/data/attributes/published-at' },
|
source: { pointer: '/data/attributes/published-at' },
|
||||||
:detail => 'must be in the future'
|
detail: 'must be in the future'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
:source => { :pointer => '/data/attributes/title' },
|
source: { pointer: '/data/attributes/title' },
|
||||||
:detail => 'must be longer'
|
detail: 'must be longer'
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@ -357,14 +357,14 @@ module ActiveModelSerializers
|
|||||||
adapter.serializable_hash
|
adapter.serializable_hash
|
||||||
end
|
end
|
||||||
expected_errors_object = {
|
expected_errors_object = {
|
||||||
:Errors => [
|
Errors: [
|
||||||
{
|
{
|
||||||
:Source => { :Pointer => '/data/attributes/PublishedAt' },
|
Source: { Pointer: '/data/attributes/PublishedAt' },
|
||||||
:Detail => 'must be in the future'
|
Detail: 'must be in the future'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
:Source => { :Pointer => '/data/attributes/Title' },
|
Source: { Pointer: '/data/attributes/Title' },
|
||||||
:Detail => 'must be longer'
|
Detail: 'must be longer'
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@ -382,14 +382,14 @@ module ActiveModelSerializers
|
|||||||
adapter.serializable_hash
|
adapter.serializable_hash
|
||||||
end
|
end
|
||||||
expected_errors_object = {
|
expected_errors_object = {
|
||||||
:Errors => [
|
Errors: [
|
||||||
{
|
{
|
||||||
:Source => { :Pointer => '/data/attributes/PublishedAt' },
|
Source: { Pointer: '/data/attributes/PublishedAt' },
|
||||||
:Detail => 'must be in the future'
|
Detail: 'must be in the future'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
:Source => { :Pointer => '/data/attributes/Title' },
|
Source: { Pointer: '/data/attributes/Title' },
|
||||||
:Detail => 'must be longer'
|
Detail: 'must be longer'
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@ -408,14 +408,14 @@ module ActiveModelSerializers
|
|||||||
result = adapter.serializable_hash
|
result = adapter.serializable_hash
|
||||||
|
|
||||||
expected_errors_object = {
|
expected_errors_object = {
|
||||||
:errors => [
|
errors: [
|
||||||
{
|
{
|
||||||
:source => { :pointer => '/data/attributes/published-at' },
|
source: { pointer: '/data/attributes/published-at' },
|
||||||
:detail => 'must be in the future'
|
detail: 'must be in the future'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
:source => { :pointer => '/data/attributes/title' },
|
source: { pointer: '/data/attributes/title' },
|
||||||
:detail => 'must be longer'
|
detail: 'must be longer'
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@ -434,9 +434,9 @@ module ActiveModelSerializers
|
|||||||
result = adapter.serializable_hash
|
result = adapter.serializable_hash
|
||||||
|
|
||||||
expected_errors_object = {
|
expected_errors_object = {
|
||||||
:errors => [
|
errors: [
|
||||||
{ :source => { :pointer => '/data/attributes/published_at' }, :detail => 'must be in the future' },
|
{ source: { pointer: '/data/attributes/published_at' }, detail: 'must be in the future' },
|
||||||
{ :source => { :pointer => '/data/attributes/title' }, :detail => 'must be longer' }
|
{ source: { pointer: '/data/attributes/title' }, detail: 'must be longer' }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
assert_equal expected_errors_object, result
|
assert_equal expected_errors_object, result
|
||||||
@ -470,9 +470,9 @@ module ActiveModelSerializers
|
|||||||
result = adapter.serializable_hash
|
result = adapter.serializable_hash
|
||||||
|
|
||||||
expected_errors_object = {
|
expected_errors_object = {
|
||||||
:Errors => [
|
Errors: [
|
||||||
{ :Source => { :Pointer => '/data/attributes/PublishedAt' }, :Detail => 'must be in the future' },
|
{ Source: { Pointer: '/data/attributes/PublishedAt' }, Detail: 'must be in the future' },
|
||||||
{ :Source => { :Pointer => '/data/attributes/Title' }, :Detail => 'must be longer' }
|
{ Source: { Pointer: '/data/attributes/Title' }, Detail: 'must be longer' }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
assert_equal expected_errors_object, result
|
assert_equal expected_errors_object, result
|
||||||
@ -490,9 +490,9 @@ module ActiveModelSerializers
|
|||||||
result = adapter.serializable_hash
|
result = adapter.serializable_hash
|
||||||
|
|
||||||
expected_errors_object = {
|
expected_errors_object = {
|
||||||
:errors => [
|
errors: [
|
||||||
{ :source => { :pointer => '/data/attributes/publishedAt' }, :detail => 'must be in the future' },
|
{ source: { pointer: '/data/attributes/publishedAt' }, detail: 'must be in the future' },
|
||||||
{ :source => { :pointer => '/data/attributes/title' }, :detail => 'must be longer' }
|
{ source: { pointer: '/data/attributes/title' }, detail: 'must be longer' }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
assert_equal expected_errors_object, result
|
assert_equal expected_errors_object, result
|
||||||
|
|||||||
@ -47,7 +47,7 @@ module ActiveModel
|
|||||||
assert_equal(expected_type, hash.fetch(:data).fetch(:type))
|
assert_equal(expected_type, hash.fetch(:data).fetch(:type))
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_jsonapi_resource_type inflection
|
def with_jsonapi_resource_type(inflection)
|
||||||
old_inflection = ActiveModelSerializers.config.jsonapi_resource_type
|
old_inflection = ActiveModelSerializers.config.jsonapi_resource_type
|
||||||
ActiveModelSerializers.config.jsonapi_resource_type = inflection
|
ActiveModelSerializers.config.jsonapi_resource_type = inflection
|
||||||
yield
|
yield
|
||||||
|
|||||||
@ -4,7 +4,7 @@ module ActiveModelSerializers
|
|||||||
module Adapter
|
module Adapter
|
||||||
class NullTest < ActiveSupport::TestCase
|
class NullTest < ActiveSupport::TestCase
|
||||||
def setup
|
def setup
|
||||||
profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||||
serializer = ProfileSerializer.new(profile)
|
serializer = ProfileSerializer.new(profile)
|
||||||
|
|
||||||
@adapter = Null.new(serializer)
|
@adapter = Null.new(serializer)
|
||||||
@ -20,4 +20,3 @@ module ActiveModelSerializers
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -51,7 +51,7 @@ module ActiveModelSerializers
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_create_adapter_with_override
|
def test_create_adapter_with_override
|
||||||
adapter = ActiveModelSerializers::Adapter.create(@serializer, { adapter: :json_api })
|
adapter = ActiveModelSerializers::Adapter.create(@serializer, adapter: :json_api)
|
||||||
assert_equal ActiveModelSerializers::Adapter::JsonApi, adapter.class
|
assert_equal ActiveModelSerializers::Adapter::JsonApi, adapter.class
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -101,7 +101,7 @@ module ActiveModelSerializers
|
|||||||
uncached_author_serializer = AuthorSerializer.new(uncached_author)
|
uncached_author_serializer = AuthorSerializer.new(uncached_author)
|
||||||
|
|
||||||
render_object_with_cache(uncached_author)
|
render_object_with_cache(uncached_author)
|
||||||
key = "#{uncached_author_serializer.class._cache_key}/#{uncached_author_serializer.object.id}-#{uncached_author_serializer.object.updated_at.strftime("%Y%m%d%H%M%S%9N")}"
|
key = "#{uncached_author_serializer.class._cache_key}/#{uncached_author_serializer.object.id}-#{uncached_author_serializer.object.updated_at.strftime('%Y%m%d%H%M%S%9N')}"
|
||||||
key = "#{key}/#{adapter.cache_key}"
|
key = "#{key}/#{adapter.cache_key}"
|
||||||
assert_equal(uncached_author_serializer.attributes.to_json, cache_store.fetch(key).to_json)
|
assert_equal(uncached_author_serializer.attributes.to_json, cache_store.fetch(key).to_json)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -11,7 +11,7 @@ module ActiveModel
|
|||||||
@comment = Comment.new
|
@comment = Comment.new
|
||||||
@post = Post.new
|
@post = Post.new
|
||||||
@resource = build_named_collection @comment, @post
|
@resource = build_named_collection @comment, @post
|
||||||
@serializer = collection_serializer.new(@resource, { some: :options })
|
@serializer = collection_serializer.new(@resource, some: :options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def collection_serializer
|
def collection_serializer
|
||||||
@ -44,7 +44,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_serializer_option_not_passed_to_each_serializer
|
def test_serializer_option_not_passed_to_each_serializer
|
||||||
serializers = collection_serializer.new([@post], { serializer: PostSerializer }).to_a
|
serializers = collection_serializer.new([@post], serializer: PostSerializer).to_a
|
||||||
|
|
||||||
refute serializers.first.custom_options.key?(:serializer)
|
refute serializers.first.custom_options.key?(:serializer)
|
||||||
end
|
end
|
||||||
|
|||||||
4
test/fixtures/poro.rb
vendored
4
test/fixtures/poro.rb
vendored
@ -8,7 +8,7 @@ class Model < ActiveModelSerializers::Model
|
|||||||
# Convenience when not adding @attributes readers and writers
|
# Convenience when not adding @attributes readers and writers
|
||||||
def method_missing(meth, *args)
|
def method_missing(meth, *args)
|
||||||
if meth.to_s =~ /^(.*)=$/
|
if meth.to_s =~ /^(.*)=$/
|
||||||
attributes[$1.to_sym] = args[0]
|
attributes[Regexp.last_match(1).to_sym] = args[0]
|
||||||
elsif attributes.key?(meth)
|
elsif attributes.key?(meth)
|
||||||
attributes[meth]
|
attributes[meth]
|
||||||
else
|
else
|
||||||
@ -64,7 +64,7 @@ VirtualValue = Class.new(Model)
|
|||||||
Comment = Class.new(Model) do
|
Comment = Class.new(Model) do
|
||||||
# Uses a custom non-time-based cache key
|
# Uses a custom non-time-based cache key
|
||||||
def cache_key
|
def cache_key
|
||||||
"#{self.class.name.downcase}/#{self.id}"
|
"#{self.class.name.downcase}/#{id}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -5,7 +5,8 @@ require 'kaminari'
|
|||||||
require 'kaminari/hooks'
|
require 'kaminari/hooks'
|
||||||
::Kaminari::Hooks.init
|
::Kaminari::Hooks.init
|
||||||
|
|
||||||
class ActiveModelSerializers::GrapeTest < ActiveSupport::TestCase
|
module ActiveModelSerializers
|
||||||
|
class GrapeTest < ActiveSupport::TestCase
|
||||||
include Rack::Test::Methods
|
include Rack::Test::Methods
|
||||||
module Models
|
module Models
|
||||||
def self.model1
|
def self.model1
|
||||||
@ -173,4 +174,5 @@ class ActiveModelSerializers::GrapeTest < ActiveSupport::TestCase
|
|||||||
assert representation.include?('links')
|
assert representation.include?('links')
|
||||||
assert representation['links'].count > 0
|
assert representation['links'].count > 0
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -30,7 +30,7 @@ module ActiveModel
|
|||||||
def errors
|
def errors
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.human_attribute_name(attr, options = {})
|
def self.human_attribute_name(_, _ = {})
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.lookup_ancestors
|
def self.lookup_ancestors
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class ActiveModelSerializers::LoggerTest < ActiveSupport::TestCase
|
module ActiveModelSerializers
|
||||||
|
class LoggerTest < ActiveSupport::TestCase
|
||||||
def test_logger_is_set_to_action_controller_logger_when_initializer_runs
|
def test_logger_is_set_to_action_controller_logger_when_initializer_runs
|
||||||
assert_equal $action_controller_logger, ActionController::Base.logger # rubocop:disable Style/GlobalVars
|
assert_equal $action_controller_logger, ActionController::Base.logger # rubocop:disable Style/GlobalVars
|
||||||
end
|
end
|
||||||
@ -15,4 +16,5 @@ class ActiveModelSerializers::LoggerTest < ActiveSupport::TestCase
|
|||||||
ensure
|
ensure
|
||||||
ActiveModelSerializers.logger = original_logger
|
ActiveModelSerializers.logger = original_logger
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -3,7 +3,7 @@ require 'test_helper'
|
|||||||
module ActiveModelSerializers
|
module ActiveModelSerializers
|
||||||
class SerializableResourceTest < ActiveSupport::TestCase
|
class SerializableResourceTest < ActiveSupport::TestCase
|
||||||
def setup
|
def setup
|
||||||
@resource = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
@resource = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||||
@serializer = ProfileSerializer.new(@resource)
|
@serializer = ProfileSerializer.new(@resource)
|
||||||
@adapter = ActiveModelSerializers::Adapter.create(@serializer)
|
@adapter = ActiveModelSerializers::Adapter.create(@serializer)
|
||||||
@serializable_resource = SerializableResource.new(@resource)
|
@serializable_resource = SerializableResource.new(@resource)
|
||||||
@ -32,11 +32,11 @@ module ActiveModelSerializers
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_use_adapter_with_adapter_option
|
def test_use_adapter_with_adapter_option
|
||||||
assert SerializableResource.new(@resource, { adapter: 'json' }).use_adapter?
|
assert SerializableResource.new(@resource, adapter: 'json').use_adapter?
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_use_adapter_with_adapter_option_as_false
|
def test_use_adapter_with_adapter_option_as_false
|
||||||
refute SerializableResource.new(@resource, { adapter: false }).use_adapter?
|
refute SerializableResource.new(@resource, adapter: false).use_adapter?
|
||||||
end
|
end
|
||||||
|
|
||||||
class SerializableResourceErrorsTest < Minitest::Test
|
class SerializableResourceErrorsTest < Minitest::Test
|
||||||
@ -45,14 +45,12 @@ module ActiveModelSerializers
|
|||||||
resource = ModelWithErrors.new
|
resource = ModelWithErrors.new
|
||||||
resource.errors.add(:name, 'must be awesome')
|
resource.errors.add(:name, 'must be awesome')
|
||||||
serializable_resource = ActiveModelSerializers::SerializableResource.new(
|
serializable_resource = ActiveModelSerializers::SerializableResource.new(
|
||||||
resource, {
|
resource, serializer: ActiveModel::Serializer::ErrorSerializer,
|
||||||
serializer: ActiveModel::Serializer::ErrorSerializer,
|
|
||||||
adapter: :json_api
|
adapter: :json_api
|
||||||
}
|
|
||||||
)
|
)
|
||||||
expected_response_document = {
|
expected_response_document = {
|
||||||
:errors => [
|
errors: [
|
||||||
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'must be awesome' }
|
{ source: { pointer: '/data/attributes/name' }, detail: 'must be awesome' }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
assert_equal serializable_resource.as_json(options), expected_response_document
|
assert_equal serializable_resource.as_json(options), expected_response_document
|
||||||
@ -65,15 +63,13 @@ module ActiveModelSerializers
|
|||||||
resource.errors.add(:title, 'must be amazing')
|
resource.errors.add(:title, 'must be amazing')
|
||||||
resources << ModelWithErrors.new
|
resources << ModelWithErrors.new
|
||||||
serializable_resource = SerializableResource.new(
|
serializable_resource = SerializableResource.new(
|
||||||
resources, {
|
resources, serializer: ActiveModel::Serializer::ErrorsSerializer,
|
||||||
serializer: ActiveModel::Serializer::ErrorsSerializer,
|
|
||||||
each_serializer: ActiveModel::Serializer::ErrorSerializer,
|
each_serializer: ActiveModel::Serializer::ErrorSerializer,
|
||||||
adapter: :json_api
|
adapter: :json_api
|
||||||
}
|
|
||||||
)
|
)
|
||||||
expected_response_document = {
|
expected_response_document = {
|
||||||
:errors => [
|
errors: [
|
||||||
{ :source => { :pointer => '/data/attributes/title' }, :detail => 'must be amazing' }
|
{ source: { pointer: '/data/attributes/title' }, detail: 'must be amazing' }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
assert_equal serializable_resource.as_json(options), expected_response_document
|
assert_equal serializable_resource.as_json(options), expected_response_document
|
||||||
|
|||||||
@ -7,10 +7,10 @@ module ActiveModel
|
|||||||
@author = Author.new(name: 'Steve K.')
|
@author = Author.new(name: 'Steve K.')
|
||||||
@author.bio = nil
|
@author.bio = nil
|
||||||
@author.roles = []
|
@author.roles = []
|
||||||
@blog = Blog.new({ name: 'AMS Blog' })
|
@blog = Blog.new(name: 'AMS Blog')
|
||||||
@post = Post.new({ title: 'New Post', body: 'Body' })
|
@post = Post.new(title: 'New Post', body: 'Body')
|
||||||
@tag = Tag.new({ name: '#hashtagged' })
|
@tag = Tag.new(name: '#hashtagged')
|
||||||
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||||
@post.comments = [@comment]
|
@post.comments = [@comment]
|
||||||
@post.tags = [@tag]
|
@post.tags = [@tag]
|
||||||
@post.blog = @blog
|
@post.blog = @blog
|
||||||
@ -19,7 +19,7 @@ module ActiveModel
|
|||||||
@post.author = @author
|
@post.author = @author
|
||||||
@author.posts = [@post]
|
@author.posts = [@post]
|
||||||
|
|
||||||
@post_serializer = PostSerializer.new(@post, { custom_options: true })
|
@post_serializer = PostSerializer.new(@post, custom_options: true)
|
||||||
@author_serializer = AuthorSerializer.new(@author)
|
@author_serializer = AuthorSerializer.new(@author)
|
||||||
@comment_serializer = CommentSerializer.new(@comment)
|
@comment_serializer = CommentSerializer.new(@comment)
|
||||||
end
|
end
|
||||||
@ -143,12 +143,12 @@ module ActiveModel
|
|||||||
)
|
)
|
||||||
actual = serializable(post, adapter: :attributes, serializer: InlineAssociationTestPostSerializer).as_json
|
actual = serializable(post, adapter: :attributes, serializer: InlineAssociationTestPostSerializer).as_json
|
||||||
expected = {
|
expected = {
|
||||||
:comments => [
|
comments: [
|
||||||
{ :id => 1, :contents => 'first comment' },
|
{ id: 1, contents: 'first comment' },
|
||||||
{ :id => 2, :contents => 'last comment' }
|
{ id: 2, contents: 'last comment' }
|
||||||
],
|
],
|
||||||
:last_comments => [
|
last_comments: [
|
||||||
{ :id => 2, :contents => 'last comment' }
|
{ id: 2, contents: 'last comment' }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -22,7 +22,7 @@ module ActiveModel
|
|||||||
inherited_klass = Class.new(AlternateBlogSerializer)
|
inherited_klass = Class.new(AlternateBlogSerializer)
|
||||||
blog_serializer = inherited_klass.new(@blog)
|
blog_serializer = inherited_klass.new(@blog)
|
||||||
adapter = ActiveModelSerializers::Adapter::Attributes.new(blog_serializer)
|
adapter = ActiveModelSerializers::Adapter::Attributes.new(blog_serializer)
|
||||||
assert_equal({ :id => 1, :title => 'AMS Hints' }, adapter.serializable_hash)
|
assert_equal({ id: 1, title: 'AMS Hints' }, adapter.serializable_hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_multiple_calls_with_the_same_attribute
|
def test_multiple_calls_with_the_same_attribute
|
||||||
|
|||||||
@ -4,7 +4,7 @@ module ActiveModel
|
|||||||
class Serializer
|
class Serializer
|
||||||
class AttributesTest < ActiveSupport::TestCase
|
class AttributesTest < ActiveSupport::TestCase
|
||||||
def setup
|
def setup
|
||||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||||
@profile_serializer = ProfileSerializer.new(@profile)
|
@profile_serializer = ProfileSerializer.new(@profile)
|
||||||
@comment = Comment.new(id: 1, body: 'ZOMG!!', date: '2015')
|
@comment = Comment.new(id: 1, body: 'ZOMG!!', date: '2015')
|
||||||
@serializer_klass = Class.new(CommentSerializer)
|
@serializer_klass = Class.new(CommentSerializer)
|
||||||
|
|||||||
@ -5,7 +5,7 @@ module ActiveModel
|
|||||||
class FieldsetTest < ActiveSupport::TestCase
|
class FieldsetTest < ActiveSupport::TestCase
|
||||||
def test_fieldset_with_hash
|
def test_fieldset_with_hash
|
||||||
fieldset = ActiveModel::Serializer::Fieldset.new('post' => %w(id title), 'comment' => ['body'])
|
fieldset = ActiveModel::Serializer::Fieldset.new('post' => %w(id title), 'comment' => ['body'])
|
||||||
expected = { :post => [:id, :title], :comment => [:body] }
|
expected = { post: [:id, :title], comment: [:body] }
|
||||||
|
|
||||||
assert_equal(expected, fieldset.fields)
|
assert_equal(expected, fieldset.fields)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -8,7 +8,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_overwrite_root
|
def test_overwrite_root
|
||||||
serializer = VirtualValueSerializer.new(@virtual_value, { root: 'smth' })
|
serializer = VirtualValueSerializer.new(@virtual_value, root: 'smth')
|
||||||
assert_equal('smth', serializer.json_key)
|
assert_equal('smth', serializer.json_key)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -41,7 +41,9 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
class CustomProfile
|
class CustomProfile
|
||||||
def serializer_class; ProfileSerializer; end
|
def serializer_class
|
||||||
|
ProfileSerializer
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Tweet = Class.new(::Model)
|
Tweet = Class.new(::Model)
|
||||||
|
|||||||
@ -75,6 +75,8 @@ module TestHelpers
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class ActiveSupport::TestCase
|
module ActiveSupport
|
||||||
|
class TestCase
|
||||||
include TestHelpers::Generation
|
include TestHelpers::Generation
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -44,10 +44,12 @@ module SerializationTesting
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Minitest::Test
|
module Minitest
|
||||||
|
class Test
|
||||||
def before_setup
|
def before_setup
|
||||||
ActionController::Base.cache_store.clear
|
ActionController::Base.cache_store.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
include SerializationTesting
|
include SerializationTesting
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user