active_model_serializers.rb 1.0 KB

123456789101112131415161718192021222324
  1. ActiveModelSerializers.config.tap do |config|
  2. config.default_includes = '**'
  3. end
  4. ActiveSupport::Notifications.unsubscribe(ActiveModelSerializers::Logging::RENDER_EVENT)
  5. class ActiveModel::Serializer::Reflection
  6. # We monkey-patch this method so that when we include associations in a serializer,
  7. # the nested serializers can send information about used contexts upwards back to
  8. # the root. We do this via instance_options because the nesting can be dynamic.
  9. def build_association(parent_serializer, parent_serializer_options, include_slice = {})
  10. serializer = options[:serializer]
  11. parent_serializer_options.merge!(named_contexts: serializer._named_contexts, context_extensions: serializer._context_extensions) if serializer.respond_to?(:_named_contexts)
  12. association_options = {
  13. parent_serializer: parent_serializer,
  14. parent_serializer_options: parent_serializer_options,
  15. include_slice: include_slice,
  16. }
  17. ActiveModel::Serializer::Association.new(self, association_options)
  18. end
  19. end