Skip to content

Commit

Permalink
Add test for same exposures with different options using and if
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Lyzo committed Apr 24, 2014
1 parent 112a9f4 commit b73d3d9
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,65 @@ class Parent < Person
subject.represent(object).send(:value_for, :size).should == object.class.to_s
end
end

context 'when same exposures with different options using and if' do
it 'should generate right result' do
class PhotoEntity < Grape::Entity
expose :photo_file
end

class SoundEntity < Grape::Entity
expose :sound_file
end

class MediaEntity < Grape::Entity
expose :media, using: SoundEntity, if: lambda { |object, _| object.sound? }
expose :media, using: PhotoEntity, if: lambda { |object, _| object.photo? }
end

class Photo
def photo_file
'photo_file'
end
end

class Sound
def sound_file
'sound_file'
end
end

class Media
attr_accessor :media

def initialize(media)
self.media = media
end

def photo?
media.is_a? Photo
end

def sound?
media.is_a? Sound
end
end

MediaEntity.represent([Media.new(Sound.new), Media.new(Photo.new)], serializable: true).should == [
{
media: {
sound_file: 'sound_file'
}
},
{
media: {
photo_file: 'photo_file'
}
}
]
end
end

end

describe '.with_options' do
Expand Down

0 comments on commit b73d3d9

Please sign in to comment.