Extract Association login into AssociationConfiguration

This commit is contained in:
Adrian Mugnolo and Santiago Pastorino 2014-01-17 13:15:35 -02:00 committed by Santiago Pastorino
parent 203887e613
commit ca1e1b4b55
3 changed files with 52 additions and 31 deletions

View File

@ -68,9 +68,9 @@ module ActiveModel
attr_accessor :object, :configuration attr_accessor :object, :configuration
def initialize(object, options = {}) def initialize(object, options = {}, configuration = nil)
@object = object @object = object
@configuration = InstanceConfiguration.new self.class.configuration, options @configuration = InstanceConfiguration.new(configuration || self.class.configuration, options)
end end
def json_key def json_key
@ -92,9 +92,9 @@ module ActiveModel
included_associations = filter(associations.keys) included_associations = filter(associations.keys)
associations.each_with_object({}) do |(name, association), hash| associations.each_with_object({}) do |(name, association), hash|
if included_associations.include? name if included_associations.include? name
if association.embed_ids? if association.embed_ids
hash[association.key] = serialize_ids association hash[association.key] = serialize_ids association
elsif association.embed_objects? elsif association.embed_objects
hash[association.embedded_key] = serialize association hash[association.embedded_key] = serialize association
end end
end end
@ -110,7 +110,7 @@ module ActiveModel
included_associations = filter(associations.keys) included_associations = filter(associations.keys)
associations.each_with_object({}) do |(name, association), hash| associations.each_with_object({}) do |(name, association), hash|
if included_associations.include? name if included_associations.include? name
if association.embed_in_root? if association.embed_in_root
association_serializer = build_serializer(association) association_serializer = build_serializer(association)
hash.merge! association_serializer.embedded_in_root_associations hash.merge! association_serializer.embedded_in_root_associations

View File

@ -4,6 +4,8 @@ require 'active_model/serializer'
module ActiveModel module ActiveModel
class Serializer class Serializer
class Association class Association
attr_accessor :name, :configuration
def initialize(name, options = {}, configuration = nil) def initialize(name, options = {}, configuration = nil)
if options.has_key?(:include) if options.has_key?(:include)
ActiveSupport::Deprecation.warn <<-WARN ActiveSupport::Deprecation.warn <<-WARN
@ -13,28 +15,25 @@ module ActiveModel
end end
@name = name.to_s @name = name.to_s
@options = options @configuration = AssociationConfiguration.new configuration, options
self.embed = options.fetch(:embed) { configuration.embed }
@embed_in_root = options.fetch(:embed_in_root) { configuration.embed_in_root }
@embed_key = options[:embed_key] || :id
@key = options[:key]
@embedded_key = options[:root] || name
serializer = @options[:serializer]
@serializer_from_options = serializer.is_a?(String) ? serializer.constantize : serializer
end end
attr_reader :name, :embed_ids, :embed_objects extend Forwardable
attr_accessor :embed_in_root, :embed_key, :key, :embedded_key, :root_key, :serializer_from_options, :options def_delegators :configuration, :embed_ids, :embed_objects, :embed, :embed_in_root, :embed_key, :key, :root, :serializer
alias embed_ids? embed_ids
alias embed_objects? embed_objects def embedded_key
alias embed_in_root? embed_in_root root || name
end
def embed=(embed) def embed=(embed)
@embed_ids = embed == :id || embed == :ids @embed_ids = embed == :id || embed == :ids
@embed_objects = embed == :object || embed == :objects @embed_objects = embed == :object || embed == :objects
end end
def serializer_from_options
configuration.serializer
end
def serializer_from_object(object) def serializer_from_object(object)
Serializer.serializer_for(object) Serializer.serializer_for(object)
end end
@ -44,14 +43,16 @@ module ActiveModel
end end
def build_serializer(object, options = {}) def build_serializer(object, options = {})
serializer_class(object).new(object, options.merge(self.options)) serializer_class(object).new(object, options, configuration)
end end
class HasOne < Association class HasOne < Association
def initialize(name, *args) def root_key
super embedded_key.to_s.pluralize
@root_key = @embedded_key.to_s.pluralize end
@key ||= "#{name}_id"
def key
"#{name}_id"
end end
def serializer_class(object) def serializer_class(object)
@ -60,16 +61,16 @@ module ActiveModel
def build_serializer(object, options = {}) def build_serializer(object, options = {})
super.tap do |instance| super.tap do |instance|
instance.configuration.wrap_in_array = embed_in_root? instance.configuration.wrap_in_array = embed_in_root
end end
end end
end end
class HasMany < Association class HasMany < Association
def initialize(name, *args) alias_method :root_key, :embedded_key
super
@root_key = @embedded_key def key
@key ||= "#{name.to_s.singularize}_ids" "#{name.to_s.singularize}_ids"
end end
def serializer_class(object) def serializer_class(object)
@ -80,9 +81,9 @@ module ActiveModel
end end
end end
def options def build_serializer(object, options = {})
if use_array_serializer? if use_array_serializer?
{ each_serializer: serializer_from_options }.merge! super super object, { each_serializer: serializer_from_options }.merge!(options), configuration
else else
super super
end end

View File

@ -115,5 +115,25 @@ module ActiveModel
{ embed: :objects, meta_key: :meta } { embed: :objects, meta_key: :meta }
end end
end end
class AssociationConfiguration < Configuration
options :embed, :embed_in_root, :embed_key, :key, :root, :serializer
def default_options
{ embed_key: :id }
end
def serializer=(value)
@serializer = value.is_a?(String) ? value.constantize : value
end
def embed_objects
[:objects, :object].include? embed
end
def embed_ids
[:ids, :id].include? embed
end
end
end end
end end