Separate associations into multiple files

This commit is contained in:
Pol Miro 2014-08-24 15:28:20 -07:00
parent bb18fc6225
commit e8b983490e
4 changed files with 61 additions and 49 deletions

View File

@ -1,6 +1,6 @@
require 'active_model/array_serializer'
require 'active_model/serializable'
require 'active_model/serializer/associations'
require 'active_model/serializer/association'
require 'active_model/serializer/config'
require 'thread'

View File

@ -1,4 +1,6 @@
require 'active_model/default_serializer'
require 'active_model/serializer/association/has_one'
require 'active_model/serializer/association/has_many'
module ActiveModel
class Serializer
@ -49,54 +51,6 @@ module ActiveModel
def build_serializer(object, options = {})
serializer_class(object).new(object, options.merge(self.options))
end
class HasOne < Association
def initialize(name, *args)
super
@root_key = @embedded_key.to_s.pluralize
@key ||= "#{name}_id"
end
def serializer_class(object)
serializer_from_options || serializer_from_object(object) || default_serializer
end
def build_serializer(object, options = {})
options[:_wrap_in_array] = embed_in_root?
super
end
end
class HasMany < Association
def initialize(name, *args)
super
@root_key = @embedded_key
@key ||= "#{name.to_s.singularize}_ids"
end
def serializer_class(object)
if use_array_serializer?
ArraySerializer
else
serializer_from_options
end
end
def options
if use_array_serializer?
{ each_serializer: serializer_from_options }.merge! super
else
super
end
end
private
def use_array_serializer?
!serializer_from_options ||
serializer_from_options && !(serializer_from_options <= ArraySerializer)
end
end
end
end
end

View File

@ -0,0 +1,36 @@
module ActiveModel
class Serializer
class Association
class HasMany < Association
def initialize(name, *args)
super
@root_key = @embedded_key
@key ||= "#{name.to_s.singularize}_ids"
end
def serializer_class(object)
if use_array_serializer?
ArraySerializer
else
serializer_from_options
end
end
def options
if use_array_serializer?
{ each_serializer: serializer_from_options }.merge! super
else
super
end
end
private
def use_array_serializer?
!serializer_from_options ||
serializer_from_options && !(serializer_from_options <= ArraySerializer)
end
end
end
end
end

View File

@ -0,0 +1,22 @@
module ActiveModel
class Serializer
class Association
class HasOne < Association
def initialize(name, *args)
super
@root_key = @embedded_key.to_s.pluralize
@key ||= "#{name}_id"
end
def serializer_class(object)
serializer_from_options || serializer_from_object(object) || default_serializer
end
def build_serializer(object, options = {})
options[:_wrap_in_array] = embed_in_root?
super
end
end
end
end
end