aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/serializer.rb
diff options
context:
space:
mode:
authorJose and Yehuda <wycats@gmail.com>2011-10-15 18:27:56 +0200
committerJose and Yehuda <wycats@gmail.com>2011-10-15 18:40:38 +0200
commit322f47898e80af3fcdc3cb3db35e177d8216a2d2 (patch)
treee77463224a32635da6e48c24dd8c1ae45b167086 /activemodel/lib/active_model/serializer.rb
parent22c322f056f42d95b0421e6608f404134463de13 (diff)
downloadrails-322f47898e80af3fcdc3cb3db35e177d8216a2d2.tar.gz
rails-322f47898e80af3fcdc3cb3db35e177d8216a2d2.tar.bz2
rails-322f47898e80af3fcdc3cb3db35e177d8216a2d2.zip
Add association_ids
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 = {}