Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow decorating objects after pagination (#91)
* Rubocop fix: Use filter_map instead of map { ... }.compact Currently, builds are failing because of Rubocop complaining about these three lines. * Fix specs with Ransack 4.0 Ransack 4 requires explicitly allowlisting ransackable attributes and associations for each model. This causes the specs to fail with the most recent Ransack version. This fixes those spec failures. * Allow decorating objects after pagination This simplification allows decorating objects after they are paginated, without losing the correct total object count. I'm using an instance variable on the including controller here, because the decorating the paginated collection will have us lose the instance variable we set on it. Here's the case where this happens: We have a complex ActiveRecord collection that we run through Ransack and Kaminari, but before rendering we want to convert each object in it using a `SimpleDelegator`. Here's a simplified version of the controller action we're looking at: ``` class UserDecorator < SimpleDelegator def fantastic_for_rendering "Whoah" end end def index allowed_fields = [ :first_name, :last_name, :created_at, :notes_created_at, :notes_quantity ] options = { sort_with_expressions: true } jsonapi_filter(User.all, allowed_fields, options) do |filtered| result = filtered.result jsonapi_paginate(result) do |paginated| paginated = paginated.map { |user| UserDecorator.new() } render jsonapi: paginated end end end ``` --------- Co-authored-by: Stas <[email protected]>
- Loading branch information