Support scoped ActiveRecord associations when joining for sorting
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 25/100
Rechercherichtung
Beginne in lib/jsonapi/active_relation_resource_finder.rb, insbesondere bei _build_joins in den referenzierten Zeilen, und vergleiche die vorgeschlagene Struktur mit dem verlinkten WIP PR. Überprüfe, wie gescopte ActiveRecord-Assoziationen und längere Assoziationsketten dargestellt werden sollten, und bewerte anschließend die genannte Sorge bezüglich Aliasing und Breaking Change. Als abgeschlossen gilt die Aufgabe, wenn das Sortieren durch gescopte Assoziationen deren Scope mit der vorgesehenen Join-Struktur bewahrt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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.
- Vorherrschende Sprache
- Ruby
- Sterne
- 2.3k
- Forks
- 547
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Kein Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus JSONAPI-Resources/jsonapi-resources
-
Fix incorrect translation key for parameter_not_allowed.detailEvtl. vergeben @pozzer hat das vor 596 Tagen übernommen. Offen
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 85/100
-
ArgumentError with Rails 8.1Evtl. vergeben @SAY-5 hat das vor 53 Tagen übernommen. Offen
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 50/100
JSONAPI-Resources/jsonapi-resources#1488 · 1 Kommentar ·
-
Rails 8.1: jsonapi_resources DSL fails to generate relationship routes due to Resource#initialize signature changeEvtl. wieder frei Ein Pull Request dazu wurde ohne Merge geschlossen. Offen
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 42/100
JSONAPI-Resources/jsonapi-resources#1479 · 2 Kommentare ·
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 30/100
JSONAPI-Resources/jsonapi-resources#1477 · 3 Kommentare · 1 Reaktion ·
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 45/100
Alle Issues in JSONAPI-Resources/jsonapi-resources
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
Maintainer antworten meist innerhalb von 4 Tagen
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 66/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
-
ds-drift
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
we-promise/sure#4266 ·
Maintainer antworten meist innerhalb von 1 Tag
-
:bank: debt
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
cucumber/cucumber-ruby-core#346 · 1 Kommentar ·