Class: Dorsale::Flyboy::TasksSorter

Inherits:
Agilibox::Sorter
  • Object
show all
Defined in:
app/sorters/dorsale/flyboy/tasks_sorter.rb

Instance Method Summary collapse

Instance Method Details

#callObject



33
34
35
36
37
38
39
40
41
42
43
# File 'app/sorters/dorsale/flyboy/tasks_sorter.rb', line 33

def call
  order = sort

  if order.is_a?(Proc) # sorting by a polymorphic attribute
    result = @collection.sort(&order)
  else
    result = @collection.reorder(order)
  end

  result
end

#sortObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/sorters/dorsale/flyboy/tasks_sorter.rb', line 2

def sort
  case column
  when :name, :status
    %(LOWER(dorsale_flyboy_tasks.#{column}) #{direction})
  when :progress, :term
    %(dorsale_flyboy_tasks.#{column} #{direction})
  when :taskable
    if direction == :asc
      proc { |a, b| a.taskable.to_s.downcase <=> b.taskable.to_s.downcase }
    else
      proc { |a, b| b.taskable.to_s.downcase <=> a.taskable.to_s.downcase }
    end
  when :tags
    %(
      (
        SELECT STRING_AGG(n, ' ' ORDER BY n)
        FROM (
          SELECT name AS n
          FROM tags
          WHERE id IN (
            SELECT taggings.tag_id
            FROM taggings
            WHERE taggable_id = dorsale_flyboy_tasks.id
            AND taggings.taggable_type = '#{Dorsale::Flyboy::Task}'
          )
        ) AS task_tags
      ) #{direction}
    )
  end
end