Module: Datagrid::Ordering

Included in:
Base
Defined in:
lib/datagrid/ordering.rb

Overview

Module adds support for ordering by defined columns for Datagrid.

Instance Method Summary collapse

Instance Method Details

#descending=(value) ⇒ void

This method returns an undefined value.

Specify an order direction for an order column

Parameters:

  • value (Boolean)

    specify true for descending orderorfalse` for ascending

See Also:



# File 'lib/datagrid/ordering.rb', line 55

#descending?Boolean

Returns specified order direction.

Returns:

  • (Boolean)

    specified order direction

See Also:



# File 'lib/datagrid/ordering.rb', line 61

#orderSymbol?

Returns specified order column name.

Returns:

  • (Symbol, nil)

    specified order column name

See Also:



# File 'lib/datagrid/ordering.rb', line 51

#order=(value) ⇒ void

This method returns an undefined value.

Specify a column to be used to order the grid

Examples:

class MyGrid < ApplicationGrid
  scope { User }
  column(:name)
end

grid = MyGrid.new
grid.order = :name
grid.descending = true
grid.assets # => SELECT * FROM users ORDER BY users.name DESC

Parameters:

  • value (Symbol, String)

    column name



# File 'lib/datagrid/ordering.rb', line 36

#order_columnDatagrid::Columns::Column?

Returns a column definition that is currently used to order assets.

Examples:

class MyGrid
  scope { Model }
  column(:id)
  column(:name)
end
MyGrid.new(order: "name").order_column # => #<Column name: "name", ...>

Returns:



79
80
81
# File 'lib/datagrid/ordering.rb', line 79

def order_column
  order ? column_by_name(order) : nil
end

#ordered_by?(column, desc = nil) ⇒ Boolean

Returns true if given grid is ordered by given column.

Parameters:

  • column (String, Datagrid::Columns::Column)
  • desc (nil, Boolean) (defaults to: nil)

    confirm order direction as well if specified

Returns:

  • (Boolean)

    true if given grid is ordered by given column.



86
87
88
89
# File 'lib/datagrid/ordering.rb', line 86

def ordered_by?(column, desc = nil)
  order_column == column_by_name(column) &&
    (desc.nil? || (desc ? descending? : !descending?))
end