module OrderOrder::Extensions

Constants

DEFAULT_ALPHABETICAL_COLUMN
DEFAULT_CHRONOLOGICAL_COLUMN

Public Instance Methods

alphabetical(column_or_options=DEFAULT_ALPHABETICAL_COLUMN, options={}) click to toggle source

Order records alphabetically.

@param column_or_options [String] the name of the column which represents the object’s

name, or a hash of options. Defaults to `"name"`.

@option case_sensitive [Boolean] order case-sensitively, e.g. “C” will go

before "b". True by default
# File lib/order_order/extensions.rb, line 34
def alphabetical(column_or_options=DEFAULT_ALPHABETICAL_COLUMN, options={})
  column = get_alphabetical_column(column_or_options, options)
  order("#{column} ASC")
end
chronological(column=DEFAULT_CHRONOLOGICAL_COLUMN) click to toggle source

Order records by date, with the newest records first.

@param column [String] the name of the column which represents the object’s date. Defaults to ‘created_at`

# File lib/order_order/extensions.rb, line 15
def chronological(column=DEFAULT_CHRONOLOGICAL_COLUMN)
  order("#{column} ASC")
end
reverse_alphabetical(column_or_options=DEFAULT_ALPHABETICAL_COLUMN, options={}) click to toggle source

Order records alphabetically.

@param column_or_options [String] the name of the column which represents the object’s

name, or a hash of options. Defaults to `"name"`.

@option case_sensitive [Boolean] order case-sensitively, e.g. “C” will go

after "b". True by default
# File lib/order_order/extensions.rb, line 46
def reverse_alphabetical(column_or_options=DEFAULT_ALPHABETICAL_COLUMN, options={})
  column = get_alphabetical_column(column_or_options, options)
  order("#{column} DESC")
end
reverse_chronological(column=DEFAULT_CHRONOLOGICAL_COLUMN) click to toggle source

Order records by date, with the oldest records first.

@param column [String] the name of the column which represents the object’s date. Defaults to ‘created_at`

# File lib/order_order/extensions.rb, line 23
def reverse_chronological(column=DEFAULT_CHRONOLOGICAL_COLUMN)
  order("#{column} DESC")
end
since(time) click to toggle source

Return all records created SINCE the provided date

@param [Time] @return [ActiveRecord::Relation]

# File lib/order_order/extensions.rb, line 60
def since(time)
  where("created_at > ?", time)
end

Private Instance Methods

get_alphabetical_column(column_or_options, options) click to toggle source
# File lib/order_order/extensions.rb, line 67
def get_alphabetical_column(column_or_options, options)
  if column_or_options.is_a?(Hash)
    options = column_or_options
    column  = DEFAULT_ALPHABETICAL_COLUMN
  else
    column  = column_or_options
  end

  column = "lower(#{column})" if !options.fetch(:case_sensitive, true)
  column
end