Skip to content

Commit 2da0b5b

Browse files
hmcfletchshishirmk
authored andcommitted
rework of AS Notifications
1 parent f029cf7 commit 2da0b5b

6 files changed

Lines changed: 160 additions & 18 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'fast_jsonapi/instrumentation/serializable_hash'
2+
require 'fast_jsonapi/instrumentation/serialized_json'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'active_support/notifications'
2+
3+
module FastJsonapi
4+
module ObjectSerializer
5+
6+
alias_method :serializable_hash_without_instrumentation, :serializable_hash
7+
8+
def serializable_hash
9+
ActiveSupport::Notifications.instrument(SERIALIZABLE_HASH_NOTIFICATION, { name: self.class.name }) do
10+
serializable_hash_without_instrumentation
11+
end
12+
end
13+
14+
end
15+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'active_support/notifications'
2+
3+
module FastJsonapi
4+
module ObjectSerializer
5+
6+
alias_method :serialized_json_without_instrumentation, :serialized_json
7+
8+
def serialized_json
9+
ActiveSupport::Notifications.instrument(SERIALIZED_JSON_NOTIFICATION, { name: self.class.name }) do
10+
serialized_json_without_instrumentation
11+
end
12+
end
13+
14+
end
15+
end

lib/fast_jsonapi/object_serializer.rb

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,15 @@
55
require 'active_support/inflector'
66
require 'fast_jsonapi/serialization_core'
77

8-
begin
9-
require 'skylight'
10-
SKYLIGHT_ENABLED = true
11-
rescue LoadError
12-
SKYLIGHT_ENABLED = false
13-
end
14-
158
module FastJsonapi
169
module ObjectSerializer
1710
extend ActiveSupport::Concern
1811
include SerializationCore
1912

20-
included do
21-
# Skylight integration
22-
# To remove Skylight
23-
# Remove the included do block
24-
# Remove the Gemfile entry
25-
if SKYLIGHT_ENABLED
26-
include Skylight::Helpers
27-
28-
instrument_method :serializable_hash
29-
instrument_method :to_json
30-
end
13+
SERIALIZABLE_HASH_NOTIFICATION = 'render.fast_jsonapi.serializable_hash'.freeze
14+
SERIALIZED_JSON_NOTIFICATION = 'render.fast_jsonapi.serialized_json'.freeze
3115

16+
included do
3217
# Set record_type based on the name of the serializer class
3318
set_type(reflected_record_type) if reflected_record_type
3419
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require 'spec_helper'
2+
3+
describe FastJsonapi::ObjectSerializer do
4+
include_context 'movie class'
5+
6+
context 'instrument' do
7+
8+
before(:each) do
9+
options = {}
10+
options[:meta] = { total: 2 }
11+
options[:include] = [:actors]
12+
13+
@serializer = MovieSerializer.new([movie, movie], options)
14+
end
15+
16+
context 'serializable_hash' do
17+
18+
it 'should send not notifications' do
19+
events = []
20+
21+
ActiveSupport::Notifications.subscribe(FastJsonapi::ObjectSerializer::SERIALIZABLE_HASH_NOTIFICATION) do |*args|
22+
events << ActiveSupport::Notifications::Event.new(*args)
23+
end
24+
25+
serialized_hash = @serializer.serializable_hash
26+
27+
expect(events.length).to eq(0)
28+
29+
expect(serialized_hash.key?(:data)).to eq(true)
30+
expect(serialized_hash.key?(:meta)).to eq(true)
31+
expect(serialized_hash.key?(:included)).to eq(true)
32+
end
33+
34+
end
35+
36+
context 'serialized_json' do
37+
38+
it 'should send not notifications' do
39+
events = []
40+
41+
ActiveSupport::Notifications.subscribe(FastJsonapi::ObjectSerializer::SERIALIZED_JSON_NOTIFICATION) do |*args|
42+
events << ActiveSupport::Notifications::Event.new(*args)
43+
end
44+
45+
json = @serializer.serialized_json
46+
47+
expect(events.length).to eq(0)
48+
49+
expect(json.length).to be > 50
50+
end
51+
52+
end
53+
54+
end
55+
56+
end
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
require 'spec_helper'
2+
require 'fast_jsonapi/instrumentation'
3+
4+
describe FastJsonapi::ObjectSerializer do
5+
include_context 'movie class'
6+
7+
context 'instrument' do
8+
9+
before(:each) do
10+
options = {}
11+
options[:meta] = { total: 2 }
12+
options[:include] = [:actors]
13+
14+
@serializer = MovieSerializer.new([movie, movie], options)
15+
end
16+
17+
context 'serializable_hash' do
18+
19+
it 'should send notifications' do
20+
events = []
21+
22+
ActiveSupport::Notifications.subscribe(FastJsonapi::ObjectSerializer::SERIALIZABLE_HASH_NOTIFICATION) do |*args|
23+
events << ActiveSupport::Notifications::Event.new(*args)
24+
end
25+
26+
serialized_hash = @serializer.serializable_hash
27+
28+
expect(events.length).to eq(1)
29+
30+
event = events.first
31+
32+
expect(event.duration).to be > 0
33+
expect(event.payload).to eq({ name: 'MovieSerializer' })
34+
expect(event.name).to eq(FastJsonapi::ObjectSerializer::SERIALIZABLE_HASH_NOTIFICATION)
35+
36+
expect(serialized_hash.key?(:data)).to eq(true)
37+
expect(serialized_hash.key?(:meta)).to eq(true)
38+
expect(serialized_hash.key?(:included)).to eq(true)
39+
end
40+
41+
end
42+
43+
context 'serialized_json' do
44+
45+
it 'should send notifications' do
46+
events = []
47+
48+
ActiveSupport::Notifications.subscribe(FastJsonapi::ObjectSerializer::SERIALIZED_JSON_NOTIFICATION) do |*args|
49+
events << ActiveSupport::Notifications::Event.new(*args)
50+
end
51+
52+
json = @serializer.serialized_json
53+
54+
expect(events.length).to eq(1)
55+
56+
event = events.first
57+
58+
expect(event.duration).to be > 0
59+
expect(event.payload).to eq({ name: 'MovieSerializer' })
60+
expect(event.name).to eq(FastJsonapi::ObjectSerializer::SERIALIZED_JSON_NOTIFICATION)
61+
62+
expect(json.length).to be > 50
63+
end
64+
65+
end
66+
67+
end
68+
69+
end

0 commit comments

Comments
 (0)