-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update active_model_serializers 0.9.7 to 0.10.10 #8154
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module Export | ||
class AspectSerializer < ActiveModel::Serializer | ||
class AspectSerializer < AMS::V09::Serializer | ||
attributes :name | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
module AMS | ||
module V09 | ||
class Serializer < ActiveModel::Serializer | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body beginning. |
||
def serializable_hash(adapter_options = nil, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
options = {}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceAroundEqualsInParameterDefault: Surrounding space detected in default value assignment. |
||
adapter_instance = self.class.serialization_adapter_instance) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceAroundEqualsInParameterDefault: Surrounding space detected in default value assignment. |
||
object.nil? ? nil : super | ||
end | ||
end | ||
|
||
class ArraySerializer < ActiveModel::Serializer::CollectionSerializer | ||
end | ||
|
||
class ResourceIdentifier < ActiveModelSerializers::Adapter::JsonApi::ResourceIdentifier | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body beginning. |
||
def initialize(serializer, options) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lint/UnusedMethodArgument: Unused method argument - |
||
@id = id_for(serializer) | ||
@type = type_for(serializer) | ||
end | ||
|
||
private | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/AccessModifierIndentation: Indent access modifiers like |
||
|
||
def id_for(serializer) | ||
serializer.read_attribute_for_serialization(:id) | ||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body end. |
||
end | ||
|
||
class Base < ActiveModelSerializers::Adapter::Base | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body beginning. |
||
def initialize(serializer, options = {}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceAroundEqualsInParameterDefault: Surrounding space detected in default value assignment. |
||
super | ||
@fieldset = options[:fieldset] || ActiveModel::Serializer::Fieldset.new(options.delete(:fields)) | ||
end | ||
|
||
private | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/AccessModifierIndentation: Indent access modifiers like |
||
|
||
attr_reader :fieldset | ||
|
||
def attributes_for(serializer, fields) | ||
serializer.attributes(fields).except(:id) | ||
end | ||
|
||
def resource_object_for(serializer) | ||
resource_object = serializer.fetch(self) do | ||
resource_object = ResourceIdentifier.new(serializer, instance_options).as_json | ||
|
||
requested_fields = fieldset && fieldset.fields_for(resource_object[:type]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/SafeNavigation: Use safe navigation ( |
||
attributes = attributes_for(serializer, requested_fields) | ||
resource_object.merge!(attributes) if attributes.any? | ||
resource_object | ||
end | ||
|
||
resource_object | ||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body end. |
||
end | ||
|
||
# represent resource without associations | ||
class Attributes < Base | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body beginning. |
||
def serializable_hash(options = nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return nil if serializer.nil? || serializer.object.nil? | ||
|
||
is_collection = serializer.respond_to?(:each) | ||
serializers = is_collection ? serializer : [serializer] | ||
data = resource_objects_for(serializers) | ||
|
||
result = is_collection ? data : data[0] | ||
|
||
self.class.transform_key_casing!(result, instance_options) | ||
end | ||
|
||
private | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/AccessModifierIndentation: Indent access modifiers like |
||
|
||
def resource_objects_for(serializers) | ||
serializers.map { |serializer| resource_object_for(serializer) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceInsideBlockBraces: Space between { and | detected. |
||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body end. |
||
end | ||
|
||
class AttributesWithIncluded < Base | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body beginning. |
||
def serializable_hash(options = nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return {} if serializer.object.nil? | ||
|
||
is_collection = serializer.respond_to?(:each) | ||
serializers = is_collection ? serializer : [serializer] | ||
primary_data, included = resource_objects_for(serializers) | ||
|
||
hash = {} | ||
hash[root] = is_collection ? primary_data : primary_data[0] | ||
hash[:included] = included if included.any? | ||
hash[meta_key] = meta unless meta.blank? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rails/Present: Use |
||
|
||
self.class.transform_key_casing!(hash, instance_options) | ||
end | ||
|
||
private | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/AccessModifierIndentation: Indent access modifiers like |
||
|
||
def resource_objects_for(serializers) | ||
@primary = [] | ||
@included = {} | ||
@resource_identifiers = Set.new | ||
serializers.each { |serializer| process_resource(serializer) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceInsideBlockBraces: Space between { and | detected. |
||
serializers.each { |serializer| process_relationships(serializer) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceInsideBlockBraces: Space between { and | detected. |
||
|
||
[@primary, @included] | ||
end | ||
|
||
def process_resource(serializer) | ||
return false unless resource_already_processed?(serializer) | ||
|
||
@primary << resource_object_for(serializer) | ||
true | ||
end | ||
|
||
def process_relationship_resource(serializer, json_key) | ||
return false unless resource_already_processed?(serializer) | ||
|
||
@included[json_key] << resource_object_for(serializer) | ||
true | ||
end | ||
|
||
def process_relationships(serializer) | ||
serializer.associations.each do |association| | ||
process_relationship(association.serializer, association.key) | ||
end | ||
end | ||
|
||
def process_relationship(serializer, key) | ||
json_key = key || serializer.json_key.to_s.pluralize | ||
@included[json_key] ||= [] | ||
|
||
if serializer.respond_to?(:each) | ||
serializer.each { |s| process_relationship(s, key) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceInsideBlockBraces: Space between { and | detected. |
||
return | ||
end | ||
return unless serializer && serializer.object | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/SafeNavigation: Use safe navigation ( |
||
return unless process_relationship_resource(serializer, json_key) | ||
|
||
process_relationships(serializer) | ||
end | ||
|
||
def resource_already_processed?(serializer) | ||
resource_identifier = ResourceIdentifier.new(serializer, instance_options).as_json | ||
@resource_identifiers.add?(resource_identifier) | ||
end | ||
|
||
def meta | ||
instance_options.fetch(:meta, nil) | ||
end | ||
|
||
def meta_key | ||
instance_options.fetch(:meta_key, 'meta'.freeze) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body end. |
||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/EmptyLinesAroundModuleBody: Extra empty line detected at module body end. |
||
end | ||
end | ||
|
||
ActiveModelSerializers::Adapter.register :v09_attributes_with_included, AMS::V09::AttributesWithIncluded | ||
ActiveModelSerializers::Adapter.register :v09_attributes, AMS::V09::Attributes | ||
ActiveModelSerializers.config.adapter = :v09_attributes_with_included |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,7 +72,7 @@ def serializer_class | |
def pick_serializer_class | ||
return association_serializer_class unless association_serializer_class.nil? | ||
return @serializer_from_options unless @serializer_from_options.nil? | ||
return ActiveModel::ArraySerializer unless each_serializer_class.nil? | ||
return ActiveModelSerializers::SerializableResource unless each_serializer_class.nil? | ||
end | ||
end | ||
|
||
|
@@ -82,6 +82,6 @@ def pick_serializer_class | |
# NOTE: this matcher uses knowledge of AMS internals | ||
RSpec::Matchers.define :serialize_each_with do |expected| | ||
match do |actual| | ||
actual.is_a?(ActiveModel::ArraySerializer) && actual.instance_variable_get("@each_serializer") == expected | ||
actual.is_a?(ActiveModelSerializers::SerializableResource) && actual.instance_variable_get("@each_serializer") == expected | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/LineLength: Line is too long. [126/120] |
||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/FrozenStringLiteralComment: Missing frozen string literal comment.