Sha256: cb9e444489ff846238c004a75ba00d1a179ff71aba5a0c390052b337d894f55f
Contents?: true
Size: 1.95 KB
Versions: 36
Compression:
Stored size: 1.95 KB
Contents
# frozen_string_literal: true module LinkedRails class Collection class Sorting < RDF::Node include ActiveModel::Model include LinkedRails::Model DATE_MIN = Date.new(1970, 1, 1) DATE_TIME_MIN = Time.new(1970, 1, 1).utc STRING_MIN = ('0'.ord - 1).chr STRING_MAX = ('z'.ord + 1).chr attr_accessor :association_class, :direction, :key, :collection def attribute_name @attribute_name ||= key_from_mapping || :created_at end # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength def default_value column = association_class.column_for_attribute(attribute_name) case column.type when :date direction == :asc ? DATE_MIN : Date.current when :datetime (direction == :asc ? DATE_TIME_MIN : Time.current.utc).iso8601(6) when :string, :text direction == :asc ? STRING_MIN : STRING_MAX else ActiveModel::Type::Integer.new(limit: column.limit).send(direction == :asc ? :min_value : :max_value) end end # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength def iri(**_opts) self end def sort_direction direction == :desc ? :lt : :gt end def sort_value {attribute_name => direction} end private def key_from_mapping if key == Vocab.ontola[:primaryKey] association_class.try(:primary_key) else association_class.try(:predicate_mapping).try(:[], key)&.key end end class << self def from_array(association_class, array, collection) array&.map do |sort| new( collection: collection, association_class: association_class, direction: sort[:direction]&.to_sym, key: sort[:key] ) end end end end end end
Version data entries
36 entries across 36 versions & 1 rubygems