Validate units (must be one of individual/bunches/kg/lb)

Also changed "units" attribute to "unit". Oops, we weren't following the
Rails naming convention.
This commit is contained in:
Skud
2013-09-17 17:26:18 +10:00
parent ebf6034659
commit 40ce4ab77b
9 changed files with 19 additions and 9 deletions

View File

@@ -1,7 +1,12 @@
class Harvest < ActiveRecord::Base
attr_accessible :crop_id, :harvested_at, :description, :owner_id, :quantity, :units
attr_accessible :crop_id, :harvested_at, :description, :owner_id, :quantity, :unit
belongs_to :crop
belongs_to :owner, :class_name => 'Member'
UNITS_VALUES = %w(individual bunches kg lb)
validates :unit, :inclusion => { :in => UNITS_VALUES,
:message => "%{value} is not a valid unit" },
:allow_nil => true,
:allow_blank => true
end

View File

@@ -22,7 +22,7 @@
= f.label 'How many?', :class => 'control-label'
.controls
= f.number_field :quantity, :class => 'input-small'
= f.text_field :units, :class => 'input-small'
= f.select(:unit, Harvest::UNITS_VALUES, {:include_blank => false}, :class => 'input-medium')
.control-group
= f.label 'Notes', :class => 'control-label'

View File

@@ -41,7 +41,7 @@
%td
- if harvest.quantity
= harvest.quantity
= harvest.units
= harvest.unit
%td= harvest.description
%td= link_to 'Details', harvest, :class => 'btn btn-mini'

View File

@@ -9,7 +9,7 @@
%b Quantity:
- if ! @harvest.quantity.blank?
= @harvest.quantity
= @harvest.units
= @harvest.unit
- else
not specified

View File

@@ -0,0 +1,5 @@
class ChangeHarvestUnitsToUnit < ActiveRecord::Migration
def change
rename_column :harvests, :units, :unit
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130917060257) do
ActiveRecord::Schema.define(:version => 20130917071545) do
create_table "account_types", :force => true do |t|
t.string "name", :null => false
@@ -92,7 +92,7 @@ ActiveRecord::Schema.define(:version => 20130917060257) do
t.integer "owner_id", :null => false
t.date "harvested_at"
t.decimal "quantity"
t.string "units"
t.string "unit"
t.text "description"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false

View File

@@ -6,7 +6,7 @@ FactoryGirl.define do
owner
harvested_at "2013-09-17"
quantity "9.99"
units "kg"
unit "kg"
description "A lovely harvest"
end
end

View File

@@ -10,7 +10,7 @@ describe "harvests/edit" do
assert_select "form", :action => harvests_path, :method => "post" do
assert_select "select#harvest_crop_id", :name => "harvest[crop_id]"
assert_select "input#harvest_quantity", :name => "harvest[quantity]"
assert_select "input#harvest_units", :name => "harvest[units]"
assert_select "input#harvest_unit", :name => "harvest[unit]"
assert_select "textarea#harvest_description", :name => "harvest[description]"
end
end

View File

@@ -10,7 +10,7 @@ describe "harvests/new" do
assert_select "form", :action => harvests_path, :method => "post" do
assert_select "select#harvest_crop_id", :name => "harvest[crop_id]"
assert_select "input#harvest_quantity", :name => "harvest[quantity]"
assert_select "input#harvest_units", :name => "harvest[units]"
assert_select "input#harvest_unit", :name => "harvest[unit]"
assert_select "textarea#harvest_description", :name => "harvest[description]"
end
end