Sha256: f340251c3a8f17ae58e3a5d62c7eff56f8df46c8791cd05ba04033baebb7378c

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

module JsonapiCompliable
  # Apply sorting logic to the scope.
  #
  # By default, sorting comes 'for free'. To specify a custom sorting proc:
  #
  #   class PostResource < ApplicationResource
  #     sort do |scope, att, dir|
  #       int = dir == :desc ? -1 : 1
  #       scope.sort_by { |x| x[att] * int }
  #     end
  #   end
  #
  # The sorting proc will be called once for each sort att/dir requested.
  # @see Resource.sort
  class Scoping::Sort < Scoping::Base
    # @return [Proc, Nil] The custom proc specified by Resource DSL
    def custom_scope
      resource.sort_all
    end

    # Apply default scope logic via Resource adapter
    # @return the scope we are chaining/modifying
    def apply_standard_scope
      each_sort do |attribute, direction|
        if sort = resource.sorts[attribute]
          @scope = sort.call(@scope, direction)
        else
          @scope = resource.adapter.order(@scope, attribute, direction)
        end
      end
      @scope
    end

    # Apply custom scoping proc configured via Resource DSL
    # @return the scope we are chaining/modifying
    def apply_custom_scope
      each_sort do |attribute, direction|
        @scope = custom_scope
          .call(@scope, attribute, direction, resource.context)
      end
      @scope
    end

    private

    def each_sort
      sort_param.each do |sort_hash|
        attribute = sort_hash.keys.first
        direction = sort_hash.values.first
        yield attribute, direction
      end
    end

    def sort_param
      @sort_param ||= begin
        if query_hash[:sort].blank?
          resource.default_sort
        else
          query_hash[:sort]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jsonapi_compliable-1.0.alpha.2 lib/jsonapi_compliable/scoping/sort.rb