Added validation for garden names.

This commit is contained in:
Ryan Clark
2013-05-13 17:28:37 -07:00
parent ee136666d5
commit 5d03ffdf87
2 changed files with 19 additions and 14 deletions

View File

@@ -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

View File

@@ -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