mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Add inline syntax for defining associations
Adapted from https://github.com/rails-api/active_model_serializers/pull/1262
This commit is contained in:
parent
6020450fe4
commit
7cbef1b3b5
@ -69,7 +69,7 @@ module ActiveModel
|
|||||||
serializer.class_attribute :_cache_digest # @api private : Generated
|
serializer.class_attribute :_cache_digest # @api private : Generated
|
||||||
end
|
end
|
||||||
|
|
||||||
# Serializers inherit serialized_attributes and _attributes_keys.
|
# Serializers inherit serialized_attributes, _attributes_keys, and _reflections.
|
||||||
# Generates a unique digest for each serializer at load.
|
# Generates a unique digest for each serializer at load.
|
||||||
def self.inherited(base)
|
def self.inherited(base)
|
||||||
caller_line = caller.first
|
caller_line = caller.first
|
||||||
|
|||||||
@ -13,9 +13,8 @@ module ActiveModel
|
|||||||
DEFAULT_INCLUDE_TREE = ActiveModel::Serializer::IncludeTree.from_string('*')
|
DEFAULT_INCLUDE_TREE = ActiveModel::Serializer::IncludeTree.from_string('*')
|
||||||
|
|
||||||
included do |base|
|
included do |base|
|
||||||
class << base
|
base.class_attribute :_reflections
|
||||||
attr_accessor :_reflections
|
base._reflections ||= []
|
||||||
end
|
|
||||||
|
|
||||||
extend ActiveSupport::Autoload
|
extend ActiveSupport::Autoload
|
||||||
autoload :Association
|
autoload :Association
|
||||||
@ -28,8 +27,10 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
|
# Serializers inherit _reflections.
|
||||||
def inherited(base)
|
def inherited(base)
|
||||||
base._reflections = self._reflections.try(:dup) || []
|
super
|
||||||
|
base._reflections = _reflections.dup
|
||||||
end
|
end
|
||||||
|
|
||||||
# @param [Symbol] name of the association
|
# @param [Symbol] name of the association
|
||||||
@ -39,8 +40,8 @@ module ActiveModel
|
|||||||
# @example
|
# @example
|
||||||
# has_many :comments, serializer: CommentSummarySerializer
|
# has_many :comments, serializer: CommentSummarySerializer
|
||||||
#
|
#
|
||||||
def has_many(name, options = {})
|
def has_many(name, options = {}, &block)
|
||||||
associate HasManyReflection.new(name, options)
|
associate(HasManyReflection.new(name, options), block)
|
||||||
end
|
end
|
||||||
|
|
||||||
# @param [Symbol] name of the association
|
# @param [Symbol] name of the association
|
||||||
@ -50,8 +51,8 @@ module ActiveModel
|
|||||||
# @example
|
# @example
|
||||||
# belongs_to :author, serializer: AuthorSerializer
|
# belongs_to :author, serializer: AuthorSerializer
|
||||||
#
|
#
|
||||||
def belongs_to(name, options = {})
|
def belongs_to(name, options = {}, &block)
|
||||||
associate BelongsToReflection.new(name, options)
|
associate(BelongsToReflection.new(name, options), block)
|
||||||
end
|
end
|
||||||
|
|
||||||
# @param [Symbol] name of the association
|
# @param [Symbol] name of the association
|
||||||
@ -61,8 +62,8 @@ module ActiveModel
|
|||||||
# @example
|
# @example
|
||||||
# has_one :author, serializer: AuthorSerializer
|
# has_one :author, serializer: AuthorSerializer
|
||||||
#
|
#
|
||||||
def has_one(name, options = {})
|
def has_one(name, options = {}, &block)
|
||||||
associate HasOneReflection.new(name, options)
|
associate(HasOneReflection.new(name, options), block)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
@ -73,11 +74,15 @@ module ActiveModel
|
|||||||
#
|
#
|
||||||
# @api private
|
# @api private
|
||||||
#
|
#
|
||||||
def associate(reflection)
|
def associate(reflection, block)
|
||||||
self._reflections = _reflections.dup
|
self._reflections = _reflections.dup
|
||||||
|
|
||||||
define_method reflection.name do
|
define_method reflection.name do
|
||||||
object.send reflection.name
|
if block_given?
|
||||||
|
instance_eval(&block)
|
||||||
|
else
|
||||||
|
object.send reflection.name
|
||||||
|
end
|
||||||
end unless method_defined?(reflection.name)
|
end unless method_defined?(reflection.name)
|
||||||
|
|
||||||
self._reflections << reflection
|
self._reflections << reflection
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user