Sha256: 10bc9bc91051778474468243f42d8211b34ecf857818cfdbf0b77b7a9cd2750c

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

module Skylight
  module Probes
    module ActiveModelSerializers
      module Instrumentation
        def as_json(*)
          payload = { serializer: self.class }
          ActiveSupport::Notifications.instrument("render.active_model_serializers", payload) { super }
        end
      end

      class Probe
        def install
          version = nil

          # File moved location between version
          %w[serializer serializers].each do |dir|
            # rubocop:disable Lint/SuppressedException
            begin
              require "active_model/#{dir}/version"
            rescue LoadError
            end
            # rubocop:enable Lint/SuppressedException
          end

          if Gem.loaded_specs["active_model_serializers"]
            version = Gem.loaded_specs["active_model_serializers"].version
          end

          if !version || version < Gem::Version.new("0.5.0")
            Skylight.error "Instrumention is only available for ActiveModelSerializers version 0.5.0 and greater."
            return
          end

          # We don't actually support the RCs correctly, requires
          # a release after 0.10.0.rc3
          if version >= Gem::Version.new("0.10.0.rc1")
            # AS::N is built in to newer versions
            return
          end

          # End users could override as_json without calling super, but it's likely safer
          # than overriding serializable_array/hash/object.

          [::ActiveModel::Serializer, ::ActiveModel::ArraySerializer].each do |klass|
            klass.prepend(Instrumentation)
          end
        end
      end
    end

    register(:active_model_serializers, "ActiveModel::Serializer", "active_model/serializer",
             ActiveModelSerializers::Probe.new)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
skylight-5.0.0.beta lib/skylight/probes/active_model_serializers.rb