Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for sorting and pagination from JR #95

Open
RoM4iK opened this issue Feb 4, 2019 · 0 comments
Open

Support for sorting and pagination from JR #95

RoM4iK opened this issue Feb 4, 2019 · 0 comments

Comments

@RoM4iK
Copy link

RoM4iK commented Feb 4, 2019

Since https://github.com/cerebris/jsonapi-resources/releases/tag/v0.9.4 JR allows to filter records on nested relationship values while JU allows filtering only by where condition.

Are there any reasons why you decided to rewrite filter/sort/pagination workflow?

I've played a little with monkey patching, and looks like using filters from JR is possible.

Here is my code, it's working within my application.
Note: it breaks core functionality for usage with non-AR objects, and can break something else, be careful if you want to use it.

module JSONAPI::Utils
  module Response
    module Formatters
      def jsonapi_format(object, options = {})
        operations = @request.operations
        unless JSONAPI.configuration.resource_cache.nil?
          operations.each {|op| op.options[:cache_serializer] = resource_serializer }
        end
        if object.respond_to?(:to_ary)
          operations.each { |op| op.options[:context][:records] = object }
          results = process_operations(operations)
        else
          results = JSONAPI::OperationResults.new
          record = turn_into_resource(object, options)
          results.add_result(JSONAPI::ResourceOperationResult.new(:ok, record))
        end
        @_response_document = create_response_document(results)
        @_response_document.contents
      end
    end
  end
end


module JSONAPI
  class Resource
    class << self
      def records(options = {})
        options.dig(:context, :records) || _model_class.all
      end

      def apply_included_resources_filters(records, options = {})
        include_directives = options[:include_directives]
        return records unless include_directives
        related_directives = include_directives.include_directives.fetch(:include_related)
        related_directives.reduce(records) do |memo, (relationship_name, config)|
          relationship = _relationship(relationship_name)
          next memo unless relationship
          filtering_resource = relationship.resource_klass

          # Don't try to merge where clauses when relation isn't already being joined to query.
          next memo unless config[:include_in_join]

          filters = config[:include_filters]
          next memo unless filters

          rel_records = filtering_resource.apply_filters(filtering_resource.records({}), filters, options).references(relationship_name)
          memo.merge(rel_records)
        end
      end
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant