mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-02-02 05:31:01 -05:00
Added new fields to garden form
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user