Add filter to allow users implement filter method to include/exclude attributes and relations

This commit is contained in:
Santiago Pastorino
2013-09-16 11:41:56 -03:00
parent f6ea07dd22
commit 841f3b8181
4 changed files with 77 additions and 16 deletions

View File

@@ -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