Skip to content

Commit a065bc2

Browse files
committed
Fix serialization scope options
1 parent 96c5516 commit a065bc2

File tree

3 files changed

+49
-35
lines changed

3 files changed

+49
-35
lines changed

lib/action_controller/serialization.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def get_serializer(resource, options = {})
3030
options[:adapter] = false
3131
end
3232
serializable_resource = ActiveModelSerializers::SerializableResource.new(resource, options)
33-
serializable_resource.serialization_scope ||= serialization_scope
34-
serializable_resource.serialization_scope_name = _serialization_scope
33+
serializable_resource.serialization_scope ||= options.fetch(:scope) { serialization_scope }
34+
serializable_resource.serialization_scope_name = options.fetch(:scope_name) { _serialization_scope }
3535
# For compatibility with the JSON renderer: `json.to_json(options) if json.is_a?(String)`.
3636
# Otherwise, since `serializable_resource` is not a string, the renderer would call
3737
# `to_json` on a String and given odd results, such as `"".to_json #=> '""'`

lib/active_model/serializer.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,18 @@ def self.get_serializer_for(klass)
9696
end
9797
end
9898

99-
def self._serializer_instance_method_defined?(name)
100-
_serializer_instance_methods.include?(name)
99+
# rubocop:disable Style/ClassVars
100+
def self.method_added(method_name)
101+
@@_serializer_instance_methods ||= Hash.new { |h, k| h[k] = Set.new }
102+
@@_serializer_instance_methods[self] << method_name
101103
end
102104

103-
# TODO: Fix load-order failures when different serializer instances define different
104-
# scope methods
105-
def self._serializer_instance_methods
106-
@_serializer_instance_methods ||= (public_instance_methods - Object.public_instance_methods).to_set
105+
def self._serializer_instance_method_defined?(name)
106+
@_serializer_instance_methods ||= (ActiveModel::Serializer.public_instance_methods - Object.public_instance_methods).to_set
107+
@_serializer_instance_methods.include?(name) ||
108+
@@_serializer_instance_methods[self].include?(name)
107109
end
108-
private_class_method :_serializer_instance_methods
110+
# rubocop:enable Style/ClassVars
109111

110112
attr_accessor :object, :root, :scope
111113

test/action_controller/serialization_scope_name_test.rb

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,6 @@ def test_serialization_scope_admin
158158
assert_equal expected_json, @response.body
159159
end
160160
end
161-
# FIXME: Has bugs. See comments below and
162-
# https://github.com/rails-api/active_model_serializers/issues/1509
163161
class NilSerializationScopeTest < ActionController::TestCase
164162
class PostViewContextTestController < ActionController::Base
165163
serialization_scope nil
@@ -171,12 +169,15 @@ def render_post_with_no_scope
171169
render json: new_post, serializer: PostSerializer, adapter: :json
172170
end
173171

174-
# TODO: run test when
175-
# global state in Serializer._serializer_instance_methods is fixed
176-
# def render_post_with_passed_in_scope
177-
# self.current_user = User.new(id: 3, name: 'Pete', admin: false)
178-
# render json: new_post, serializer: PostSerializer, adapter: :json, scope: current_user, scope_name: :current_user
179-
# end
172+
def render_post_with_passed_in_scope
173+
self.current_user = User.new(id: 3, name: 'Pete', admin: false)
174+
render json: new_post, serializer: PostSerializer, adapter: :json, scope: current_user, scope_name: :current_user
175+
end
176+
177+
def render_post_with_passed_in_scope_without_scope_name
178+
self.current_user = User.new(id: 3, name: 'Pete', admin: false)
179+
render json: new_post, serializer: PostSerializer, adapter: :json, scope: current_user
180+
end
180181

181182
private
182183

@@ -194,8 +195,6 @@ def test_nil_serialization_scope_object
194195
assert_nil @controller.serialization_scope
195196
end
196197

197-
# TODO: change to NoMethodError and match 'admin?' when the
198-
# global state in Serializer._serializer_instance_methods is fixed
199198
def test_nil_scope
200199
if Rails.version.start_with?('4.0')
201200
exception_class = NoMethodError
@@ -210,21 +209,34 @@ def test_nil_scope
210209
assert_match exception_matcher, exception.message
211210
end
212211

213-
# TODO: run test when
214-
# global state in Serializer._serializer_instance_methods is fixed
215-
# def test_nil_scope_passed_in_current_user
216-
# get :render_post_with_passed_in_scope
217-
# expected_json = {
218-
# post: {
219-
# id: 4,
220-
# title: 'Title',
221-
# body: "The 'scope' is the 'current_user': true",
222-
# comments: [
223-
# { id: 2, body: 'Scoped' }
224-
# ]
225-
# }
226-
# }.to_json
227-
# assert_equal expected_json, @response.body
228-
# end
212+
def test_serialization_scope_is_and_nil_scope_passed_in_current_user
213+
get :render_post_with_passed_in_scope
214+
expected_json = {
215+
post: {
216+
id: 4,
217+
title: 'Title',
218+
body: "The 'scope' is the 'current_user': true",
219+
comments: [
220+
{ id: 2, body: 'Scoped' }
221+
]
222+
}
223+
}.to_json
224+
assert_equal expected_json, @response.body
225+
end
226+
227+
def test_serialization_scope_is_nil_and_scope_passed_in_current_user_without_scope_name
228+
get :render_post_with_passed_in_scope_without_scope_name
229+
expected_json = {
230+
post: {
231+
id: 4,
232+
title: 'Title',
233+
body: "The 'scope' is the 'current_user': true",
234+
comments: [
235+
{ id: 2, body: 'Scoped' }
236+
]
237+
}
238+
}.to_json
239+
assert_equal expected_json, @response.body
240+
end
229241
end
230242
end

0 commit comments

Comments
 (0)