Method: Nodes::ApplicationController#sort

Defined in:
app/controllers/nodes/application_controller.rb

#sortObject

ASC | DESC



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/nodes/application_controller.rb', line 15

def sort
  if session[:sort] == params[:field] #change sort direction
    if session[:sort_direction] == 'DESC'
      session[:sort_direction] = 'ASC'
    else
      session[:sort_direction] = 'DESC'
    end
  else # create new sort direction
    session[:sort] = params[:field]
    session[:sort_direction] = 'ASC'
  end

  list = @model.all
  if session[:sort_direction] == 'ASC'
    list.to_a.sort! do |one, two|
      a = one.send(session[:sort])
      b = two.send(session[:sort])
      (a and b) ? a <=> b : (a ? -1 : 1)
    end
  else
    list.to_a.sort! do |one, two|
      a = one.send(session[:sort])
      b = two.send(session[:sort])
      (b and a) ? b <=> a : (b ? -1 : 1)
    end
  end
  instance_variable_set("@#{@controller}", list)
  render action: :index
end