Sha256: 71135f53eb6ea8a433628b53c83cbb0784e0a893b9a0716477417cf860cb36c0

Contents?: true

Size: 574 Bytes

Versions: 5

Compression:

Stored size: 574 Bytes

Contents

# frozen_string_literal: true

module Motor
  module ApiQuery
    module Sort
      FIELD_PARSE_REGEXP = /\A(-)?(.*)\z/.freeze

      module_function

      def call(rel, params)
        return rel if params.blank?

        normalized_params = build_params(params)

        rel.order(normalized_params)
      end

      def build_params(param)
        param.split(',').each_with_object({}) do |field, hash|
          direction, name = field.match(FIELD_PARSE_REGEXP).captures

          hash[name] = direction.present? ? :desc : :asc
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
motor-admin-0.1.13 lib/motor/api_query/sort.rb
motor-admin-0.1.12 lib/motor/api_query/sort.rb
motor-admin-0.1.11 lib/motor/api_query/sort.rb
motor-admin-0.1.10 lib/motor/api_query/sort.rb
motor-admin-0.1.9 lib/motor/api_query/sort.rb