Sha256: 6b946069eb3d2bca5cb6640d3b0355304e3350ee202c1853ea05e7eff8d221b5
Contents?: true
Size: 1.17 KB
Versions: 11
Compression:
Stored size: 1.17 KB
Contents
# frozen_string_literal: true module Motor module ApiQuery module Sort FIELD_PARSE_REGEXP = /\A(-)?(.*)\z/.freeze module_function def call(rel, param) return rel if param.blank? arel_order = build_arel_order(rel.klass, param) join_params = build_join_params(rel.klass, param) rel.order(arel_order).left_joins(join_params) end def build_join_params(_model, param) param.split(',').each_with_object({}) do |field, result| key = field[FIELD_PARSE_REGEXP, 2] *path, _ = key.split('.') path.reduce(result) do |acc, fragment| acc[fragment] = {} end end end def build_arel_order(model, param) param.split(',').map do |field| direction, key = field.match(FIELD_PARSE_REGEXP).captures *path, field = key.split('.') reflection_model = path.reduce(model) do |acc, fragment| acc.reflections[fragment].klass end arel_column = reflection_model.arel_table[field] direction.present? ? arel_column.desc : arel_column.asc end end end end end
Version data entries
11 entries across 11 versions & 1 rubygems