JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Strategy for grouped resources

Open
#840 3 comments 1 reaction 0 assignees View on GitHub

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.