Skip to content

Commit

Permalink
Use ActiveModel::Callbacks on the SerializableResource
Browse files Browse the repository at this point in the history
  • Loading branch information
maurogeorge committed Nov 5, 2015
1 parent 2c4a028 commit 49c8f69
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/active_model/serializable_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
module ActiveModel
class SerializableResource
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links])
extend ActiveModel::Callbacks

define_model_callbacks :render

around_render do |_, block, _|
notify_active_support do
block.call
end
end

# Primary interface to composing a resource with a serializer and adapter.
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
Expand All @@ -12,19 +21,19 @@ def initialize(resource, options = {})
end

def serializable_hash(*args)
notify_active_support do
run_callbacks :render do
adapter.serializable_hash(*args)
end
end

def as_json(*args)
notify_active_support do
run_callbacks :render do
adapter.as_json(*args)
end
end

def to_json(*args)
notify_active_support do
run_callbacks :render do
adapter.to_json(*args)
end
end
Expand Down

0 comments on commit 49c8f69

Please sign in to comment.