JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Support scoped ActiveRecord associations when joining for sorting

未关闭
#1,155 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 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 摘要。