added planted_from (eg seed, seedling)

This commit is contained in:
Skud
2013-07-05 20:53:11 +10:00
parent 2483d760d3
commit b71ac3decb
5 changed files with 63 additions and 17 deletions

View File

@@ -3,7 +3,7 @@ class Planting < ActiveRecord::Base
friendly_id :planting_slug, use: :slugged
attr_accessible :crop_id, :description, :garden_id, :planted_at,
:quantity, :sunniness
:quantity, :sunniness, :planted_from
belongs_to :garden
belongs_to :crop
@@ -29,6 +29,22 @@ class Planting < ActiveRecord::Base
:allow_nil => true,
:allow_blank => true
PLANTED_FROM_VALUES = [
'seed',
'seedling',
'cutting',
'root division',
'runner',
'bare root plant',
'advanced plant',
'graft',
'layering'
]
validates :planted_from, :inclusion => { :in => PLANTED_FROM_VALUES,
:message => "%{value} is not a valid planting method" },
:allow_nil => true,
:allow_blank => true
def planting_slug
"#{owner.login_name}-#{garden}-#{crop}".downcase.gsub(' ', '-')
end

View File

@@ -0,0 +1,5 @@
class AddPlantedFromToPlanting < ActiveRecord::Migration
def change
add_column :plantings, :planted_from, :string
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130606233733) do
ActiveRecord::Schema.define(:version => 20130705104238) do
create_table "account_types", :force => true do |t|
t.string "name", :null => false
@@ -179,15 +179,16 @@ ActiveRecord::Schema.define(:version => 20130606233733) do
end
create_table "plantings", :force => true do |t|
t.integer "garden_id", :null => false
t.integer "crop_id", :null => false
t.integer "garden_id", :null => false
t.integer "crop_id", :null => false
t.date "planted_at"
t.integer "quantity"
t.text "description"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "slug"
t.string "sunniness"
t.string "planted_from"
end
add_index "plantings", ["slug"], :name => "index_plantings_on_slug", :unique => true

View File

@@ -6,5 +6,6 @@ FactoryGirl.define do
quantity 33
description "This is a *really* good plant."
sunniness 'sun'
planted_from 'seed'
end
end

View File

@@ -49,21 +49,44 @@ describe Planting do
end
end
it 'should have a sunniness value' do
@planting.sunniness.should eq 'sun'
end
context 'sunniness' do
it 'should have a sunniness value' do
@planting.sunniness.should eq 'sun'
end
it 'all three valid sunniness values should work' do
['sun', 'shade', 'semi-shade', nil, ''].each do |s|
@planting = FactoryGirl.build(:planting, :sunniness => s)
@planting.should be_valid
it 'all three valid sunniness values should work' do
['sun', 'shade', 'semi-shade', nil, ''].each do |s|
@planting = FactoryGirl.build(:planting, :sunniness => s)
@planting.should be_valid
end
end
it 'should refuse invalid sunniness values' do
@planting = FactoryGirl.build(:planting, :sunniness => 'not valid')
@planting.should_not be_valid
@planting.errors[:sunniness].should include("not valid is not a valid sunniness value")
end
end
it 'should refuse invalid sunniness values' do
@planting = FactoryGirl.build(:planting, :sunniness => 'not valid')
@planting.should_not be_valid
@planting.errors[:sunniness].should include("not valid is not a valid sunniness value")
context 'planted from' do
it 'should have a planted_from value' do
@planting.planted_from.should eq 'seed'
end
it 'all valid planted_from values should work' do
['seed', 'seedling', 'cutting', 'root division',
'runner', 'bare root plant', 'advanced plant',
'graft', 'layering', nil, ''].each do |p|
@planting = FactoryGirl.build(:planting, :planted_from => p)
@planting.should be_valid
end
end
it 'should refuse invalid planted_from values' do
@planting = FactoryGirl.build(:planting, :planted_from => 'not valid')
@planting.should_not be_valid
@planting.errors[:planted_from].should include("not valid is not a valid planting method")
end
end
# we decided that all the tests for the planting/photo association would