Module: Angsa::Sorting

Included in:
Base
Defined in:
lib/angsa/sorting.rb

Instance Method Summary collapse

Instance Method Details

#apply_sorting(records, params) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/angsa/sorting.rb', line 3

def apply_sorting(records, params)
  if params[:order] && params[:dir]
    sort = columns.find { |column| column[:name].to_s == params[:order] }
    if sort
      if sort[:source].include?('.')
        # This is an attribute of an associated model
        association, attribute = sort[:source].split('.')
        records = records.joins(association.to_sym).order("#{association.pluralize}.#{attribute} #{params[:dir]}")
      else
        # This is an attribute of the Course model
        records = records.order("#{sort[:source]} #{params[:dir]}")
      end
    end
  end
  records
end