Skip to content

Commit

Permalink
Add AS::Notifications (thx @twinturbo)
Browse files Browse the repository at this point in the history
  • Loading branch information
twinturbo authored and wycats committed Jun 5, 2012
1 parent 6e2554d commit 2139a6d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions lib/active_model_serializers.rb
Original file line number Diff line number Diff line change
@@ -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"

Expand Down

0 comments on commit 2139a6d

Please sign in to comment.