JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Polymorphic code in serializer makes some AR assumptions

オープン
#829 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Ruby
スター
2.3k
フォーク
546
PR マージ指標
30日以内にマージされた PR はありません

説明

In the following code in resource_serializer.rb


    def foreign_key_types_and_values(source, relationship)
      if relationship.is_a?(JSONAPI::Relationship::ToMany)
        if relationship.polymorphic?
          assoc = source._model.public_send(relationship.name)
          # Avoid hitting the database again for values already pre-loaded
          if assoc.respond_to?(:loaded?) and assoc.loaded?
            assoc.map do |obj|
              [obj.type.underscore.pluralize, @id_formatter.format(obj.id)]
            end
          else
            assoc.pluck(:type, :id).map do |type, id|
              [type.underscore.pluralize, @id_formatter.format(id)]
            end
          end
        else
          source.public_send(relationship.name).map do |value|
            [relationship.type, @id_formatter.format(value.id)]
          end
        end
      end
    end

It makes the assumption that if you specify a polymorphic relationship that it should get the data straight from the model rather than allowing this to be changed via the resource.
It also assumes that we are using "id" and "type" which is very much an active record thing.

In my application, I am using active record, but have a "has_many" relationship to a class which then has a has_many polymorphic. I effectively want to do a has_many through the middle class straight to the polymorphic data which I cannot find a way of doing.

If the above code allowed the resource to intervene here, I could have overriden something.

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

resource_serializer.rb の foreign_key_types_and_values から始め、ポリモーフィックな関連がどのように値を取得するかを追跡します。Active Record の id/type に関する前提を避けるために必要なリソースレベルの拡張ポイントを特定し、その後、シリアライザーがモデルを直接問い合わせることなく、リソースがポリモーフィックな has-many-through 関連を提供できることを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
rails, ruby
領域
api, backend
issue の種類
リファクタリング
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。