mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Reorder methods
This commit is contained in:
parent
0b648fceac
commit
2dd0090f13
@ -8,9 +8,8 @@ module ActiveModel
|
|||||||
@serializer_options = serializer_options
|
@serializer_options = serializer_options
|
||||||
end
|
end
|
||||||
|
|
||||||
def target_serializer
|
def name
|
||||||
serializer = options[:serializer]
|
options[:name] || @name
|
||||||
serializer.is_a?(String) ? serializer.constantize : serializer
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def key
|
def key
|
||||||
@ -21,14 +20,6 @@ module ActiveModel
|
|||||||
options[:root] || @name
|
options[:root] || @name
|
||||||
end
|
end
|
||||||
|
|
||||||
def name
|
|
||||||
options[:name] || @name
|
|
||||||
end
|
|
||||||
|
|
||||||
def object
|
|
||||||
options[:value]
|
|
||||||
end
|
|
||||||
|
|
||||||
def embed_ids?
|
def embed_ids?
|
||||||
[:id, :ids].include? options[:embed]
|
[:id, :ids].include? options[:embed]
|
||||||
end
|
end
|
||||||
@ -45,6 +36,12 @@ module ActiveModel
|
|||||||
!object.nil?
|
!object.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def object
|
||||||
|
options[:value]
|
||||||
|
end
|
||||||
|
|
||||||
def embed_key
|
def embed_key
|
||||||
if key = options[:embed_key]
|
if key = options[:embed_key]
|
||||||
key
|
key
|
||||||
@ -53,7 +50,10 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
def target_serializer
|
||||||
|
serializer = options[:serializer]
|
||||||
|
serializer.is_a?(String) ? serializer.constantize : serializer
|
||||||
|
end
|
||||||
|
|
||||||
def find_serializable(object)
|
def find_serializable(object)
|
||||||
if target_serializer
|
if target_serializer
|
||||||
@ -83,18 +83,18 @@ module ActiveModel
|
|||||||
"#{@name.to_s.singularize}_ids".to_sym
|
"#{@name.to_s.singularize}_ids".to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize
|
|
||||||
object.map do |item|
|
|
||||||
find_serializable(item).serializable_hash
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def serializables
|
def serializables
|
||||||
object.map do |item|
|
object.map do |item|
|
||||||
find_serializable(item)
|
find_serializable(item)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def serialize
|
||||||
|
object.map do |item|
|
||||||
|
find_serializable(item).serializable_hash
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def serialize_ids
|
def serialize_ids
|
||||||
object.map do |item|
|
object.map do |item|
|
||||||
item.read_attribute_for_serialization(embed_key)
|
item.read_attribute_for_serialization(embed_key)
|
||||||
@ -103,18 +103,16 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
class HasOne < Base #:nodoc:
|
class HasOne < Base #:nodoc:
|
||||||
def embeddable?
|
def key
|
||||||
if polymorphic? && object.nil?
|
if key = options[:key]
|
||||||
false
|
key
|
||||||
|
elsif embed_ids? && !polymorphic?
|
||||||
|
id_key
|
||||||
else
|
else
|
||||||
true
|
@name
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def polymorphic?
|
|
||||||
options[:polymorphic]
|
|
||||||
end
|
|
||||||
|
|
||||||
def root
|
def root
|
||||||
if root = options[:root]
|
if root = options[:root]
|
||||||
root
|
root
|
||||||
@ -125,22 +123,21 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def key
|
|
||||||
if key = options[:key]
|
|
||||||
key
|
|
||||||
elsif embed_ids? && !polymorphic?
|
|
||||||
id_key
|
|
||||||
else
|
|
||||||
@name
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def id_key
|
def id_key
|
||||||
"#{@name}_id".to_sym
|
"#{@name}_id".to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
def polymorphic_key
|
def embeddable?
|
||||||
object.class.to_s.demodulize.underscore.to_sym
|
if polymorphic? && object.nil?
|
||||||
|
false
|
||||||
|
else
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def serializables
|
||||||
|
value = object && find_serializable(object)
|
||||||
|
value ? [value] : []
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize
|
def serialize
|
||||||
@ -156,11 +153,6 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializables
|
|
||||||
value = object && find_serializable(object)
|
|
||||||
value ? [value] : []
|
|
||||||
end
|
|
||||||
|
|
||||||
def serialize_ids
|
def serialize_ids
|
||||||
if object
|
if object
|
||||||
id = object.read_attribute_for_serialization(embed_key)
|
id = object.read_attribute_for_serialization(embed_key)
|
||||||
@ -174,6 +166,16 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def polymorphic?
|
||||||
|
options[:polymorphic]
|
||||||
|
end
|
||||||
|
|
||||||
|
def polymorphic_key
|
||||||
|
object.class.to_s.demodulize.underscore.to_sym
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user