Skip to content

Commit 89f87bf

Browse files
committed
Merge pull request #679 from Ahalogy/serializer-options
Serializer options
2 parents 4a4bcdc + 2a1d934 commit 89f87bf

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

lib/active_model/array_serializer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ def serializer_for(item)
3838
serializer_class.new(item, scope: scope, key_format: key_format, only: @only, except: @except, polymorphic: @polymorphic, namespace: @namespace)
3939
end
4040

41-
def serializable_object
41+
def serializable_object(options={})
4242
@object.map do |item|
43-
serializer_for(item).serializable_object_with_notification
43+
serializer_for(item).serializable_object_with_notification(options)
4444
end
4545
end
4646
alias_method :serializable_array, :serializable_object

lib/active_model/serializable.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ def self.included(base)
99
def as_json(options={})
1010
instrument('!serialize') do
1111
if root = options.fetch(:root, json_key)
12-
hash = { root => serializable_object }
12+
hash = { root => serializable_object(options) }
1313
hash.merge!(serializable_data)
1414
hash
1515
else
16-
serializable_object
16+
serializable_object(options)
1717
end
1818
end
1919
end
2020

21-
def serializable_object_with_notification
21+
def serializable_object_with_notification(options={})
2222
instrument('!serialize') do
23-
serializable_object
23+
serializable_object(options)
2424
end
2525
end
2626

lib/active_model/serializer.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,13 @@ def convert_keys(hash)
273273
end]
274274
end
275275

276+
attr_writer :serialization_options
277+
def serialization_options
278+
@serialization_options || {}
279+
end
280+
276281
def serializable_object(options={})
282+
self.serialization_options = options
277283
return @wrap_in_array ? [] : nil if @object.nil?
278284
hash = attributes
279285
hash.merge! associations

test/unit/active_model/serializer/options_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,24 @@ def test_custom_options_are_accessible_from_serializer
1111
assert_equal({foo: :bar}, @serializer.context)
1212
end
1313
end
14+
15+
class SerializationOptionsTest < Minitest::Test
16+
def setup
17+
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
18+
@profile_serializer = ProfileSerializer.new(@profile)
19+
@profile_serializer.instance_eval do
20+
def description
21+
serialization_options[:force_the_description]
22+
end
23+
end
24+
end
25+
26+
def test_filtered_attributes_serialization
27+
forced_description = "This is a test"
28+
assert_equal({
29+
'profile' => { name: 'Name 1', description: forced_description }
30+
}, @profile_serializer.as_json(force_the_description: forced_description))
31+
end
32+
end
1433
end
1534
end

0 commit comments

Comments
 (0)