JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Support scoped ActiveRecord associations when joining for sorting

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

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

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

説明

This issue is a (choose one):

  • Problem/bug report.
  • Feature request.
  • Request for support. Note: Please try to avoid submitting issues for support requests. Use Gitter instead.

Checklist before submitting:

  • I've searched for an existing issue.
  • I've asked my question on Gitter and have not received a satisfactory answer.
  • I've included a complete bug report template. This step helps us and allows us to see the bug without trying to reproduce the problem from your description. It helps you because you will frequently detect if it's a problem specific to your project.
  • The feature I'm asking for is compliant with the JSON:API spec.

Description

Features:

It would be good to support scoped ActiveRecord associations when resources are queried for sorting.

Example
class Contact < ApplicationRecord
  has_many :addresses
  has_one :primary_address, -> { where(primary: true) }, class_name: 'Address'
end

class Address < ApplicationRecord
  belongs_to :contact
end

class ContactResource < JSONAPI::Resource
  has_one :primary_address, class_name: 'Address'

  class << self
      def sortable_fields(context)
        super + %i[primary_address.street]
      end
    end
end

If contact is queried with sort=primary_address.street, the resulting query on the current master branch would ignore the scope of the association.

The SQL query would look something like this:

SELECT  "contacts".* FROM contacts LEFT JOIN addresses AS primary_address_sorting ON primary_address_sorting.contact_id = contacts.id ORDER BY primary_address_sorting.street asc 

My proposal would be to use the native syntax of ActiveRecord's joins instead of the current stitching of queries. Thus, _build_joins(https://github.com/cerebris/jsonapi-resources/blob/5dfeb0f741e3fc98ee9786f7eb182b15f391ca63/lib/jsonapi/active_relation_resource_finder.rb#L431-L446) would return something like [:primary_address]. For longer association chains, it would return something like [:book => [:author => [:address]]].

You can find a WIP PR for this here: https://github.com/HQTrust/jsonapi-resources/pull/1

I'd be very happy to get your feedback on this to eventually create an upstream PR. Because of the approach to structure the joins, I don't think it is possible to alias them as #{association.name}_sorting anymore, which would make this a breaking change for some users. If you have an example for this, maybe you can provide me with one so I can better understand the impact.

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

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

はじめの一歩

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

調査の方向性

lib/jsonapi/active_relation_resource_finder.rb、特に参照されている行の _build_joins から始め、提案された構造をリンク先の WIP PR と比較してください。scope 付きの ActiveRecord アソシエーションと、より長いアソシエーションチェーンをどのように表現すべきかを確認し、そのうえで、示されているエイリアシングと breaking change に関する懸念を評価してください。scope 付きアソシエーションを通じたソートが、意図した join 構造でその scope を保持すれば完了です。

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

評価

技術スタック
rails, ruby
領域
api, backend
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

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

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