From 08302a48644befeaa38d154f548eda4ec8164720 Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 26 Nov 2012 21:34:23 +1100 Subject: [PATCH] Randomly display 8 crops on the garden page --- app/helpers/application_helper.rb | 5 +++++ app/models/crop.rb | 5 +++++ app/views/gardens/show.html.haml | 8 +++++--- spec/models/crop_spec.rb | 16 +++++++++++++++- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be7945..11d1c7f13 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,2 +1,7 @@ module ApplicationHelper + + def random_crop + Crop.random + end + end diff --git a/app/models/crop.rb b/app/models/crop.rb index 9efd7b272..55ec38ded 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -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 diff --git a/app/views/gardens/show.html.haml b/app/views/gardens/show.html.haml index bf527f4c9..1e832338c 100644 --- a/app/views/gardens/show.html.haml +++ b/app/views/gardens/show.html.haml @@ -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 diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index d2859787b..b4f6a4025 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -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