mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Merge pull request #1426 from brigade/default-include
Add a default_include_tree config variable to ActiveModel::Serializer
This commit is contained in:
@@ -10,8 +10,6 @@ module ActiveModel
|
||||
module Associations
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
DEFAULT_INCLUDE_TREE = ActiveModel::Serializer::IncludeTree.from_string('*')
|
||||
|
||||
included do
|
||||
with_options instance_writer: false, instance_reader: true do |serializer|
|
||||
serializer.class_attribute :_reflections
|
||||
@@ -80,10 +78,11 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
# @param [IncludeTree] include_tree (defaults to all associations when not provided)
|
||||
# @param [IncludeTree] include_tree (defaults to the
|
||||
# default_includes config value when not provided)
|
||||
# @return [Enumerator<Association>]
|
||||
#
|
||||
def associations(include_tree = DEFAULT_INCLUDE_TREE)
|
||||
def associations(include_tree = ActiveModelSerializers.default_include_tree)
|
||||
return unless object
|
||||
|
||||
Enumerator.new do |y|
|
||||
|
||||
@@ -19,6 +19,7 @@ module ActiveModel
|
||||
collection_serializer
|
||||
end
|
||||
|
||||
config.default_includes = '*'
|
||||
config.adapter = :attributes
|
||||
config.jsonapi_resource_type = :plural
|
||||
config.jsonapi_version = '1.0'
|
||||
|
||||
@@ -31,6 +31,13 @@ module ActiveModelSerializers
|
||||
[file, lineno]
|
||||
end
|
||||
|
||||
# Memoized default include tree
|
||||
# @return [ActiveModel::Serializer::IncludeTree]
|
||||
def self.default_include_tree
|
||||
@default_include_tree ||= ActiveModel::Serializer::IncludeTree
|
||||
.from_include_args(config.default_includes)
|
||||
end
|
||||
|
||||
require 'active_model/serializer/version'
|
||||
require 'active_model/serializer'
|
||||
require 'active_model/serializable_resource'
|
||||
|
||||
@@ -3,8 +3,13 @@ module ActiveModelSerializers
|
||||
class Attributes < Base
|
||||
def initialize(serializer, options = {})
|
||||
super
|
||||
@include_tree = ActiveModel::Serializer::IncludeTree.from_include_args(options[:include] || '*')
|
||||
@cached_attributes = options[:cache_attributes] || {}
|
||||
@include_tree =
|
||||
if options[:include]
|
||||
ActiveModel::Serializer::IncludeTree.from_include_args(options[:include])
|
||||
else
|
||||
ActiveModelSerializers.default_include_tree
|
||||
end
|
||||
end
|
||||
|
||||
def serializable_hash(options = nil)
|
||||
|
||||
Reference in New Issue
Block a user