JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources
Strategy for grouped resources
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 2.3k
- Forks
- 546
- PR merge metrics
- No merged PRs in 30d
Description
I realize this isn't exclusively a JR question, but I feel it's too common to not bring it up and how one might accomplish it using JR.
My current use case has a type of model that's the basis for summed queries grouped by one or more attributes. In this case, it's a timesheet entry, and I initially need to group them by user_id and project_id, but they could easily be grouped by time periods as well. The aggregated value is SUM(hours). I've created an TimesheetEntryCollection model as an ActiveModel::Model object to handle the aggregated data, but I'm unsure how to serialize them, as JR expects and ActiveRecord::Relation to be returned from the Resource's self.records method and I'm not sure how to force a relation out of a tableless model. Do I just need to override the controller methods, or is there some way to do this through the resource?
Am I thinking about this the wrong way? Should JR maybe allow arrays to be returned from self.records with the understanding that you have to handle pagination/filtering yourself?
Any help would be greatly appreciated!
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Resource's self.records contract and the controller methods mentioned in the report, then trace how JR serializes an ActiveRecord::Relation. Compare that path with the tableless ActiveModel::Model TimesheetEntryCollection and grouped SUM(hours) use case. Done requires an agreed approach for grouped resources, including whether arrays and caller-managed pagination or filtering are supported.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rails, ruby
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100