Class: LinkedRails::Collection::Sorting

Inherits:
RDF::Node
  • Object
show all
Includes:
ActiveModel::Model, Model
Defined in:
app/models/linked_rails/collection/sorting.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

#build_child, #singular_resource?

Methods included from Model::Serialization

#preview_includes, #show_includes

Methods included from Model::Iri

#anonymous_iri, #anonymous_iri?, #iri_opts, #rdf_type, #reload, #root_relative_iri, #route_fragment

Methods included from Model::Enhancements

#enhanced_with?

Methods included from Model::Dirty

#previous_changes_by_predicate, #previously_changed_relations

Methods included from Model::Collections

#collection_for, #parent_collections

Instance Attribute Details

#association_classObject

Returns the value of attribute association_class.



13
14
15
# File 'app/models/linked_rails/collection/sorting.rb', line 13

def association_class
  @association_class
end

#collectionObject

Returns the value of attribute collection.



13
14
15
# File 'app/models/linked_rails/collection/sorting.rb', line 13

def collection
  @collection
end

#directionObject

Returns the value of attribute direction.



13
14
15
# File 'app/models/linked_rails/collection/sorting.rb', line 13

def direction
  @direction
end

#keyObject

Returns the value of attribute key.



13
14
15
# File 'app/models/linked_rails/collection/sorting.rb', line 13

def key
  @key
end

Class Method Details

.from_array(association_class, array, collection) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'app/models/linked_rails/collection/sorting.rb', line 59

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

Instance Method Details

#attribute_nameObject



15
16
17
# File 'app/models/linked_rails/collection/sorting.rb', line 15

def attribute_name
  @attribute_name ||= key_from_mapping || :created_at
end

#default_valueObject

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/linked_rails/collection/sorting.rb', line 20

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

#iri(_opts = {}) ⇒ Object

rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength



36
37
38
# File 'app/models/linked_rails/collection/sorting.rb', line 36

def iri(_opts = {})
  self
end

#sort_directionObject



40
41
42
# File 'app/models/linked_rails/collection/sorting.rb', line 40

def sort_direction
  direction == :desc ? :lt : :gt
end

#sort_valueObject



44
45
46
# File 'app/models/linked_rails/collection/sorting.rb', line 44

def sort_value
  {attribute_name => direction}
end