From 49dc051234959de99021ee0ac12463d1ecf4182b Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Sun, 1 Aug 2010 12:50:37 -0300 Subject: [PATCH] MySQL-compatible random record selection --- app/models/communities_block.rb | 2 +- app/models/enterprises_block.rb | 2 +- app/models/people_block.rb | 2 +- app/models/profile_list_block.rb | 2 +- lib/noosfero/sql.rb | 15 +++++++++++++++ 5 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 lib/noosfero/sql.rb diff --git a/app/models/communities_block.rb b/app/models/communities_block.rb index d17403f..086fdb7 100644 --- a/app/models/communities_block.rb +++ b/app/models/communities_block.rb @@ -49,7 +49,7 @@ class CommunitiesBlock < ProfileListBlock # FIXME when owner is an environment (i.e. listing communities globally # this can become SLOW) if block.owner.kind_of?(Environment) - block.owner.communities.all(:conditions => {:visible => true}, :limit => block.limit, :order => 'random()').map(&:id) + block.owner.communities.all(:conditions => {:visible => true}, :limit => block.limit, :order => Noosfero::SQL.random_function).map(&:id) else block.owner.communities(:visible => true).map(&:id) end diff --git a/app/models/enterprises_block.rb b/app/models/enterprises_block.rb index 07625c8..3c30499 100644 --- a/app/models/enterprises_block.rb +++ b/app/models/enterprises_block.rb @@ -46,7 +46,7 @@ class EnterprisesBlock < ProfileListBlock # FIXME when owner is an environment (i.e. listing enterprises globally # this can become SLOW) if block.owner.kind_of?(Environment) - block.owner.enterprises.all(:conditions => {:visible => true}, :limit => block.limit, :order => 'random()').map(&:id) + block.owner.enterprises.all(:conditions => {:visible => true}, :limit => block.limit, :order => Noosfero::SQL.random_function).map(&:id) else block.owner.enterprises.select(&:visible).map(&:id) end diff --git a/app/models/people_block.rb b/app/models/people_block.rb index 9e40648..2714502 100644 --- a/app/models/people_block.rb +++ b/app/models/people_block.rb @@ -18,7 +18,7 @@ class PeopleBlock < ProfileListBlock class Finder < ProfileListBlock::Finder def ids - block.owner.people.visible.all(:limit => block.limit, :order => 'random()').map(&:id) + block.owner.people.visible.all(:limit => block.limit, :order => Noosfero::SQL.random_function).map(&:id) end end diff --git a/app/models/profile_list_block.rb b/app/models/profile_list_block.rb index 832424a..13e6ba7 100644 --- a/app/models/profile_list_block.rb +++ b/app/models/profile_list_block.rb @@ -42,7 +42,7 @@ class ProfileListBlock < Block rand(top) end def ids - block.owner.profiles.visible.all(:limit => block.limit, :order => 'random()').map(&:id) + block.owner.profiles.visible.all(:limit => block.limit, :order => Noosfero::SQL.random_function).map(&:id) end end diff --git a/lib/noosfero/sql.rb b/lib/noosfero/sql.rb new file mode 100644 index 0000000..ca3585b --- /dev/null +++ b/lib/noosfero/sql.rb @@ -0,0 +1,15 @@ +module Noosfero + module SQL + class << self + + def random_function() + default = 'random()' + adapter = ActiveRecord::Base.configurations[Rails.env]['adapter'] + { + 'mysql' => 'rand()' + }[adapter] || default + end + + end + end +end -- libgit2 0.21.2