module RandomOrder

Constants

ORDERS
VERSION

Public Class Methods

adapter() click to toggle source
# File lib/random_order.rb, line 6
def self.adapter
  @@adapter ||= begin
    if self.const_defined?("ActiveRecord")
      ActiveRecord::Base.configurations[Rails.env]['adapter']
    end
  end
end
order_func() click to toggle source
# File lib/random_order.rb, line 14
def RandomOrder.order_func
  ORDERS[adapter]
end
sql_query_string(model, n) click to toggle source
# File lib/random_order.rb, line 18
def self.sql_query_string(model, n)
  model.from(
    "(SELECT * FROM #{model.table_name}, ( SELECT #{model.primary_key} AS sid "\
    "FROM #{model.table_name} ORDER BY #{RandomOrder.order_func} LIMIT #{n} ) tmp WHERE "\
    "#{model.table_name}.#{model.primary_key} = tmp.sid) as #{model.table_name}"
  )
end