tiagopog/jsonapi-utils

Support for page_count in response meta field

ychaker opened this issue · 8 comments

Howdy,
first I'd like to thank you for your good work.

jsonapi-resources have a config to show both the record count and the page count. it would be nice to be able to support both, and the flexibility of choosing the key name:

  # Metadata
  # Output record count in top level meta for find operation
  config.top_level_meta_include_record_count = true
  config.top_level_meta_record_count_key     = :record_count
  config.top_level_meta_include_page_count   = true
  config.top_level_meta_page_count_key       = :page_count

the line I found useful in the original gem is the following:

https://github.com/cerebris/jsonapi-resources/blob/0eaa40c509a21e2d4cf02a52e6139fe30465260b/lib/jsonapi/processor.rb#L94

what I did in the meantime is override the module like so:

module JSONAPI
  module Utils
    module Response
      module Formatters
        private

        def calculate_page_count record_count
          (record_count / paginator.size.to_f).ceil
        end

        # Overriding method to also return page count in the meta response
        def result_options records, options
          {}.tap do |data|
            if JSONAPI.configuration.default_paginator != :none &&
              JSONAPI.configuration.top_level_links_include_pagination
              data[:pagination_params] = pagination_params(records, options)
            end

            if JSONAPI.configuration.top_level_meta_include_record_count
              data[:record_count] = count_records(records, options)
              data[:page_count]   = calculate_page_count(data[:record_count])
            end
          end
        end
      end
    end
  end
end

would be nice to have this baked in to the gem instead of having to do an override.

Thanks!

Howdy, @ychaker!

Good catch! I would bring that simple feature to the gem but I'm totally out of time lately. Can you provide a PR with that implementation + specs?

Cheers!

@tiagopog should I match what jsonapi-resources does, ie support the same configurations and make the implementation copy theirs, or would my simple hack suffice?

and are you aware of any other potential parts of the code that would need to be update accordingly?

I see no problem on implementing this simpler version now. So you can stick with your hack since it achieves the expected results (specs will ensure that).

Implementing this page count on the formatter level, as you proposed on your hack, will spread the feature across the renders, so that the collection rendering should display it out of the box.

@ychaker, are you still considering to implement this feature?

hi @tiagopog I was on vacation last week.
I might take a stab at it later this week if you're still interested and haven't already done so yourself.

I'm interested for sure :-)

@tiagopog Would you need help merging the associated PR at all?

Closed by #87