Class: Skylight::Core::Probes::ActiveModelSerializers::Probe Private

Inherits:
Object
  • Object
show all
Defined in:
lib/skylight/core/probes/active_model_serializers.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#installObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/skylight/core/probes/active_model_serializers.rb', line 5

def install
  version = nil

  # File moved location between version
  %w[serializer serializers].each do |dir|
    # rubocop:disable Lint/HandleExceptions
    begin
      require "active_model/#{dir}/version"
    rescue LoadError
    end
    # rubocop:enable Lint/HandleExceptions
  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")
    # Using $stderr here isn't great, but we don't have a logger accessible
    $stderr.puts "[SKYLIGHT::CORE] [#{Skylight::Core::VERSION}] 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.class_eval do
      alias_method :as_json_without_sk, :as_json
      def as_json(*args)
        payload = { serializer: self.class }
        ActiveSupport::Notifications.instrument("render.active_model_serializers", payload) do
          as_json_without_sk(*args)
        end
      end
    end
  end
end