Randomly display 8 crops on the garden page

This commit is contained in:
Skud
2012-11-26 21:34:23 +11:00
parent 2a64f04c2d
commit 08302a4864
4 changed files with 30 additions and 4 deletions

View File

@@ -1,2 +1,7 @@
module ApplicationHelper
def random_crop
Crop.random
end
end

View File

@@ -3,4 +3,9 @@ class Crop < ActiveRecord::Base
friendly_id :system_name, use: :slugged
attr_accessible :en_wikipedia_url, :system_name
has_many :scientific_names
def Crop.random
@crop = Crop.offset(rand(Crop.count)).first
return @crop
end
end

View File

@@ -14,12 +14,14 @@
.span9
%h2 What's planted here?
%div.alert
%button.close{:type => 'button', 'data-dismiss' => 'alert'} ×
Note: these are a random selection, and don't represent actual plantings
%ul.thumbnails
- (1..8).each do
%li.span2
.thumbnail
= image_tag('http://placehold.it/150x150', :alt => '', :class => 'img-rounded')
Crop name
= render :partial => "crops/thumbnail", :locals => { :crop => random_crop }
%h2 Updates

View File

@@ -1,4 +1,4 @@
require 'spec_helper'
rGkequire 'spec_helper'
describe Crop do
context 'all fields present' do
@@ -27,4 +27,18 @@ describe Crop do
expect { @crop.save }.to raise_error ActiveRecord::StatementInvalid
end
end
context 'random' do
before(:each) do
@crop = Crop.new
@crop.system_name = "Tomato"
@crop.en_wikipedia_url = "http://en.wikipedia.org/wiki/Tomato"
@crop.save
end
it 'should find a random crop' do
@rand_crop = Crop.random
@rand_crop.system_name.should == 'Tomato'
end
end
end