From 2139a6d07a52e2d4b41fe1e3252a9cceee48294d Mon Sep 17 00:00:00 2001 From: twinturbo Date: Thu, 26 Apr 2012 08:39:59 -0500 Subject: [PATCH] Add AS::Notifications (thx @twinturbo) --- lib/active_model/serializer.rb | 16 +++++++++++++--- lib/active_model_serializers.rb | 1 + 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index c9ad56393..e5021320a 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -410,9 +410,13 @@ def as_json(options=nil) # Returns a hash representation of the serializable # object without the root. def serializable_hash - node = attributes - include_associations!(node) if _embed - node + instrument(:serialize, :serializer => self.class.name) do + node = attributes + instrument :associations do + include_associations!(node) if _embed + end + node + end end def include_associations!(node) @@ -513,6 +517,12 @@ def attributes end alias :read_attribute_for_serialization :send + + # Use ActiveSupport::Notifications to send events to external systems. + # The event name is: name.class_name.serializer + def instrument(name, payload = {}, &block) + ActiveSupport::Notifications.instrument("#{name}.serializer", payload, &block) + end end end diff --git a/lib/active_model_serializers.rb b/lib/active_model_serializers.rb index 6f0e89e7a..da4c17808 100644 --- a/lib/active_model_serializers.rb +++ b/lib/active_model_serializers.rb @@ -1,5 +1,6 @@ require "active_support" require "active_support/core_ext/string/inflections" +require "active_support/notifications" require "active_model" require "active_model/serializer"