JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources
Could _add_relationship generated methods use resource_for_model instead of resource_for?
まだ誰も着手していません。
- 主要言語
- Ruby
- スター
- 2.3k
- フォーク
- 546
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
In an application I'm working on, we had an issue where we had a model with a polymorphic belongs to association, that used different resource subclasses based on the type of the association.
For the basic index, show, etc. actions this worked fine (you can override resource_type_for or use model hints), but when we tried to access one of these models using a get_related_resource route, we found that it used the superclass resource.
This seems to be because of this method in resource.rb:
define_method relationship_name do |options = {}|
relationship = self.class._relationships[relationship_name]
if relationship.polymorphic?
associated_model = public_send(associated_records_method_name)
resource_klass = self.class.resource_for_model(associated_model) if associated_model
return resource_klass.new(associated_model, @context) if resource_klass
else
resource_klass = relationship.resource_klass
if resource_klass
associated_model = public_send(associated_records_method_name)
return associated_model ? resource_klass.new(associated_model, @context) : nil
end
end
end unless method_defined?(relationship_name)
Using relationship.resource_klass looks up the resource class using the class of the model, which doesn't allow for the resource class to be changed based on attributes of the model. At this point, the model instance is known, so it should be possible to adapt it to look up the resource class based on the model instance instead of the model class.
Currently, we're getting around this by overriding .new on the superclass relationship like this, but it's pretty hacky:
def self.new(model, *args)
return super if self < V1::QuestionResource || model.content_type.nil?
resource_for_model(model).new(model, *args)
end
Is this something there'd be interest in changing?
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
lib/jsonapi/resource.rb にある、issue に示された生成されたリレーションメソッドから始め、ポリモーフィック分岐と非ポリモーフィック分岐を比較します。get_related_resource がこのメソッドに到達する経路を追跡し、既存のリレーション動作を壊すことなく、モデルインスタンスが意図したリソースサブクラスを選択できることを確認します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- ruby
- 領域
- api, backend
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100