pass array of unique movies to serializer

This commit is contained in:
iwsksky 2018-12-21 03:44:42 +09:00 committed by Kevin Pheasey
parent 5767664c8a
commit b9a86a002a
6 changed files with 22 additions and 14 deletions

View File

@ -308,7 +308,7 @@ options[:links] = {
prev: '...' prev: '...'
} }
options[:include] = [:actors, :'actors.agency', :'actors.agency.state'] options[:include] = [:actors, :'actors.agency', :'actors.agency.state']
MovieSerializer.new([movie, movie], options).serialized_json MovieSerializer.new(movies, options).serialized_json
``` ```
### Collection Serialization ### Collection Serialization
@ -320,7 +320,7 @@ options[:links] = {
next: '...', next: '...',
prev: '...' prev: '...'
} }
hash = MovieSerializer.new([movie, movie], options).serializable_hash hash = MovieSerializer.new(movies, options).serializable_hash
json_string = MovieSerializer.new([movie, movie], options).serialized_json json_string = MovieSerializer.new([movie, movie], options).serialized_json
``` ```

View File

@ -10,7 +10,8 @@ describe FastJsonapi::ObjectSerializer do
options[:meta] = { total: 2 } options[:meta] = { total: 2 }
options[:include] = [:actors] options[:include] = [:actors]
@serializer = MovieSerializer.new([movie, movie], options) movies = build_movies(2)
@serializer = MovieSerializer.new(movies, options)
end end
context 'serializable_hash' do context 'serializable_hash' do

View File

@ -24,7 +24,8 @@ describe FastJsonapi::ObjectSerializer do
options[:meta] = { total: 2 } options[:meta] = { total: 2 }
options[:include] = [:actors] options[:include] = [:actors]
@serializer = MovieSerializer.new([movie, movie], options) movies = build_movies(2)
@serializer = MovieSerializer.new(movies, options)
end end
context 'serializable_hash' do context 'serializable_hash' do

View File

@ -16,7 +16,8 @@ describe FastJsonapi::ObjectSerializer do
options[:links] = { self: 'self' } options[:links] = { self: 'self' }
options[:include] = [:actors] options[:include] = [:actors]
serializable_hash = CachingMovieSerializer.new([movie, movie], options).serializable_hash movies = build_movies(2)
serializable_hash = CachingMovieSerializer.new(movies, options).serializable_hash
expect(serializable_hash[:data].length).to eq 2 expect(serializable_hash[:data].length).to eq 2
expect(serializable_hash[:data][0][:relationships].length).to eq 3 expect(serializable_hash[:data][0][:relationships].length).to eq 3

View File

@ -213,7 +213,7 @@ describe FastJsonapi::ObjectSerializer do
end end
context 'when an array of records is given' do context 'when an array of records is given' do
let(:resource) { [movie, movie] } let(:resource) { build_movies(2) }
it 'returns correct hash which id equals owner_id' do it 'returns correct hash which id equals owner_id' do
expect(serializable_hash[:data][0][:id].to_i).to eq movie.owner_id expect(serializable_hash[:data][0][:id].to_i).to eq movie.owner_id
@ -240,7 +240,7 @@ describe FastJsonapi::ObjectSerializer do
end end
context 'when an array of records is given' do context 'when an array of records is given' do
let(:resource) { [movie, movie] } let(:resource) { build_movies(2) }
it 'returns correct hash which id equals movie-id' do it 'returns correct hash which id equals movie-id' do
expect(serializable_hash[:data][0][:id]).to eq "movie-#{movie.owner_id}" expect(serializable_hash[:data][0][:id]).to eq "movie-#{movie.owner_id}"
@ -403,7 +403,7 @@ describe FastJsonapi::ObjectSerializer do
end end
describe '#key_transform' do describe '#key_transform' do
subject(:hash) { movie_serializer_class.new([movie, movie], include: [:movie_type]).serializable_hash } subject(:hash) { movie_serializer_class.new(build_movies(2), include: [:movie_type]).serializable_hash }
let(:movie_serializer_class) { "#{key_transform}_movie_serializer".classify.constantize } let(:movie_serializer_class) { "#{key_transform}_movie_serializer".classify.constantize }

View File

@ -10,7 +10,8 @@ describe FastJsonapi::ObjectSerializer do
options[:meta] = { total: 2 } options[:meta] = { total: 2 }
options[:links] = { self: 'self' } options[:links] = { self: 'self' }
options[:include] = [:actors] options[:include] = [:actors]
serializable_hash = MovieSerializer.new([movie, movie], options).serializable_hash movies = build_movies(2)
serializable_hash = MovieSerializer.new(movies, options).serializable_hash
expect(serializable_hash[:data].length).to eq 2 expect(serializable_hash[:data].length).to eq 2
expect(serializable_hash[:data][0][:relationships].length).to eq 4 expect(serializable_hash[:data][0][:relationships].length).to eq 4
@ -58,7 +59,8 @@ describe FastJsonapi::ObjectSerializer do
it 'returns correct number of records when serialized_json is called for an array' do it 'returns correct number of records when serialized_json is called for an array' do
options = {} options = {}
options[:meta] = { total: 2 } options[:meta] = { total: 2 }
json = MovieSerializer.new([movie, movie], options).serialized_json movies = build_movies(2)
json = MovieSerializer.new(movies, options).serialized_json
serializable_hash = JSON.parse(json) serializable_hash = JSON.parse(json)
expect(serializable_hash['data'].length).to eq 2 expect(serializable_hash['data'].length).to eq 2
expect(serializable_hash['meta']).to be_instance_of(Hash) expect(serializable_hash['meta']).to be_instance_of(Hash)
@ -124,7 +126,8 @@ describe FastJsonapi::ObjectSerializer do
end end
it 'returns multiple records' do it 'returns multiple records' do
json_hash = MovieSerializer.new([movie, movie]).as_json movies = build_movies(2)
json_hash = MovieSerializer.new(movies).as_json
expect(json_hash['data'].length).to eq 2 expect(json_hash['data'].length).to eq 2
end end
@ -139,7 +142,8 @@ describe FastJsonapi::ObjectSerializer do
options = {} options = {}
options[:meta] = { total: 2 } options[:meta] = { total: 2 }
options[:include] = [:blah_blah] options[:include] = [:blah_blah]
expect { MovieSerializer.new([movie, movie], options).serializable_hash }.to raise_error(ArgumentError) movies = build_movies(2)
expect { MovieSerializer.new(movies, options).serializable_hash }.to raise_error(ArgumentError)
end end
it 'returns errors when serializing with non-existent and existent includes keys' do it 'returns errors when serializing with non-existent and existent includes keys' do
@ -165,9 +169,10 @@ describe FastJsonapi::ObjectSerializer do
options = {} options = {}
options[:meta] = { total: 2 } options[:meta] = { total: 2 }
options[:include] = [''] options[:include] = ['']
expect(MovieSerializer.new([movie, movie], options).serializable_hash.keys).to eq [:data, :meta] movies = build_movies(2)
expect(MovieSerializer.new(movies, options).serializable_hash.keys).to eq [:data, :meta]
options[:include] = [nil] options[:include] = [nil]
expect(MovieSerializer.new([movie, movie], options).serializable_hash.keys).to eq [:data, :meta] expect(MovieSerializer.new(movies, options).serializable_hash.keys).to eq [:data, :meta]
end end
end end