Skip to content

Commit

Permalink
Adds test on proxying has_many through polymorphic
Browse files Browse the repository at this point in the history
  • Loading branch information
zedtux committed May 31, 2022
1 parent e49ce0f commit 5d5f281
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
20 changes: 20 additions & 0 deletions spec/integration/associations/belongs_to_polymorphic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@
end
end

context 'proxying a has_many association through a polymorphic association' do
it 'returns the associated documents' do
event = Event.create(restaurant: restaurant)

picture = Picture.create(imageable: event, mime: 'image/png')
guillaume = IdentifiedPerson.create(picture: picture,
fullname: 'Guillaume Briat')
alexandre = IdentifiedPerson.create(picture: picture,
fullname: 'Alexandre Astier')

picture = Picture.create(imageable: event, mime: 'image/png')
lionnel = IdentifiedPerson.create(picture: picture,
fullname: 'Lionnel Astier')
franck = IdentifiedPerson.create(picture: picture,
fullname: 'Franck Pitiot')

expect(event.people.to_a).to eql([guillaume, alexandre, lionnel, franck])
end
end

context 'joining on the imageable belongs_to' do
it 'fails' do
expect do
Expand Down
13 changes: 12 additions & 1 deletion spec/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ def color=(value)
end

def load_belongs_to_polymorphic_models
define_class :IdentifiedPerson do
include NoBrainer::Document

field :fullname

belongs_to :picture
end

define_class :Image do
include NoBrainer::Document

Expand All @@ -171,13 +179,16 @@ def load_belongs_to_polymorphic_models

define_class :Picture, Image do
include NoBrainer::Document

has_many :people, class_name: 'IdentifiedPerson'
end

define_class :Event do
include NoBrainer::Document

belongs_to :restaurant
has_many :photos, as: :imageable, class_name: 'Picture'
has_many :photos, class_name: 'Picture', as: :imageable
has_many :people, through: :photos
end

define_class :Restaurant do
Expand Down

0 comments on commit 5d5f281

Please sign in to comment.