From 296970415a88476ed3a7db8e6c4306b9209a2bf7 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 16 May 2013 17:12:02 -0700 Subject: [PATCH] Move key method to the base class --- lib/active_model/serializer/associations.rb | 35 +++++++++------------ 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/lib/active_model/serializer/associations.rb b/lib/active_model/serializer/associations.rb index cacf7dbd..4ef04176 100644 --- a/lib/active_model/serializer/associations.rb +++ b/lib/active_model/serializer/associations.rb @@ -17,9 +17,20 @@ module ActiveModel attr_reader :root, :name, :embed_in_root alias :embed_in_root? :embed_in_root + def key + if key = options[:key] + key + elsif use_id_key? + id_key + else + @name + end + end + def embed_ids? embed == :id || embed == :ids end + alias use_id_key? embed_ids? def embed_objects? embed == :object || embed == :objects @@ -52,16 +63,6 @@ module ActiveModel end class HasMany < Base #:nodoc: - def key - if key = options[:key] - key - elsif embed_ids? - id_key - else - @name - end - end - def root options[:root] || name end @@ -90,16 +91,6 @@ module ActiveModel end class HasOne < Base #:nodoc: - def key - if key = options[:key] - key - elsif embed_ids? && !polymorphic? - id_key - else - @name - end - end - def root if root = options[:root] root @@ -156,6 +147,10 @@ module ActiveModel private + def use_id_key? + embed_ids? && !polymorphic? + end + def polymorphic? options[:polymorphic] end