module Orderable::ClassMethods

Public Instance Methods

in_order() click to toggle source
# File lib/orderable.rb, line 14
def in_order
  order "#{quoted_orderable_field} ASC"
end
orderable_field_is(field = nil) click to toggle source
# File lib/orderable.rb, line 18
def orderable_field_is(field = nil)
  self.orderable_field = field.to_sym
end
orderable_sql_for_ids(ids) click to toggle source
# File lib/orderable.rb, line 36
def orderable_sql_for_ids(ids)
  ids = ids.join(",")
  case adapter = Orderable.adapter
  when /^mysql/
    ["#{quoted_column} = FIND_IN_SET(id, ?)", ids]
  when /^postgres/
    ["#{quoted_column} = STRPOS(?, ','||id||',')", ",#{ids},"]
  else
    raise ArgumentError, "The database adapter #{adapter} is not supported by orderable"
  end
end
quoted_column() click to toggle source
# File lib/orderable.rb, line 32
def quoted_column
  connection.quote_column_name(orderable_field)
end
quoted_orderable_field() click to toggle source
# File lib/orderable.rb, line 28
def quoted_orderable_field
  "#{quoted_table_name}.#{quoted_column}"
end
update_order(*id_array) click to toggle source
# File lib/orderable.rb, line 22
def update_order(*id_array)
  id_array = Array.wrap(id_array).flatten.reject(&:blank?).map(&:to_i).uniq
  return if id_array.blank?
  update_all orderable_sql_for_ids(id_array), ['id IN (?)', id_array]
end