Class: LinkedRails::Collection::Sorting
- Inherits:
-
RDF::Node
- Object
- RDF::Node
- LinkedRails::Collection::Sorting
- 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
-
#association_class ⇒ Object
Returns the value of attribute association_class.
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#direction ⇒ Object
Returns the value of attribute direction.
-
#key ⇒ Object
Returns the value of attribute key.
Class Method Summary collapse
Instance Method Summary collapse
- #attribute_name ⇒ Object
-
#default_value ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength.
-
#iri(_opts = {}) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength.
- #sort_direction ⇒ Object
- #sort_value ⇒ Object
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
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_class ⇒ Object
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 |
#collection ⇒ Object
Returns the value of attribute collection.
13 14 15 |
# File 'app/models/linked_rails/collection/sorting.rb', line 13 def collection @collection end |
#direction ⇒ Object
Returns the value of attribute direction.
13 14 15 |
# File 'app/models/linked_rails/collection/sorting.rb', line 13 def direction @direction end |
#key ⇒ Object
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_name ⇒ Object
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_value ⇒ Object
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_direction ⇒ Object
40 41 42 |
# File 'app/models/linked_rails/collection/sorting.rb', line 40 def sort_direction direction == :desc ? :lt : :gt end |
#sort_value ⇒ Object
44 45 46 |
# File 'app/models/linked_rails/collection/sorting.rb', line 44 def sort_value {attribute_name => direction} end |