Module: Orderable::ClassMethods

Defined in:
lib/orderable.rb

Instance Method Summary collapse

Instance Method Details

#in_orderObject



14
15
16
# File 'lib/orderable.rb', line 14

def in_order
  order "#{quoted_orderable_field} ASC"
end

#orderable_field_is(field = nil) ⇒ Object



18
19
20
# File 'lib/orderable.rb', line 18

def orderable_field_is(field = nil)
  self.orderable_field = field.to_sym
end

#orderable_sql_for_ids(ids) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# 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_columnObject



32
33
34
# File 'lib/orderable.rb', line 32

def quoted_column
  connection.quote_column_name(orderable_field)
end

#quoted_orderable_fieldObject



28
29
30
# File 'lib/orderable.rb', line 28

def quoted_orderable_field
  "#{quoted_table_name}.#{quoted_column}"
end

#update_order(*id_array) ⇒ Object



22
23
24
25
26
# 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