diff --git a/app/models/garden.rb b/app/models/garden.rb index ec9146c7b..7accf1be8 100644 --- a/app/models/garden.rb +++ b/app/models/garden.rb @@ -22,6 +22,32 @@ class Garden < ActiveRecord::Base :with => /\S/ } + validates :area, + :numericality => { :only_integer => false }, + :allow_nil => true + + AREA_UNITS_VALUES = { + "square metres" => "square metre", + "square feet" => "square feet", + "hectares" => "hectare", + "acres" => "acre" + } + validates :area_unit, :inclusion => { :in => AREA_UNITS_VALUES.values, + :message => "%{value} is not a valid area unit" }, + :allow_nil => true, + :allow_blank => true + + after_validation :cleanup_area + + def cleanup_area + if area == 0 + self.area = nil + end + if area.blank? + self.area_unit = nil + end + end + def garden_slug "#{owner.login_name}-#{name}".downcase.gsub(' ', '-') end diff --git a/app/views/gardens/_form.html.haml b/app/views/gardens/_form.html.haml index d5d5768af..8a2739460 100644 --- a/app/views/gardens/_form.html.haml +++ b/app/views/gardens/_form.html.haml @@ -1,4 +1,4 @@ -= form_for @garden, :html => {} do |f| += form_for(@garden, :html => {:class => "form-horizontal"}) do |f| - if @garden.errors.any? #error_explanation %h2= "#{pluralize(@garden.errors.count, "error")} prohibited this garden from being saved:" @@ -6,12 +6,37 @@ - @garden.errors.full_messages.each do |msg| %li= msg - .control_group - = f.label "Garden name: ", :class => 'control-label' - .controls= f.text_field :name .control-group - = f.label 'Description: ', :class => 'control-label' - .controls= f.text_area :description, :rows => 6 + = f.label :name, :class => 'control-label' + .controls + = f.text_field :name + + .control-group + = f.label :description, :class => 'control-label' + .controls + = f.text_area :description, :rows => 6 + + .control-group + = f.label :location, :class => 'control-label' + .controls + = f.text_field :location + %span.help-inline + A garden's location does not have to be the same as the place + where you live. + + .control-group + = f.label :area, :class => 'control-label' + .controls + = f.number_field :area, :class => 'input-small' + = f.select(:area_unit, Garden::AREA_UNITS_VALUES, {:include_blank => false}, :class => 'input-medium') + + .control-group + = f.label 'Active? ', :class => 'control-label' + .controls + = f.check_box :active + %span.help-inline + You can mark a garden as inactive if you no longer use it. + .form-actions = f.submit 'Save Garden', :class => 'btn btn-primary' diff --git a/spec/factories/garden.rb b/spec/factories/garden.rb index d699e5e58..0a2b6a7c8 100644 --- a/spec/factories/garden.rb +++ b/spec/factories/garden.rb @@ -3,6 +3,7 @@ FactoryGirl.define do name 'Springfield Community Garden' description "This is a **totally** cool garden" owner + active true # the following are used for testing alphabetical ordering factory :garden_a do diff --git a/spec/models/garden_spec.rb b/spec/models/garden_spec.rb index cbbf3a950..da6ef2b2d 100644 --- a/spec/models/garden_spec.rb +++ b/spec/models/garden_spec.rb @@ -23,7 +23,7 @@ describe Garden 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 @@ -96,4 +96,57 @@ describe Garden do Planting.count.should == all - 2 end + context 'area' do + it 'allows numeric area' do + @garden = FactoryGirl.build(:garden, :area => 33) + @garden.should be_valid + end + + it 'allows decimal quantities' do + @garden = FactoryGirl.build(:garden, :area => 3.3) + @garden.should be_valid + end + + it 'allows blank quantities' do + @garden = FactoryGirl.build(:garden, :area => '') + @garden.should be_valid + end + + it 'allows nil quantities' do + @garden = FactoryGirl.build(:garden, :area => nil) + @garden.should be_valid + end + + it 'cleans up zero quantities' do + @garden = FactoryGirl.build(:garden, :area => 0) + @garden.area.should == 0 + end + + it "doesn't allow non-numeric quantities" do + @garden = FactoryGirl.build(:garden, :area => "99a") + @garden.should_not be_valid + end + end + + context 'units' do + Garden::AREA_UNITS_VALUES.values.push(nil, '').each do |s| + it "#{s} should be a valid unit" do + @garden = FactoryGirl.build(:garden, :area_unit => s) + @garden.should be_valid + end + end + + it 'should refuse invalid unit values' do + @garden = FactoryGirl.build(:garden, :area_unit => 'not valid') + @garden.should_not be_valid + @garden.errors[:area_unit].should include("not valid is not a valid area unit") + end + + it 'sets area unit to blank if area is blank' do + @garden = FactoryGirl.build(:garden, :area => '', :area_unit => 'acre') + @garden.should be_valid + @garden.area_unit.should eq nil + end + end + end diff --git a/spec/views/gardens/edit.html.haml_spec.rb b/spec/views/gardens/edit.html.haml_spec.rb index 53063d12a..781274c2c 100644 --- a/spec/views/gardens/edit.html.haml_spec.rb +++ b/spec/views/gardens/edit.html.haml_spec.rb @@ -18,6 +18,10 @@ describe "gardens/edit" do assert_select "form", :action => gardens_path(@garden), :method => "post" do assert_select "input#garden_name", :name => "garden[name]" assert_select "textarea#garden_description", :name => "garden[description]" + assert_select "input#garden_location", :name => "garden[location]" + assert_select "input#garden_area", :name => "garden[area]" + assert_select "select#garden_area_unit", :name => "garden[area_unit]" + assert_select "input#garden_active", :name => "garden[active]" end end end diff --git a/spec/views/gardens/new.html.haml_spec.rb b/spec/views/gardens/new.html.haml_spec.rb index cbb11662f..3eb7507a6 100644 --- a/spec/views/gardens/new.html.haml_spec.rb +++ b/spec/views/gardens/new.html.haml_spec.rb @@ -13,6 +13,10 @@ describe "gardens/new" do assert_select "form", :action => gardens_path, :method => "post" do assert_select "input#garden_name", :name => "garden[name]" assert_select "textarea#garden_description", :name => "garden[description]" + assert_select "input#garden_location", :name => "garden[location]" + assert_select "input#garden_area", :name => "garden[area]" + assert_select "select#garden_area_unit", :name => "garden[area_unit]" + assert_select "input#garden_active", :name => "garden[active]" end end end