From 40ce4ab77b00e4fd09db1afe43574181af20690c Mon Sep 17 00:00:00 2001 From: Skud Date: Tue, 17 Sep 2013 17:26:18 +1000 Subject: [PATCH] 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. --- app/models/harvest.rb | 7 ++++++- app/views/harvests/_form.html.haml | 2 +- app/views/harvests/index.html.haml | 2 +- app/views/harvests/show.html.haml | 2 +- db/migrate/20130917071545_change_harvest_units_to_unit.rb | 5 +++++ db/schema.rb | 4 ++-- spec/factories/harvests.rb | 2 +- spec/views/harvests/edit.html.haml_spec.rb | 2 +- spec/views/harvests/new.html.haml_spec.rb | 2 +- 9 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20130917071545_change_harvest_units_to_unit.rb diff --git a/app/models/harvest.rb b/app/models/harvest.rb index 48e6ede26..8e54b31f6 100644 --- a/app/models/harvest.rb +++ b/app/models/harvest.rb @@ -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 diff --git a/app/views/harvests/_form.html.haml b/app/views/harvests/_form.html.haml index 31f90bd30..99926a892 100644 --- a/app/views/harvests/_form.html.haml +++ b/app/views/harvests/_form.html.haml @@ -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' diff --git a/app/views/harvests/index.html.haml b/app/views/harvests/index.html.haml index e0b96b981..647e66b3b 100644 --- a/app/views/harvests/index.html.haml +++ b/app/views/harvests/index.html.haml @@ -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' diff --git a/app/views/harvests/show.html.haml b/app/views/harvests/show.html.haml index 7f866858b..cd2f2666e 100644 --- a/app/views/harvests/show.html.haml +++ b/app/views/harvests/show.html.haml @@ -9,7 +9,7 @@ %b Quantity: - if ! @harvest.quantity.blank? = @harvest.quantity - = @harvest.units + = @harvest.unit - else not specified diff --git a/db/migrate/20130917071545_change_harvest_units_to_unit.rb b/db/migrate/20130917071545_change_harvest_units_to_unit.rb new file mode 100644 index 000000000..d843c2c3d --- /dev/null +++ b/db/migrate/20130917071545_change_harvest_units_to_unit.rb @@ -0,0 +1,5 @@ +class ChangeHarvestUnitsToUnit < ActiveRecord::Migration + def change + rename_column :harvests, :units, :unit + end +end diff --git a/db/schema.rb b/db/schema.rb index 19237834b..3a8149843 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/factories/harvests.rb b/spec/factories/harvests.rb index 6b9e336e9..40f5c15fb 100644 --- a/spec/factories/harvests.rb +++ b/spec/factories/harvests.rb @@ -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 diff --git a/spec/views/harvests/edit.html.haml_spec.rb b/spec/views/harvests/edit.html.haml_spec.rb index dd26b7926..d2f50b5f1 100644 --- a/spec/views/harvests/edit.html.haml_spec.rb +++ b/spec/views/harvests/edit.html.haml_spec.rb @@ -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 diff --git a/spec/views/harvests/new.html.haml_spec.rb b/spec/views/harvests/new.html.haml_spec.rb index 21e89e997..353dff98c 100644 --- a/spec/views/harvests/new.html.haml_spec.rb +++ b/spec/views/harvests/new.html.haml_spec.rb @@ -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