Skip to content

Commit a45b85d

Browse files
committed
test: failing request posting sti with polymorphic has one
1 parent 76ef777 commit a45b85d

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

test/fixtures/active_record.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@
275275
t.string :drive_layout
276276
t.string :serial_number
277277
t.integer :person_id
278+
t.references :imageable, polymorphic: true, index: true
278279
t.timestamps null: false
279280
end
280281

@@ -734,6 +735,9 @@ class Picture < ActiveRecord::Base
734735

735736
class Vehicle < ActiveRecord::Base
736737
belongs_to :person
738+
belongs_to :imageable, polymorphic: true
739+
# belongs_to :document, -> { where( pictures: { imageable_type: 'Document' } ) }, foreign_key: 'imageable_id'
740+
# belongs_to :product, -> { where( pictures: { imageable_type: 'Product' } ) }, foreign_key: 'imageable_id'
737741
end
738742

739743
class Car < Vehicle
@@ -743,13 +747,13 @@ class Boat < Vehicle
743747
end
744748

745749
class Document < ActiveRecord::Base
746-
has_many :pictures, as: :imageable
750+
has_many :pictures, as: :imageable # polymorphic
747751
belongs_to :author, class_name: 'Person', foreign_key: 'author_id'
748752
has_one :file_properties, as: :fileable
749753
end
750754

751755
class Product < ActiveRecord::Base
752-
has_many :pictures, as: :imageable
756+
has_many :pictures, as: :imageable # polymorphic
753757
belongs_to :designer, class_name: 'Person', foreign_key: 'designer_id'
754758
has_one :file_properties, as: :fileable
755759
end
@@ -1336,6 +1340,7 @@ class VehicleResource < JSONAPI::Resource
13361340
immutable
13371341

13381342
has_one :person
1343+
has_one :imageable, polymorphic: true
13391344
attributes :make, :model, :serial_number
13401345
end
13411346

@@ -1915,6 +1920,8 @@ class PreferencesResource < PreferencesResource; end
19151920
class SectionResource < SectionResource; end
19161921
class TagResource < TagResource; end
19171922
class CommentResource < CommentResource; end
1923+
class DocumentResource < DocumentResource; end
1924+
class ProductResource < ProductResource; end
19181925
class VehicleResource < VehicleResource; end
19191926
class CarResource < CarResource; end
19201927
class BoatResource < BoatResource; end

test/integration/requests/request_test.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,47 @@ def test_post_polymorphic_with_has_many_relationship
359359
assert_equal Car, person.vehicles.fourth.class
360360
end
361361

362+
def test_post_sti_polymorphic_with_has_one_relationship
363+
post '/vehicles', params:
364+
{
365+
'data' => {
366+
'type' => 'cars',
367+
'attributes' => {
368+
'make' => 'Mazda',
369+
'model' => 'Miata MX5',
370+
'drive_layout' => 'Front Engine RWD',
371+
'serial_number' => '32432adfsfdysua',
372+
},
373+
'relationships' => {
374+
'person' => {
375+
'data' => {
376+
'type' => 'people', 'id' => '1001',
377+
}
378+
},
379+
'picture' => {
380+
'data' => {
381+
'type' => 'products', 'id' => '1',
382+
}
383+
},
384+
}
385+
}
386+
}.to_json,
387+
headers: {
388+
'CONTENT_TYPE' => JSONAPI::MEDIA_TYPE,
389+
'Accept' => JSONAPI::MEDIA_TYPE
390+
}
391+
392+
assert_jsonapi_response 201
393+
394+
body = JSON.parse(response.body)
395+
car = Vehicle.find(body.dig("data", "id"))
396+
397+
assert_equal "Card", car.type
398+
assert_equal "Mazda", car.make
399+
assert_equal Product, car.picture.class
400+
assert_equal Person, car.person.class
401+
end
402+
362403
def test_post_polymorphic_invalid_with_wrong_type
363404
post '/people', params:
364405
{

0 commit comments

Comments
 (0)