Skip to content

Commit b9de02d

Browse files
authored
Merge pull request #21 from speee/fix/rails7-polymorphic-inverse-relationship-1473
Add test for Issue JSONAPI-Resources#1473: Polymorphic has_many relationships
2 parents 953ec76 + d6066d3 commit b9de02d

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

test/fixtures/active_record.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,18 @@
429429
t.integer :version
430430
t.timestamps null: false
431431
end
432+
433+
# Tables for testing Issue #1473: Polymorphic has_many with foreign_key_on: :related
434+
create_table :articles, force: true do |t|
435+
t.string :title
436+
t.timestamps null: false
437+
end
438+
439+
create_table :article_comments, force: true do |t|
440+
t.text :content
441+
t.references :commentable, polymorphic: true, index: true
442+
t.timestamps null: false
443+
end
432444
end
433445

434446
### MODELS
@@ -868,6 +880,15 @@ class ListItem < ActiveRecord::Base
868880
belongs_to :list, inverse_of: :items
869881
end
870882

883+
# Models for testing Issue #1473: Polymorphic has_many with foreign_key_on: :related
884+
class Article < ActiveRecord::Base
885+
has_many :article_comments, as: :commentable
886+
end
887+
888+
class ArticleComment < ActiveRecord::Base
889+
belongs_to :commentable, polymorphic: true
890+
end
891+
871892
### CONTROLLERS
872893
class SessionsController < ActionController::Base
873894
include JSONAPI::ActsAsResourceController
@@ -1230,6 +1251,13 @@ class IndicatorsController < JSONAPI::ResourceController
12301251
class RobotsController < JSONAPI::ResourceController
12311252
end
12321253

1254+
# Controllers for testing Issue #1473
1255+
class ArticlesController < JSONAPI::ResourceController
1256+
end
1257+
1258+
class ArticleCommentsController < JSONAPI::ResourceController
1259+
end
1260+
12331261
### RESOURCES
12341262
class BaseResource < JSONAPI::Resource
12351263
abstract
@@ -2691,6 +2719,17 @@ class RobotResource < ::JSONAPI::Resource
26912719
end
26922720
end
26932721

2722+
# Resources for testing Issue #1473: Polymorphic has_many with foreign_key_on: :related
2723+
class ArticleResource < JSONAPI::Resource
2724+
attribute :title
2725+
has_many :article_comments, foreign_key_on: :related
2726+
end
2727+
2728+
class ArticleCommentResource < JSONAPI::Resource
2729+
attribute :content
2730+
has_one :commentable, polymorphic: true
2731+
end
2732+
26942733
# Models and Resources for testing Issue #1467: ActiveModel-based resources
26952734
# ActiveModel class without ActiveRecord
26962735
class SimpleModel

test/test_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ class CatResource < JSONAPI::Resource
340340
jsonapi_resources :employees
341341
jsonapi_resources :robots
342342

343+
# Routes for testing Issue #1473
344+
jsonapi_resources :articles
345+
jsonapi_resources :article_comments
346+
343347
jsonapi_resources :lists
344348
jsonapi_resources :list_items
345349

0 commit comments

Comments
 (0)