diff --git a/lib/fast_jsonapi/object_serializer.rb b/lib/fast_jsonapi/object_serializer.rb index 2f5ebe4..6de62ea 100644 --- a/lib/fast_jsonapi/object_serializer.rb +++ b/lib/fast_jsonapi/object_serializer.rb @@ -34,6 +34,7 @@ module FastJsonapi def hash_for_one_record serializable_hash = { data: nil } serializable_hash[:meta] = @meta if @meta.present? + serializable_hash[:links] = @links if @links.present? return serializable_hash unless @resource @@ -55,6 +56,7 @@ module FastJsonapi serializable_hash[:data] = data serializable_hash[:included] = included if @includes.present? serializable_hash[:meta] = @meta if @meta.present? + serializable_hash[:links] = @links if @links.present? serializable_hash end @@ -69,6 +71,7 @@ module FastJsonapi @known_included_objects = {} @meta = options[:meta] + @links = options[:links] if options[:include].present? @includes = options[:include].delete_if(&:blank?).map(&:to_sym) diff --git a/spec/lib/object_serializer_caching_spec.rb b/spec/lib/object_serializer_caching_spec.rb index de0186a..8e455d4 100644 --- a/spec/lib/object_serializer_caching_spec.rb +++ b/spec/lib/object_serializer_caching_spec.rb @@ -13,6 +13,8 @@ describe FastJsonapi::ObjectSerializer do it 'returns correct hash when serializable_hash is called' do options = {} options[:meta] = { total: 2 } + options[:links] = { self: 'self' } + options[:include] = [:actors] serializable_hash = CachingMovieSerializer.new([movie, movie], options).serializable_hash @@ -21,6 +23,7 @@ describe FastJsonapi::ObjectSerializer do expect(serializable_hash[:data][0][:attributes].length).to eq 2 expect(serializable_hash[:meta]).to be_instance_of(Hash) + expect(serializable_hash[:links]).to be_instance_of(Hash) expect(serializable_hash[:included]).to be_instance_of(Array) expect(serializable_hash[:included][0]).to be_instance_of(Hash) @@ -30,6 +33,7 @@ describe FastJsonapi::ObjectSerializer do expect(serializable_hash[:data]).to be_instance_of(Hash) expect(serializable_hash[:meta]).to be nil + expect(serializable_hash[:links]).to be nil expect(serializable_hash[:included]).to be nil end diff --git a/spec/lib/object_serializer_spec.rb b/spec/lib/object_serializer_spec.rb index 83e1eee..111c2d4 100644 --- a/spec/lib/object_serializer_spec.rb +++ b/spec/lib/object_serializer_spec.rb @@ -7,6 +7,7 @@ describe FastJsonapi::ObjectSerializer do it 'returns correct hash when serializable_hash is called' do options = {} options[:meta] = { total: 2 } + options[:links] = { self: 'self' } options[:include] = [:actors] serializable_hash = MovieSerializer.new([movie, movie], options).serializable_hash @@ -15,6 +16,7 @@ describe FastJsonapi::ObjectSerializer do expect(serializable_hash[:data][0][:attributes].length).to eq 2 expect(serializable_hash[:meta]).to be_instance_of(Hash) + expect(serializable_hash[:links]).to be_instance_of(Hash) expect(serializable_hash[:included]).to be_instance_of(Array) expect(serializable_hash[:included][0]).to be_instance_of(Hash) @@ -24,6 +26,7 @@ describe FastJsonapi::ObjectSerializer do expect(serializable_hash[:data]).to be_instance_of(Hash) expect(serializable_hash[:meta]).to be nil + expect(serializable_hash[:links]).to be nil expect(serializable_hash[:included]).to be nil end diff --git a/spec/lib/object_serializer_struct_spec.rb b/spec/lib/object_serializer_struct_spec.rb index cb9054b..d4cda1b 100644 --- a/spec/lib/object_serializer_struct_spec.rb +++ b/spec/lib/object_serializer_struct_spec.rb @@ -7,6 +7,7 @@ describe FastJsonapi::ObjectSerializer do it 'returns correct hash when serializable_hash is called' do options = {} options[:meta] = { total: 2 } + options[:links] = { self: 'self' } options[:include] = [:actors] serializable_hash = MovieSerializer.new([movie_struct, movie_struct], options).serializable_hash @@ -15,6 +16,7 @@ describe FastJsonapi::ObjectSerializer do expect(serializable_hash[:data][0][:attributes].length).to eq 2 expect(serializable_hash[:meta]).to be_instance_of(Hash) + expect(serializable_hash[:links]).to be_instance_of(Hash) expect(serializable_hash[:included]).to be_instance_of(Array) expect(serializable_hash[:included][0]).to be_instance_of(Hash) @@ -24,6 +26,7 @@ describe FastJsonapi::ObjectSerializer do expect(serializable_hash[:data]).to be_instance_of(Hash) expect(serializable_hash[:meta]).to be nil + expect(serializable_hash[:links]).to be nil expect(serializable_hash[:included]).to be nil expect(serializable_hash[:data][:id]).to eq movie_struct.id.to_s end