Module: LinkedRails::Collection::Sortable

Included in:
LinkedRails::Collection
Defined in:
app/models/linked_rails/collection/sortable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sortObject

Returns the value of attribute sort.



6
7
8
# File 'app/models/linked_rails/collection/sortable.rb', line 6

def sort
  @sort
end

#sort_optionsObject



31
32
33
# File 'app/models/linked_rails/collection/sortable.rb', line 31

def sort_options
  @sort_options || association_class.try(:sort_options, self)
end

Instance Method Details

#default_before_valueObject



9
10
11
12
13
14
15
16
# File 'app/models/linked_rails/collection/sortable.rb', line 9

def default_before_value
  sortings.map do |sorting|
    {
      key: sorting.key,
      value: sorting.default_value
    }
  end
end

#parsed_sort_valuesObject



18
19
20
# File 'app/models/linked_rails/collection/sortable.rb', line 18

def parsed_sort_values
  sortings.map(&:sort_value)
end

#primary_key_sortingObject



22
23
24
25
26
27
28
29
# File 'app/models/linked_rails/collection/sortable.rb', line 22

def primary_key_sorting
  [
    {
      key: Vocab.ontola[:primaryKey],
      direction: :asc
    }
  ]
end

#sorted?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/models/linked_rails/collection/sortable.rb', line 39

def sorted?
  sort.present?
end

#sorted_association(scope) ⇒ Object



35
36
37
# File 'app/models/linked_rails/collection/sortable.rb', line 35

def sorted_association(scope)
  scope.respond_to?(:reorder) ? scope.reorder(parsed_sort_values) : scope
end

#sortingsObject



43
44
45
46
47
48
49
# File 'app/models/linked_rails/collection/sortable.rb', line 43

def sortings
  @sortings ||= LinkedRails.collection_sorting_class.from_array(
    association_class,
    (sort || default_sortings) + primary_key_sorting,
    self
  )
end