aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/serializer.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activemodel/lib/active_model/serializer.rb')
-rw-r--r--activemodel/lib/active_model/serializer.rb30
1 files changed, 29 insertions, 1 deletions
diff --git a/activemodel/lib/active_model/serializer.rb b/activemodel/lib/active_model/serializer.rb
index 6e89eec09c..99a007de31 100644
--- a/activemodel/lib/active_model/serializer.rb
+++ b/activemodel/lib/active_model/serializer.rb
@@ -18,12 +18,25 @@ module ActiveModel
serializer.new(item, scope).serializable_hash
end
end
+
+ def serialize_ids(collection, scope)
+ # use named scopes if they are present
+ return collection.ids if collection.respond_to?(:ids)
+
+ collection.map do |item|
+ item.read_attribute_for_serialization(:id)
+ end
+ end
end
class HasOne < Config
def serialize(object, scope)
serializer.new(object, scope).serializable_hash
end
+
+ def serialize_ids(object, scope)
+ object.read_attribute_for_serialization(:id)
+ end
end
end
@@ -80,7 +93,11 @@ module ActiveModel
end
def serializable_hash
- hash = attributes
+ attributes.merge(associations)
+ end
+
+ def associations
+ hash = {}
_associations.each do |association|
associated_object = send(association.name)
@@ -90,6 +107,17 @@ module ActiveModel
hash
end
+ def association_ids
+ hash = {}
+
+ _associations.each do |association|
+ associated_object = send(association.name)
+ hash[association.name] = association.serialize_ids(associated_object, scope)
+ end
+
+ hash
+ end
+
def attributes
hash = {}