Permalink
Browse files

Now shows only 6 random orgs in the front page

  • Loading branch information...
1 parent 79f0185 commit ba72f6951b72d4b7671ef6b53a71723d91215d13 @eduaquiles eduaquiles committed Sep 13, 2010
View
2 app/controllers/home_controller.rb
@@ -1,7 +1,7 @@
class HomeController < ApplicationController
def index
- @organizations = Organization.all
+ @organizations = Organization.get_random_list(6)
respond_to do |format|
format.html # index.html.erb
View
8 app/models/organization.rb
@@ -1,4 +1,7 @@
class Organization < ActiveRecord::Base
+
+ extend Random
+
has_friendly_id :name, :use_slug => true, :cache_column => 'name_slug', :approximate_ascii => true
validates_presence_of :name, :contact, :city, :country, :password, :announcer, :email
@@ -23,5 +26,10 @@ def self.slug_name(str)
def self.slug_city(name)
name.to_s.to_slug.approximate_ascii.to_s.downcase
end
+
+ def self.get_random_list(limit)
+ organizations = Organization.all(:select => :id, :order => db_random, :limit => limit)
+ organizations.each { |u| u.reload}
+ end
end
View
9 db/migrate/201009100000_create_index_organization.rb
@@ -0,0 +1,9 @@
+class CreateIndexOrganization < ActiveRecord::Migration
+ def self.up
+ add_index :organizations, :id
+ end
+
+ def self.down
+ drop_table :organizations
+ end
+end
View
2 db/schema.rb
@@ -26,6 +26,8 @@
t.datetime "updated_at"
end
+ add_index "organizations", ["id"], :name => "index_organizations_on_id"
+
create_table "organizations_tags", :id => false, :force => true do |t|
t.integer "organization_id"
t.integer "tag_id"
View
12 lib/random.rb
@@ -0,0 +1,12 @@
+module Random
+ def db_random
+ case ActiveRecord::Base.connection.adapter_name
+ when 'MySQL'
+ 'rand()'
+ when 'SQLite'
+ 'random()'
+ when 'PostgreSQL'
+ 'random()'
+ end
+ end
+end
View
28 test/fixtures/organizations.yml
@@ -63,4 +63,30 @@ santa_casa:
needs: mucha cosa
password: 1
email: a@a.a
- announcer: announcer
+ announcer: announcer
+
+tamar:
+ id: 7
+ name: Projeto Tamar
+ name_slug: projeto-tamar
+ contact: lululu
+ city: Porto Alegre
+ city_slug: porto alegre
+ country: Brazil
+ needs: peixe
+ password: 1
+ email: a@a.a
+ announcer: announcer
+
+tamar:
+ id: 8
+ name: aacd
+ name_slug: aacd
+ contact: lululu
+ city: Porto Alegre
+ city_slug: porto alegre
+ country: Brazil
+ needs: tuto
+ password: 1
+ email: a@a.a
+ announcer: announcer
View
14 test/unit/organization_test.rb
@@ -56,4 +56,18 @@ class OrganizationTest < ActiveSupport::TestCase
assert_equal "minha-organizacao", org.name_slug, "name slug is '#{org.name_slug}', but 'minha-organizacao' was expected"
end
+ should "retrieve at most the parameter informed of organizations" do
+ orgs = Organization.get_random_list(2)
+ assert_operator orgs.count, :<=, 2
+
+ orgs = Organization.get_random_list(3)
+ assert_operator orgs.count, :<=, 3
+
+ orgs = Organization.get_random_list(1)
+ assert_operator orgs.count, :<=, 1
+
+ orgs = Organization.get_random_list(6)
+ assert_operator orgs.count, :<=, 6
+ end
+
end

0 comments on commit ba72f69

Please sign in to comment.