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

#default_sortingsObject



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

def default_sortings
  opts =
    @default_sortings ||
    association_class.try(:default_sortings) ||
    [{key: Vocab.schema.dateCreated, direction: :desc}]
  opts.respond_to?(:call) ? opts.call(parent) : opts
end

#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



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

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



26
27
28
# File 'app/models/linked_rails/collection/sortable.rb', line 26

def parsed_sort_values
  sortings.map(&:sort_value)
end

#primary_key_sortingObject



30
31
32
33
34
35
36
37
# File 'app/models/linked_rails/collection/sortable.rb', line 30

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

#sorted?Boolean

Returns:

  • (Boolean)


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

def sorted?
  sort.present?
end

#sorted_association(scope) ⇒ Object



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

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

#sortingsObject



51
52
53
54
55
56
57
# File 'app/models/linked_rails/collection/sortable.rb', line 51

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