has_one returns arrays with roots pluralized

This commit is contained in:
Santiago Pastorino
2013-10-30 15:17:26 -02:00
parent 1eea008336
commit 4d4b820cbe
4 changed files with 20 additions and 14 deletions

View File

@@ -164,7 +164,12 @@ end
association.serializer_class <= ArraySerializer)
associated_data.map { |elem| association.build_serializer(elem).serializable_hash }
else
association.build_serializer(associated_data).serializable_object
serializable_obj = association.build_serializer(associated_data).serializable_object
if !(association.serializer_class && association.serializer_class <= ArraySerializer)
serializable_obj = [serializable_obj]
serializable_obj.compact!
end
serializable_obj
end
end

View File

@@ -17,7 +17,6 @@ module ActiveModel
@embed_in_root = options.fetch(:embed_in_root) { options.fetch(:include) { CONFIG.embed_in_root } }
@embed_key = options[:embed_key] || :id
@key = options[:key]
@embedded_key = options[:root] || name
self.serializer_class = @options[:serializer]
end
@@ -43,16 +42,18 @@ module ActiveModel
end
class HasOne < Association
def initialize(*args)
def initialize(name, *args)
super
@key ||= "#{name}_id"
@embedded_key = "#{@options[:root] || name}".pluralize
@key ||= "#{name}_id"
end
end
class HasMany < Association
def initialize(*args)
def initialize(name, *args)
super
@key ||= "#{name.singularize}_ids"
@embedded_key = @options[:root] || name
@key ||= "#{name.to_s.singularize}_ids"
end
end
end