diff --git a/app/models/planting.rb b/app/models/planting.rb index 8bf6dda2c..8d1eb70b9 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -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 diff --git a/db/migrate/20130705104238_add_planted_from_to_planting.rb b/db/migrate/20130705104238_add_planted_from_to_planting.rb new file mode 100644 index 000000000..7eb362a85 --- /dev/null +++ b/db/migrate/20130705104238_add_planted_from_to_planting.rb @@ -0,0 +1,5 @@ +class AddPlantedFromToPlanting < ActiveRecord::Migration + def change + add_column :plantings, :planted_from, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index aab2827b1..bf9e10438 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/factories/planting.rb b/spec/factories/planting.rb index 06499b4a4..963b7a2b0 100644 --- a/spec/factories/planting.rb +++ b/spec/factories/planting.rb @@ -6,5 +6,6 @@ FactoryGirl.define do quantity 33 description "This is a *really* good plant." sunniness 'sun' + planted_from 'seed' end end diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index cfadcf9cd..ca2d4e02d 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -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