Class: TheGrid::Api::Command::Sort

Inherits:
TheGrid::Api::Command show all
Defined in:
lib/the_grid/api/command/sort.rb

Instance Method Summary collapse

Methods inherited from TheGrid::Api::Command

#batch?, build, #contextualize, #execute_on, find, register_lookup_scope, scopes

Instance Method Details

#configure(relation, params) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/the_grid/api/command/sort.rb', line 3

def configure(relation, params)
  {}.tap do |o|
    o[:field] = params[:field]
    o[:field] = "#{relation.table_name}.#{o[:field]}" if relation.column_names.include?(params[:field])

    o[:order] = params[:order]
    o[:order] = 'asc' unless %w{ asc desc }.include?(o[:order])
  end
end

#run_on(relation, params) ⇒ Object



13
14
15
16
# File 'lib/the_grid/api/command/sort.rb', line 13

def run_on(relation, params)
  return relation if params[:field].blank?
  relation.order("#{params[:field]} #{params[:order]}")
end