From 233f9dfd880a840703d8f8d15021e6b59b90c976 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Mon, 26 Jan 2015 19:18:23 +0000 Subject: [PATCH] Split "days until maturity" into min and max fields. --- app/controllers/seeds_controller.rb | 3 ++- app/models/seed.rb | 7 ++++++- .../seeds/_days_until_maturity.html.haml | 9 +++++++++ app/views/seeds/_form.html.haml | 7 +++++-- app/views/seeds/show.html.haml | 2 +- .../20150124110540_add_properties_to_seeds.rb | 3 ++- db/schema.rb | 19 ++++++++++--------- spec/features/seeds/adding_seeds_spec.rb | 3 ++- 8 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 app/views/seeds/_days_until_maturity.html.haml diff --git a/app/controllers/seeds_controller.rb b/app/controllers/seeds_controller.rb index a1d6de8e3..a2e79afaf 100644 --- a/app/controllers/seeds_controller.rb +++ b/app/controllers/seeds_controller.rb @@ -112,6 +112,7 @@ class SeedsController < ApplicationController def seed_params params.require(:seed).permit( :owner_id, :crop_id, :description, :quantity, :plant_before, - :days_until_maturity, :organic, :gmo, :heirloom, :tradable_to, :slug) + :days_until_maturity_min, :days_until_maturity_max, :organic, :gmo, + :heirloom, :tradable_to, :slug) end end diff --git a/app/models/seed.rb b/app/models/seed.rb index 96b9fa5a2..cae7402d9 100644 --- a/app/models/seed.rb +++ b/app/models/seed.rb @@ -13,7 +13,12 @@ class Seed < ActiveRecord::Base :only_integer => true, :greater_than_or_equal_to => 0 }, :allow_nil => true - validates :days_until_maturity, + validates :days_until_maturity_min, + :numericality => { + :only_integer => true, + :greater_than_or_equal_to => 0 }, + :allow_nil => true + validates :days_until_maturity_max, :numericality => { :only_integer => true, :greater_than_or_equal_to => 0 }, diff --git a/app/views/seeds/_days_until_maturity.html.haml b/app/views/seeds/_days_until_maturity.html.haml new file mode 100644 index 000000000..b8db4a203 --- /dev/null +++ b/app/views/seeds/_days_until_maturity.html.haml @@ -0,0 +1,9 @@ +- if seed.days_until_maturity_min.blank? + - if seed.days_until_maturity_max.blank? + unknown + - else + = seed.days_until_maturity_max.to_s +- elsif seed.days_until_maturity_max.blank? + = seed.days_until_maturity_min.to_s +- else + != "#{seed.days_until_maturity_min}–#{seed.days_until_maturity_max}" diff --git a/app/views/seeds/_form.html.haml b/app/views/seeds/_form.html.haml index c0b09c3bc..9472e2799 100644 --- a/app/views/seeds/_form.html.haml +++ b/app/views/seeds/_form.html.haml @@ -22,9 +22,12 @@ .col-md-2 = f.text_field :plant_before, :class => 'add-datepicker form-control', :value => @seed.plant_before ? @seed.plant_before.to_s(:ymd) : '' .form-group - = f.label :days_until_maturity, 'Days until maturity:', :class => 'control-label col-md-2' + = f.label :days_until_maturity_min, 'Days until maturity, min:', :class => 'control-label col-md-2' .col-md-2 - = f.number_field :days_until_maturity, :class => 'form-control' + = f.number_field :days_until_maturity_min, :class => 'form-control' + = f.label :days_until_maturity_max, 'Days until maturity, max:', :class => 'control-label col-md-2' + .col-md-2 + = f.number_field :days_until_maturity_max, :class => 'form-control' .form-group = f.label :organic, 'Organic?', :class => 'control-label col-md-2' .col-md-8 diff --git a/app/views/seeds/show.html.haml b/app/views/seeds/show.html.haml index dfc47108c..c2f3399ea 100644 --- a/app/views/seeds/show.html.haml +++ b/app/views/seeds/show.html.haml @@ -15,7 +15,7 @@ = @seed.plant_before.to_s %p %b Days until maturity: - = @seed.days_until_maturity.to_s + = render :partial => 'days_until_maturity', :locals => { :seed => @seed } %p %b Organic? = @seed.organic diff --git a/db/migrate/20150124110540_add_properties_to_seeds.rb b/db/migrate/20150124110540_add_properties_to_seeds.rb index 46cc67461..e2fa64fe7 100644 --- a/db/migrate/20150124110540_add_properties_to_seeds.rb +++ b/db/migrate/20150124110540_add_properties_to_seeds.rb @@ -1,6 +1,7 @@ class AddPropertiesToSeeds < ActiveRecord::Migration def change - add_column :seeds, :days_until_maturity, :integer + add_column :seeds, :days_until_maturity_min, :integer + add_column :seeds, :days_until_maturity_max, :integer add_column :seeds, :organic, :text, :default => 'unknown' add_column :seeds, :gmo, :text, :default => 'unknown' add_column :seeds, :heirloom, :text, :default => 'unknown' diff --git a/db/schema.rb b/db/schema.rb index 8ab4d579d..365c9d151 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -308,19 +308,20 @@ ActiveRecord::Schema.define(version: 20150124110540) do end create_table "seeds", force: true do |t| - t.integer "owner_id", null: false - t.integer "crop_id", null: false + t.integer "owner_id", null: false + t.integer "crop_id", null: false t.text "description" t.integer "quantity" t.date "plant_before" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "tradable_to", default: "nowhere" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "tradable_to", default: "nowhere" t.string "slug" - t.integer "days_until_maturity" - t.text "organic", default: "unknown" - t.text "gmo", default: "unknown" - t.text "heirloom", default: "unknown" + t.integer "days_until_maturity_min" + t.integer "days_until_maturity_max" + t.text "organic", default: "unknown" + t.text "gmo", default: "unknown" + t.text "heirloom", default: "unknown" end add_index "seeds", ["slug"], name: "index_seeds_on_slug", unique: true, using: :btree diff --git a/spec/features/seeds/adding_seeds_spec.rb b/spec/features/seeds/adding_seeds_spec.rb index 65bc3fcdd..e69b380d6 100644 --- a/spec/features/seeds/adding_seeds_spec.rb +++ b/spec/features/seeds/adding_seeds_spec.rb @@ -17,7 +17,8 @@ feature "Harvesting a crop", :js => true do within "form#new_seed" do fill_in "Quantity:", :with => 42 fill_in "Plant before:", :with => "2014-06-15" - fill_in "Days until maturity:", :with => 999 + fill_in "Days until maturity, min:", :with => 999 + fill_in "Days until maturity, max:", :with => 1999 select "certified organic", :from => "Organic?" select "non-certified GMO-free", :from => "GMO?" select "heirloom", :from => "Heirloom?"