mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Add :only/:except options
This commit is contained in:
parent
3201c63162
commit
2e31a14125
@ -108,6 +108,8 @@ end
|
|||||||
@meta_key = options[:meta_key] || :meta
|
@meta_key = options[:meta_key] || :meta
|
||||||
@meta = options[@meta_key]
|
@meta = options[@meta_key]
|
||||||
@wrap_in_array = options[:_wrap_in_array]
|
@wrap_in_array = options[:_wrap_in_array]
|
||||||
|
@only = Array(options[:only]) if options[:only]
|
||||||
|
@except = Array(options[:except]) if options[:except]
|
||||||
end
|
end
|
||||||
attr_accessor :object, :scope, :root, :meta_key, :meta
|
attr_accessor :object, :scope, :root, :meta_key, :meta
|
||||||
|
|
||||||
@ -140,7 +142,13 @@ end
|
|||||||
end
|
end
|
||||||
|
|
||||||
def filter(keys)
|
def filter(keys)
|
||||||
keys
|
if @only
|
||||||
|
keys & @only
|
||||||
|
elsif @except
|
||||||
|
keys - @except
|
||||||
|
else
|
||||||
|
keys
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def embedded_in_root_associations
|
def embedded_in_root_associations
|
||||||
|
|||||||
@ -2,6 +2,26 @@ require 'test_helper'
|
|||||||
|
|
||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
|
class FilterOptionsTest < Minitest::Test
|
||||||
|
def setup
|
||||||
|
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_only_option
|
||||||
|
@profile_serializer = ProfileSerializer.new(@profile, only: :name)
|
||||||
|
assert_equal({
|
||||||
|
'profile' => { name: 'Name 1' }
|
||||||
|
}, @profile_serializer.as_json)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_except_option
|
||||||
|
@profile_serializer = ProfileSerializer.new(@profile, except: :comments)
|
||||||
|
assert_equal({
|
||||||
|
'profile' => { name: 'Name 1', description: 'Description 1' }
|
||||||
|
}, @profile_serializer.as_json)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class FilterAttributesTest < Minitest::Test
|
class FilterAttributesTest < Minitest::Test
|
||||||
def setup
|
def setup
|
||||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user