extract the instrumentation key to be const and frozen string

This commit is contained in:
Yosi Attias 2017-03-24 15:16:01 +03:00
parent af410c54e6
commit 08fd8705f3
2 changed files with 7 additions and 7 deletions

View File

@ -15,7 +15,7 @@ module ActiveModel
end end
def as_json(options={}) def as_json(options={})
instrument('!serialize') do instrument do
return [] if @object.nil? && @wrap_in_array return [] if @object.nil? && @wrap_in_array
hash = @object.as_json hash = @object.as_json
@wrap_in_array ? [hash] : hash @wrap_in_array ? [hash] : hash

View File

@ -2,12 +2,14 @@ require 'active_model/serializable/utils'
module ActiveModel module ActiveModel
module Serializable module Serializable
INSTRUMENTATION_KEY = "!serialize.active_model_serializers".freeze
def self.included(base) def self.included(base)
base.extend Utils base.extend Utils
end end
def as_json(options={}) def as_json(options={})
instrument('!serialize') do instrument do
if root = options.fetch(:root, json_key) if root = options.fetch(:root, json_key)
hash = { root => serializable_object(options) } hash = { root => serializable_object(options) }
hash.merge!(serializable_data) hash.merge!(serializable_data)
@ -19,9 +21,7 @@ module ActiveModel
end end
def serializable_object_with_notification(options={}) def serializable_object_with_notification(options={})
instrument('!serialize') do instrument { serializable_object(options) }
serializable_object(options)
end
end end
def serializable_data def serializable_data
@ -51,9 +51,9 @@ module ActiveModel
modules[0..-2].join('::') if modules.size > 1 modules[0..-2].join('::') if modules.size > 1
end end
def instrument(action, &block) def instrument(&block)
payload = { serializer: self.class.name } payload = { serializer: self.class.name }
ActiveSupport::Notifications.instrument("#{action}.active_model_serializers", payload, &block) ActiveSupport::Notifications.instrument(INSTRUMENTATION_KEY, payload, &block)
end end
end end
end end