mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Add filter to allow users implement filter method to include/exclude attributes and relations
This commit is contained in:
@@ -10,7 +10,7 @@ module ActiveModel
|
||||
class << self
|
||||
def inherited(base)
|
||||
base._attributes = []
|
||||
base._associations = []
|
||||
base._associations = {}
|
||||
end
|
||||
|
||||
def setup
|
||||
@@ -70,7 +70,7 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
@_associations << klass.new(attr, options)
|
||||
@_associations[attr] = klass.new(attr, options)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -94,29 +94,41 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def attributes
|
||||
self.class._attributes.each_with_object({}) do |name, hash|
|
||||
filter(self.class._attributes.dup).each_with_object({}) do |name, hash|
|
||||
hash[name] = send(name)
|
||||
end
|
||||
end
|
||||
|
||||
def associations
|
||||
self.class._associations.each_with_object({}) do |association, hash|
|
||||
if association.embed_ids?
|
||||
hash[association.key] = serialize_ids association
|
||||
elsif association.embed_objects?
|
||||
hash[association.embedded_key] = serialize association
|
||||
associations = self.class._associations
|
||||
included_associations = filter(associations.keys)
|
||||
associations.each_with_object({}) do |(name, association), hash|
|
||||
if included_associations.include? name
|
||||
if association.embed_ids?
|
||||
hash[association.key] = serialize_ids association
|
||||
elsif association.embed_objects?
|
||||
hash[association.embedded_key] = serialize association
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def filter(keys)
|
||||
keys
|
||||
end
|
||||
|
||||
def serializable_data
|
||||
embedded_in_root_associations.merge!(super)
|
||||
end
|
||||
|
||||
def embedded_in_root_associations
|
||||
self.class._associations.each_with_object({}) do |association, hash|
|
||||
if association.embed_in_root?
|
||||
hash[association.embedded_key] = serialize association
|
||||
associations = self.class._associations
|
||||
included_associations = filter(associations.keys)
|
||||
associations.each_with_object({}) do |(name, association), hash|
|
||||
if included_associations.include? name
|
||||
if association.embed_in_root?
|
||||
hash[association.embedded_key] = serialize association
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user