diff --git a/app/models/garden.rb b/app/models/garden.rb index 029502a5a..a2bd553e7 100644 --- a/app/models/garden.rb +++ b/app/models/garden.rb @@ -7,10 +7,15 @@ class Garden < ActiveRecord::Base has_many :plantings, :order => 'created_at DESC', :dependent => :destroy has_many :crops, :through => :plantings - before_create :replace_blank_name + # before_create :replace_blank_name default_scope order("lower(name) asc") + validates :name, + :format => { + :with => /\S/ + } + def garden_slug "#{owner.login_name}-#{name}".downcase.gsub(' ', '-') end diff --git a/spec/models/garden_spec.rb b/spec/models/garden_spec.rb index d4d36636b..cbbf3a950 100644 --- a/spec/models/garden_spec.rb +++ b/spec/models/garden_spec.rb @@ -14,9 +14,19 @@ describe Garden do @garden.description.should == "This is a **totally** cool garden" end - it "should have a name" do - @no_name_garden = FactoryGirl.create(:garden, :name => nil, :description => "New Garden") - @no_name_garden.name.should_not be_blank + it "doesn't allow a nil name" do + @garden = FactoryGirl.build(:garden, :name => nil) + @garden.should_not be_valid + end + + it "doesn't allow a blank name" do + @garden = FactoryGirl.build(:garden, :name => "") + @garden.should_not be_valid + end + + it "doesn't allow a name with only spaces" do + @garden = FactoryGirl.build(:garden, :name => " ") + @garden.should_not be_valid end it "should have an owner" do @@ -86,14 +96,4 @@ describe Garden do Planting.count.should == all - 2 end - it "replaces missing name with (no name)" do - @no_name_garden = FactoryGirl.create(:garden, :name => nil) - @no_name_garden.name.should == "(no name)" - end - - it "replaces whitespace-only names with (no name)" do - @no_name_garden = FactoryGirl.create(:garden, :name => " ") - @no_name_garden.name.should == "(no name)" - end - end