module Outpost::Controller::Ordering

Public Instance Methods

order() click to toggle source

Public: The order string to be passed into ActiveRecord

Examples

order
# => "updated_at DESC"

Returns String of the order.

# File lib/outpost/controller/ordering.rb, line 41
def order
  @order ||= "#{order_attribute} #{order_direction}"
end
order_attribute() click to toggle source

Public: The order that the records are currently sorted by. This gets passed directly to ActiveRecord, so it should be a valid database order.

Returns String of the order.

# File lib/outpost/controller/ordering.rb, line 15
def order_attribute
  @order_attribute ||= set_order_attribute
end
order_direction() click to toggle source

Public: The sort mode that the records are currently sorted by. This gets passed directory to ActiveRecord, so it should be a valid database sort mode.

Examples

order_direction
# => "ASC"

Returns String of the sort mode.

# File lib/outpost/controller/ordering.rb, line 29
def order_direction
  @order_direction ||= set_order_direction
end

Private Instance Methods

set_order_attribute() click to toggle source

Set which attribute is doing the sorting

If params is present, then set the preference to that and return it.

If not present, but preferred order is, then use the preferred order.

Otherwise use the list order.

# File lib/outpost/controller/ordering.rb, line 57
def set_order_attribute
  key = "#{model.content_key}_order_attribute"
  preferred_order = preference(key)

  @order_attribute = if params[:order].present?
    set_preference(key, params[:order])
  elsif preferred_order.present?
    preferred_order
  else
    list.default_order_attribute
  end
end
set_order_direction() click to toggle source

Set the order direction

It will either be the requested direction, or if not available, then the table's default direction.

# File lib/outpost/controller/ordering.rb, line 74
def set_order_direction
  key = "#{model.content_key}_order_direction"
  preferred_direction = preference(key)

  @order_direction = if [Outpost::ASCENDING, Outpost::DESCENDING]
  .include?(params[:direction])
    set_preference(key, params[:direction])
  elsif preferred_direction.present?
    preferred_direction
  else
    list.default_order_direction
  end
end