Method: Order#orderable

Defined in:
lib/order.rb

#orderable(*attribute_names) ⇒ Object #orderable(attribute_map) ⇒ Object #orderable(order_name, &block) ⇒ Object

Generates order_by_* scopes used for ordering resulting records.

Examples:

Custom ordering strategy

orderable(:category_name){ |direction| joins(:category).order "category.name #{direction}" }

Overloads:

  • #orderable(*attribute_names) ⇒ Object

    Generates simple ordering scopes that can accept a direction such as “desc”

    Parameters:

    • of (Array)

      attribute names

  • #orderable(attribute_map) ⇒ Object

    Generates simple ordering scope using aliased mapping to one or more attributes

    Parameters:

    • mapping (Hash)

      ordering name to attribute(s)

  • #orderable(order_name, &block) ⇒ Object

    Generates an ordering scope using a custom strategy specified by the given block



27
28
29
30
31
32
33
34
35
36
# File 'lib/order.rb', line 27

def orderable(*attributes, &block)
  if block_given?
    raise ArgumentError.new('Cannot provide mapped naming for custom ordering strategy') if attributes.first.is_a? Hash
    order_scope attributes.first, &block
  elsif attributes.first.is_a? Hash
    attributes.first.each{ |name, attribute| order_scope name, attribute }
  else
    attributes.each{ |attribute| order_scope attribute }
  end
end