Skip to content

Commit bc7419f

Browse files
committed
Don’t attempt to serialize resources when the context is unsupported
1 parent 7e97fb0 commit bc7419f

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

lib/userlist/push/serializer.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ def serialize(resource)
1919
end
2020

2121
def serialize?(resource)
22-
resource.public_send("#{context}?")
22+
method_name = "#{context}?"
23+
24+
resource.respond_to?(method_name) &&
25+
resource.public_send(method_name)
2326
end
2427

2528
private

spec/userlist/push/serializer_spec.rb

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,36 @@
7676
end
7777

7878
context 'when serializing the user is not allowed' do
79-
before do
80-
allow_any_instance_of(Userlist::Push::User).to receive(:push?).and_return(false)
79+
context 'when the context is push' do
80+
subject { described_class.new(context: :push) }
81+
82+
before do
83+
expect_any_instance_of(Userlist::Push::User).to receive(:push?).and_return(false)
84+
end
85+
86+
it 'should return no payload' do
87+
expect(payload).to_not be
88+
end
8189
end
8290

83-
it 'should return no payload' do
84-
expect(payload).to_not be
91+
context 'when the context is delete' do
92+
subject { described_class.new(context: :delete) }
93+
94+
before do
95+
expect_any_instance_of(Userlist::Push::User).to receive(:delete?).and_return(false)
96+
end
97+
98+
it 'should return no payload' do
99+
expect(payload).to_not be
100+
end
101+
end
102+
103+
context 'when the context is unsupported' do
104+
subject { described_class.new(context: :unsupported) }
105+
106+
it 'should return no payload' do
107+
expect(payload).to_not be
108+
end
85109
end
86110
end
87111

0 commit comments

Comments
 (0)