diff --git a/spec/lib/object_serializer_class_methods_spec.rb b/spec/lib/object_serializer_class_methods_spec.rb index 7c6def2..1ade183 100644 --- a/spec/lib/object_serializer_class_methods_spec.rb +++ b/spec/lib/object_serializer_class_methods_spec.rb @@ -143,10 +143,6 @@ describe FastJsonapi::ObjectSerializer do describe '#attribute' do subject(:serializable_hash) { MovieSerializer.new(movie).serializable_hash } - after do - MovieSerializer.attributes_to_serialize = {} - end - context 'with block' do before do movie.release_year = 2008 @@ -155,10 +151,70 @@ describe FastJsonapi::ObjectSerializer do end end + after do + MovieSerializer.attributes_to_serialize.delete(:title_with_year) + end + it 'returns correct hash when serializable_hash is called' do expect(serializable_hash[:data][:attributes][:name]).to eq movie.name expect(serializable_hash[:data][:attributes][:title_with_year]).to eq "#{movie.name} (#{movie.release_year})" end end end + + describe '#key_transform' do + subject(:hash) { movie_serializer_class.new([movie, movie], include: [:movie_type]).serializable_hash } + + let(:movie_serializer_class) { "#{key_transform}_movie_serializer".classify.constantize } + + before(:context) do + [:dash, :camel, :camel_lower, :underscore].each do |key_transform| + movie_serializer_name = "#{key_transform}_movie_serializer".classify + movie_type_serializer_name = "#{key_transform}_movie_type_serializer".classify + # https://stackoverflow.com/questions/4113479/dynamic-class-definition-with-a-class-name + movie_serializer_class = Object.const_set(movie_serializer_name, Class.new) + # https://rubymonk.com/learning/books/5-metaprogramming-ruby-ascent/chapters/24-eval/lessons/67-instance-eval + movie_serializer_class.instance_eval do + include FastJsonapi::ObjectSerializer + set_type :movie + set_key_transform key_transform + attributes :name, :release_year + has_many :actors + belongs_to :owner, record_type: :user + belongs_to :movie_type, serializer: "#{key_transform}_movie_type" + end + movie_type_serializer_class = Object.const_set(movie_type_serializer_name, Class.new) + movie_type_serializer_class.instance_eval do + include FastJsonapi::ObjectSerializer + set_key_transform key_transform + set_type :movie_type + attributes :name + end + end + end + + context 'when key_transform is dash' do + let(:key_transform) { :dash } + + it_behaves_like 'returning key transformed hash', :'movie-type', :'release-year' + end + + context 'when key_transform is camel' do + let(:key_transform) { :camel } + + it_behaves_like 'returning key transformed hash', :MovieType, :ReleaseYear + end + + context 'when key_transform is camel_lower' do + let(:key_transform) { :camel_lower } + + it_behaves_like 'returning key transformed hash', :movieType, :releaseYear + end + + context 'when key_transform is underscore' do + let(:key_transform) { :underscore } + + it_behaves_like 'returning key transformed hash', :movie_type, :release_year + end + end end diff --git a/spec/lib/object_serializer_key_transform_spec.rb b/spec/lib/object_serializer_key_transform_spec.rb deleted file mode 100644 index 77338b1..0000000 --- a/spec/lib/object_serializer_key_transform_spec.rb +++ /dev/null @@ -1,80 +0,0 @@ -require 'spec_helper' - -describe FastJsonapi::ObjectSerializer do - include_context 'movie class' - include_context 'ams movie class' - - before(:context) do - [:dash, :camel, :camel_lower, :underscore].each do |transform_type| - movie_serializer_name = "#{transform_type}_movie_serializer".classify - movie_type_serializer_name = "#{transform_type}_movie_type_serializer".classify - # https://stackoverflow.com/questions/4113479/dynamic-class-definition-with-a-class-name - movie_serializer_class = Object.const_set( - movie_serializer_name, - Class.new { - } - ) - # https://rubymonk.com/learning/books/5-metaprogramming-ruby-ascent/chapters/24-eval/lessons/67-instance-eval - movie_serializer_class.instance_eval do - include FastJsonapi::ObjectSerializer - set_type :movie - set_key_transform transform_type - attributes :name, :release_year - has_many :actors - belongs_to :owner, record_type: :user - belongs_to :movie_type - end - movie_type_serializer_class = Object.const_set( - movie_type_serializer_name, - Class.new { - } - ) - movie_type_serializer_class.instance_eval do - include FastJsonapi::ObjectSerializer - set_key_transform transform_type - set_type :movie_type - attributes :name - end - end - end - - context 'when using dashes for word separation in the JSON API members' do - it 'returns correct hash when serializable_hash is called' do - serializable_hash = DashMovieSerializer.new([movie, movie]).serializable_hash - expect(serializable_hash[:data].length).to eq 2 - expect(serializable_hash[:data][0][:relationships].length).to eq 3 - expect(serializable_hash[:data][0][:relationships]).to have_key('movie-type'.to_sym) - expect(serializable_hash[:data][0][:attributes].length).to eq 2 - expect(serializable_hash[:data][0][:attributes]).to have_key("release-year".to_sym) - - serializable_hash = DashMovieSerializer.new(movie_struct).serializable_hash - expect(serializable_hash[:data][:relationships].length).to eq 3 - expect(serializable_hash[:data][:relationships]).to have_key('movie-type'.to_sym) - expect(serializable_hash[:data][:attributes].length).to eq 2 - expect(serializable_hash[:data][:attributes]).to have_key('release-year'.to_sym) - expect(serializable_hash[:data][:id]).to eq movie_struct.id.to_s - end - - it 'returns type hypenated when trying to serializing a class with multiple words' do - movie_type = MovieType.new - movie_type.id = 3 - movie_type.name = "x" - serializable_hash = DashMovieTypeSerializer.new(movie_type).serializable_hash - expect(serializable_hash[:data][:type].to_sym).to eq 'movie-type'.to_sym - end - end - - context 'when using other key transforms' do - [:camel, :camel_lower, :underscore, :dash].each do |transform_type| - it "returns same thing as ams when using #{transform_type}" do - ams_movie = build_ams_movies(1).first - movie = build_movies(1).first - movie_serializer_class = "#{transform_type}_movie_serializer".classify.constantize - our_json = movie_serializer_class.new([movie]).serialized_json - ams_json = ActiveModelSerializers::SerializableResource.new([ams_movie], key_transform: transform_type).to_json - expect(our_json.length).to eq (ams_json.length) - end - end - end - -end diff --git a/spec/shared/examples/object_serializer_class_methods_examples.rb b/spec/shared/examples/object_serializer_class_methods_examples.rb index 4a96e56..c529dcb 100644 --- a/spec/shared/examples/object_serializer_class_methods_examples.rb +++ b/spec/shared/examples/object_serializer_class_methods_examples.rb @@ -7,3 +7,12 @@ RSpec.shared_examples 'returning correct relationship hash' do |serializer, id_m expect(relationship[:record_type]).to be record_type end end + +RSpec.shared_examples 'returning key transformed hash' do |movie_type, release_year| + it 'returns correctly transformed hash' do + expect(hash[:data][0][:attributes]).to have_key(release_year) + expect(hash[:data][0][:relationships]).to have_key(movie_type) + expect(hash[:data][0][:relationships][movie_type][:data][:type]).to eq(movie_type) + expect(hash[:included][0][:type]).to eq(movie_type) + end +end