mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #2080 from yosiat/instrumentation-cleanup
[0.9] Removing `instrumentation_keys` in order to fix the payload
This commit is contained in:
commit
ed72979168
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
### [0-9-stable](https://github.com/rails-api/active_model_serializers/compare/v0.9.5...0-9-stable)
|
### [0-9-stable](https://github.com/rails-api/active_model_serializers/compare/v0.9.5...0-9-stable)
|
||||||
|
|
||||||
|
- [#2080](https://github.com/rails-api/active_model_serializers/pull/2080) remove `{ payload: nil }` from `!serialize.active_model_serializers` ActiveSupport::Notification. `payload` never had a value. Changes, for example `{ serializer: 'ActiveModel::DefaultSerializer', payload: nil }` to be `{ serializer: 'ActiveModel::DefaultSerializer' }` (@yosiat)
|
||||||
|
|
||||||
### [v0.9.6 (2017-01-10)](https://github.com/rails-api/active_model_serializers/compare/v0.9.5...v0.9.6)
|
### [v0.9.6 (2017-01-10)](https://github.com/rails-api/active_model_serializers/compare/v0.9.5...v0.9.6)
|
||||||
|
|
||||||
- [#2008](https://github.com/rails-api/active_model_serializers/pull/2008) Fix warning on Thor. (@kirs)
|
- [#2008](https://github.com/rails-api/active_model_serializers/pull/2008) Fix warning on Thor. (@kirs)
|
||||||
|
|||||||
@ -64,10 +64,5 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def instrumentation_keys
|
|
||||||
[:object, :scope, :root, :meta_key, :meta, :each_serializer, :resource_name, :key_format, :context]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -15,18 +15,14 @@ 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
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
alias serializable_hash as_json
|
alias serializable_hash as_json
|
||||||
alias serializable_object as_json
|
alias serializable_object as_json
|
||||||
|
|
||||||
private
|
|
||||||
def instrumentation_keys
|
|
||||||
[:object, :wrap_in_array]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -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,16 +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 = instrumentation_keys.inject({ serializer: self.class.name }) do |payload, key|
|
payload = { serializer: self.class.name }
|
||||||
payload[:payload] = self.instance_variable_get(:"@#{key}")
|
ActiveSupport::Notifications.instrument(INSTRUMENTATION_KEY, payload, &block)
|
||||||
payload
|
|
||||||
end
|
|
||||||
ActiveSupport::Notifications.instrument("#{action}.active_model_serializers", payload, &block)
|
|
||||||
end
|
|
||||||
|
|
||||||
def instrumentation_keys
|
|
||||||
[:object, :scope, :root, :meta_key, :meta, :wrap_in_array, :only, :except, :key_format]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
50
test/unit/active_model/serilizable_test.rb
Normal file
50
test/unit/active_model/serilizable_test.rb
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class SerializableTest
|
||||||
|
class InstrumentationTest < Minitest::Test
|
||||||
|
def setup
|
||||||
|
@events = []
|
||||||
|
|
||||||
|
@subscriber = ActiveSupport::Notifications.subscribe('!serialize.active_model_serializers') do |name, start, finish, id, payload|
|
||||||
|
@events << { name: name, serializer: payload[:serializer] }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def teardown
|
||||||
|
ActiveSupport::Notifications.unsubscribe(@subscriber) if defined?(@subscriber)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_instruments_default_serializer
|
||||||
|
DefaultSerializer.new(1).as_json
|
||||||
|
|
||||||
|
assert_equal [{ name: '!serialize.active_model_serializers', serializer: 'ActiveModel::DefaultSerializer' }], @events
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_instruments_serializer
|
||||||
|
profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||||
|
serializer = ProfileSerializer.new(profile)
|
||||||
|
|
||||||
|
serializer.as_json
|
||||||
|
|
||||||
|
assert_equal [{ name: '!serialize.active_model_serializers', serializer: 'ProfileSerializer' }], @events
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_instruments_array_serializer
|
||||||
|
profiles = [
|
||||||
|
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
|
||||||
|
Profile.new(name: 'Name 2', description: 'Description 2', comments: 'Comments 2')
|
||||||
|
]
|
||||||
|
serializer = ArraySerializer.new(profiles)
|
||||||
|
|
||||||
|
serializer.as_json
|
||||||
|
|
||||||
|
assert_equal [
|
||||||
|
{ name: '!serialize.active_model_serializers', serializer: 'ProfileSerializer' },
|
||||||
|
{ name: '!serialize.active_model_serializers', serializer: 'ProfileSerializer' },
|
||||||
|
{ name: '!serialize.active_model_serializers', serializer: 'ActiveModel::ArraySerializer' }
|
||||||
|
], @events
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user