Commit d1496a55e665028d4e8bdbcbd0ca66c8260b43ff

Authored by Antonio Terceiro
2 parents e449dda9 49dc0512

Merge branch 'stable'

app/models/communities_block.rb
... ... @@ -49,7 +49,7 @@ class CommunitiesBlock < ProfileListBlock
49 49 # FIXME when owner is an environment (i.e. listing communities globally
50 50 # this can become SLOW)
51 51 if block.owner.kind_of?(Environment)
52   - block.owner.communities.all(:conditions => {:visible => true}, :limit => block.limit, :order => 'random()').map(&:id)
  52 + block.owner.communities.all(:conditions => {:visible => true}, :limit => block.limit, :order => Noosfero::SQL.random_function).map(&:id)
53 53 else
54 54 block.owner.communities(:visible => true).map(&:id)
55 55 end
... ...
app/models/enterprises_block.rb
... ... @@ -46,7 +46,7 @@ class EnterprisesBlock < ProfileListBlock
46 46 # FIXME when owner is an environment (i.e. listing enterprises globally
47 47 # this can become SLOW)
48 48 if block.owner.kind_of?(Environment)
49   - block.owner.enterprises.all(:conditions => {:visible => true}, :limit => block.limit, :order => 'random()').map(&:id)
  49 + block.owner.enterprises.all(:conditions => {:visible => true}, :limit => block.limit, :order => Noosfero::SQL.random_function).map(&:id)
50 50 else
51 51 block.owner.enterprises.select(&:visible).map(&:id)
52 52 end
... ...
app/models/people_block.rb
... ... @@ -18,7 +18,7 @@ class PeopleBlock < ProfileListBlock
18 18  
19 19 class Finder < ProfileListBlock::Finder
20 20 def ids
21   - block.owner.people.visible.all(:limit => block.limit, :order => 'random()').map(&:id)
  21 + block.owner.people.visible.all(:limit => block.limit, :order => Noosfero::SQL.random_function).map(&:id)
22 22 end
23 23 end
24 24  
... ...
app/models/profile_list_block.rb
... ... @@ -42,7 +42,7 @@ class ProfileListBlock &lt; Block
42 42 rand(top)
43 43 end
44 44 def ids
45   - block.owner.profiles.visible.all(:limit => block.limit, :order => 'random()').map(&:id)
  45 + block.owner.profiles.visible.all(:limit => block.limit, :order => Noosfero::SQL.random_function).map(&:id)
46 46 end
47 47 end
48 48  
... ...
lib/noosfero/sql.rb 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +module Noosfero
  2 + module SQL
  3 + class << self
  4 +
  5 + def random_function()
  6 + default = 'random()'
  7 + adapter = ActiveRecord::Base.configurations[Rails.env]['adapter']
  8 + {
  9 + 'mysql' => 'rand()'
  10 + }[adapter] || default
  11 + end
  12 +
  13 + end
  14 + end
  15 +end
... ...